Joins and unions


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Joins and unions

  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  3. 3. JOIN• To query data from two or more tables, based on a relationship betweencertain column in these tables.• Tables in a database are often related to each other with keys.• Primary key is a column (or a combination of columns) with a uniquevalue for each row.• Foreign Key in one table points to a Primary Key in another table.
  4. 4. Different JOINs in MySQLINNER JOIN(JOIN):Return rows when there is at least one match in both tables.LEFT JOIN:Return all rows from the left table, even if there are no matchesin the right table.RIGHT JOIN:Return all rows from the right table, even if there are no matches in the lefttable.FULL JOIN:Return rows when there is a match in one of the tables.But It does not support in MySQL.
  5. 5. Tbl_deptpk_dept_id dept_name1 CSE2 ECE3 EEE4 CE
  6. 6. tbl_studentpk_student_id student_name student_place fk_dept_id1 baabtra1 Calicut 12 baabtra2 cochin 23 baabtra3 Kannur 34 baabtra4 ooty 35 baabtra5 munnar 16 baabtra6 mlp NULL
  7. 7. INNER JOIN (JOIN)• The INNER JOIN keyword returns rows when there is at least one match inboth tables.Syntax:SELECTcolumn name(s)FROMtable_name1INNER JOINtable_name2ONtable_name1.column_name=table_name2.column_nameTbl_dept Tbl_student
  8. 8. INNER JOIN Examplestudent_name dept_namebaabtra1 CSEbaabtra5 CSEbaabtra2 ECEbaabtra3 EEEbaabtra4 EEESELECT tbl_student.student_name,tbl_dept.dept_nameFROM tbl_deptJOIN tbl_studentON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
  9. 9. LEFT JOIN• The LEFT JOIN keyword returns all rows from the left table (table_name1),even if there are no matches in the right table (table_name2).Syntax:SELECTcolumn_name(s)FROMtable_name1LEFT JOINtable_name2ONtable_name1.column_name=table_name2.column_name.
  10. 10. LEFT JOIN ExampleSELECT tbl_student.student_name,tbl_dept.dept_nameFROM tbl_studentLEFT JOIN tbl_deptON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;Student_name Dept_nameBaabtra1 CSEBaabtra2 ECEBaabtra3 EEEBaabtra4 EEEBaabtra5 CSEBaabtra6 NULL
  11. 11. RIGHT JOIN• The RIGHT JOIN keyword returns all the rows from the right table(table_name2), even if there are no matches in the left table (table_name1).Syntax:SELECTcolumn_name(s)FROMtable_name1RIGHT JOINtable_name2ONtable_name1.column_name=table_name2.column_name.
  12. 12. RIGHT JOIN ExampleSELECT tbl_student.student_name,tbl_dept.dept_nameFROM tbl_studentRIGHT JOIN tbl_deptON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;student_name dept_nameBaabtra1 CSEBaabtra5 CSEBaabtra2 ECEBaabtra3 EEEBaabtra4 EEENULL CE
  13. 13. FULL JOIN• There is no FULL JOIN in MySQL.• We can possible this by using both LEFT and RIGHT Joins together withUNION key word.Tbl_dep Tbl_student
  14. 14. UNION• The UNION operator combines two or more SELECT statements.• The UNION operator is used to combine the result-set of two or moreSELECT statements.• Each SELECT statement within the UNION must have the same number ofcolumns.• The columns must also have similar data types.• The columns in each SELECT statement must be in the same order.
  15. 15. UNION SyntaxSELECT column _ name(s)FROM Table_name1UNIONSELECT column _ name(s)FROM table_name2• The column names in the result-set of a UNION are always equal to thecolumn names in the first SELECT statement in the UNION.
  16. 16. SELECT tbl_dept.dept_nameFROM tbl_deptUNIONSELECT tbl_student.student_nameFROM tbl_student;dept_nameCSEECEEEECEBaabtra1Baabtra2Baabtra3Baabtra4Baabtra5baabtra6
  17. 17. UNION ALL• The UNION operator selects only distinct values by default. If you need toallow duplicate values, use UNION ALL.SyntaxSELECT column_name (s) FROM table_name1UNION ALLSELECT column_name(s) FROM table_name2
  18. 18. UNION ExampleSELECT tbl_student.student_name,tbl_dept.dept_nameFROM tbl_deptLEFT JOIN tbl_studentON tbl_dept.pk_dept_id = tbl_student.fk_dept_idUNIONSELECT tbl_student.student_name,tbl_dept.dept_nameFROM tbl_deptRIGHT JOIN tbl_studentON tbl_dept.pk_dept_id = tbl_student.fk_dept_id;
  19. 19. Student_name Dept_nameBaabtra1 CSEBaabtra5 CSEBaabtra2 ECEBaabtra3 EEEBaabtra4 EEENULL CEBaabtra6 NULL
  20. 20. UNION ALL ExampleSELECT tbl_student.student_name,tbl_dept.dept_nameFROM tbl_deptLEFT JOIN tbl_studentON tbl_dept.pk_dept_id=tbl_student.fk_dept_idUNION ALLSELECT tbl_student.student_name,tbl_dept.dept_nameFROM tbl_deptRIGHT JOIN tbl_studentON tbl_dept.pk_dept_id=tbl_student.fk_dept_id;
  21. 21. Student_name Dept_nameBaabtra1 CSEbaabtra5 CSEbaabtra2 ECEbaabtra3 EEEbaabtra4 EEENULL CEbaabtra1 CSEbaabtra2 ECEbaabtra3 EEEbaabtra4 EEEbaabtra5 CSEbaabtra6 NULL
  22. 22. Questions…..
  23. 23. If this presentation helped you, please visit ourpage and like it.Thanks in | |
  24. 24. Contact Us