JOINS IN MYSQL by Anjali G
INTRODUCTION  Join help us to retrieve data from multiple tables in a single query.  We can use joins in select, update and delete statements.  Types of join: 1) Inner join 2) Cross join 3) Left join 4)Right join 5)Self join
INNER JOIN  This join returns rows when there is at least one match in both the tables. Criteria's before using inner join: 1) first specify the main table it appears in the from clause. 2) Specify the table that want to join with the main table. This is in the inner join clause. 3) Need to specify the join condition or join predicate. The join condition appears after the keyword ON of the INNER JOIN clause. The join condition is the rule for matching rows between the main table and the other tables.  Eg: SELECT xx, xxtable.id, yytable.id, yy FROM xxtable INNER JOIN yytable ON xxtable.id=yytable.id;
SELECT * FROM table1 t1 INNER JOIN table 2 t2 ON t1.col1=t2.col1
CROSS JOIN This join is a Cartesian join that does not necessitate any condition to join. Result set contains record that are multiplication of record number from both the tables. Also known as Cartesian product join Eg: select * from table 1 cross join table 2 ;
LEFT JOIN  LEFT JOIN produces a set of records which matches every entry in the left table regardless of any matching entry in the right table If there is a row in A that matches the WHERE clause, but there is no row in B that matches the ON condition, an extra B row is generated with all columns set to NULL.
RIGHT JOIN  This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.
Example: SELECT * FROM employee RIGHT OUTER JOIN employee_details ON employee_details.employee_id = employee.employee_id
SELF JOIN A self join is a join in which a table is joined with itself In this each row of the table is combined with itself and with every other row of the table.
Example: SELECT e1.name, e2.name,e1.email FROM employees AS e1, employees AS e2 WHERE e1.email = e2.email
THANK YOU
Title of the presentation Name @gmail.com dwww.facebook.com/userna me twitter.com/username in.linkedin.com/in/profilena me Phonenumber
If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com

Mysql joins

  • 2.
  • 3.
    INTRODUCTION  Join helpus to retrieve data from multiple tables in a single query.  We can use joins in select, update and delete statements.  Types of join: 1) Inner join 2) Cross join 3) Left join 4)Right join 5)Self join
  • 4.
    INNER JOIN  Thisjoin returns rows when there is at least one match in both the tables. Criteria's before using inner join: 1) first specify the main table it appears in the from clause. 2) Specify the table that want to join with the main table. This is in the inner join clause. 3) Need to specify the join condition or join predicate. The join condition appears after the keyword ON of the INNER JOIN clause. The join condition is the rule for matching rows between the main table and the other tables.  Eg: SELECT xx, xxtable.id, yytable.id, yy FROM xxtable INNER JOIN yytable ON xxtable.id=yytable.id;
  • 6.
    SELECT * FROMtable1 t1 INNER JOIN table 2 t2 ON t1.col1=t2.col1
  • 7.
    CROSS JOIN This joinis a Cartesian join that does not necessitate any condition to join. Result set contains record that are multiplication of record number from both the tables. Also known as Cartesian product join Eg: select * from table 1 cross join table 2 ;
  • 9.
    LEFT JOIN  LEFTJOIN produces a set of records which matches every entry in the left table regardless of any matching entry in the right table If there is a row in A that matches the WHERE clause, but there is no row in B that matches the ON condition, an extra B row is generated with all columns set to NULL.
  • 11.
    RIGHT JOIN  Thisjoin returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.
  • 12.
    Example: SELECT * FROMemployee RIGHT OUTER JOIN employee_details ON employee_details.employee_id = employee.employee_id
  • 14.
    SELF JOIN A selfjoin is a join in which a table is joined with itself In this each row of the table is combined with itself and with every other row of the table.
  • 15.
    Example: SELECT e1.name, e2.name,e1.email FROMemployees AS e1, employees AS e2 WHERE e1.email = e2.email
  • 16.
  • 17.
    Title of thepresentation Name @gmail.com dwww.facebook.com/userna me twitter.com/username in.linkedin.com/in/profilena me Phonenumber
  • 18.
    If this presentationhelped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 19.
    Contact Us Emarald Mall(Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com

Editor's Notes

  • #11 Table read order forced by left join