• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Joins and unions
 

 

Statistics

Views

Total Views
250
Views on SlideShare
250
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Joins and unions Joins and unions Presentation Transcript

    • 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
    • JOINS & UNIONS INMYSQLARUN KUMAR K Aarunkumar3040@gmail.comwww.facebook.com/Arunkumar3040twitter.com/arunkumar3040in.linkedin.com/in/arunkumar+919496349799
    • 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.
    • 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.
    • Tbl_deptpk_dept_id dept_name1 CSE2 ECE3 EEE4 CE
    • 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
    • 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
    • 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;
    • 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.
    • 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
    • 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.
    • 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
    • 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
    • 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.
    • 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.
    • SELECT tbl_dept.dept_nameFROM tbl_deptUNIONSELECT tbl_student.student_nameFROM tbl_student;dept_nameCSEECEEEECEBaabtra1Baabtra2Baabtra3Baabtra4Baabtra5baabtra6
    • 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
    • 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;
    • Student_name Dept_nameBaabtra1 CSEBaabtra5 CSEBaabtra2 ECEBaabtra3 EEEBaabtra4 EEENULL CEBaabtra6 NULL
    • 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;
    • Student_name Dept_nameBaabtra1 CSEbaabtra5 CSEbaabtra2 ECEbaabtra3 EEEbaabtra4 EEENULL CEbaabtra1 CSEbaabtra2 ECEbaabtra3 EEEbaabtra4 EEEbaabtra5 CSEbaabtra6 NULL
    • Questions…..
    • If this presentation helped you, please visit ourpage facebook.com/baabtra and like it.Thanks in advance.www.baabtra.com | www.massbaab.com |www.baabte.com
    • Contact Us