You are on page 1of 6

JOIN in MySQL

Joins are used to query data from two or more tables, based on a relationship between certain columns in these tables. There are different types of JOINing methods used in MySQL database, which are given below.

INNER JOIN:
Probably the most common join operation MySQL supports is an inner join. It identifies and combines only matching rows which are stored in two or more related tables. A join condition, which indicates how the tables are related, is added with the keywords ON or USING : ON is used when the relationship column has a different name USING is used when the relationship column has the same name in both tables

INNER JOIN with ON clause Syntax:


mysql>SELECT<columnlist>FROMtableAaINNERJOINtableBb ONa.somecolumn=b.othercolumn;

INNER JOIN with ON clause Example:


mysql>SELECTd.deptno,d.dname,e.empno,e.ename FROMdeptdINNERJOINempe ONd.deptno=e.deptid;

INNER JOIN with USING clause Syntax:


mysql>SELECT*FROMtableAaINNERJOINtableBb USING(columnname);

INNER JOIN with USING clause Example:


mysql>SELECTd.deptno,d.dname,e.empno,e.ename FROMdeptdINNERJOINempe USING(deptno);

Joining more than two tables by INNER JOIN:


mysql>SELECT*FROMtableAaINNERJOINtableBb ONa.somecolumn=b.othercolumn INNERJOINtableCc ONb.anothercolumn=c.nextcolumn;

OUTER JOIN:
OUTER JOIN allows us to retrieve all values in a certain table regardless of whether these values are present in other tables. The difference between inner and outer join is: An outer join can identify rows without a match in the joined table. When no match was found, MySQL sets the value of columns from the joined table to NULL.

LEFTOUTERJOIN RIGHTOUTERJOIN FULLOUTERJOIN

LEFT OUTER JOIN:


This type of join will display matching records from both tables and all unmatched records of the left table. Left table means which table name is listed on the left side of the JOIN keywords.

Syntax:
mysql>SELECT<columnlist>FROMtable1aLEFTOUTERJOINtable2 bONa.somecolumn=b.othercolumn;

Example:
mysql> SELECT d.deptno, d.dname, e.empno, e.ename FROM dept d LEFTOUTERJOINemployeeseONd.deptno=e.deptnumber;

RIGHT OUTER JOIN:


This type of join will display matching records from both tables and all unmatched records of the right table. Right table means which table name is listed on the right side of the JOIN keywords.

Syntax:
mysql>SELECT<columnlist>FROMtable1aRIGHTOUTERJOIN table2bONa.somecolumn=b.othercolumn;

Example:
mysql> SELECT d.deptno, d.dname, e.empno, e.ename FROM dept d RIGHTOUTERJOINemployeeseONd.deptno=e.deptnumber;

FULL OUTER JOIN:


This type of join will display matching records from both tables and all unmatched records of left as well as table. MySQL Database does not support the full outer join functionality directly, but they can emulate it through the use of an inner join and UNION ALL selects of the "single table rows" from left and right tables respectively. For Example: mysql> SELECT d.deptno, d.dname, e.empno, e.ename FROM dept d LEFTOUTERJOINemployeeseONd.deptno=e.deptnumber UNIONALL SELECT d.deptno, d.dname, e.empno, e.ename FROM dept d RIGHT OUTERJOINemployeeseONd.deptno=e.deptnumber;

SELF JOIN:
A self-join, also known as an inner join, is a structured query language (SQL) statement where a queried table is joined to itself. The self-join statement is necessary when two sets of data, within the same table, are compared. We can use a self-join to simplify nested SQL queries where the inner and outer queries reference the same table. These joins allow you to retrieve related records from the same table. The most common case where you'd use a self-join is when you have a table that references itself, such as the employees table shown below:

+++++ |id|first_name|last_name|manager| +++++ |1|Pat|Crystal|NULL| |2|Dennis|Miller|1| |3|Jacob|Smith|1| |4|Allen|Hunter|2| |5|Marry|Underwood|3| |6|Joy|Needham|3| +++++ In this table, the manager attribute simply references the employee ID of another employee in the same table. For example, Dennis Miller reports to Pat Crystal. Pat is apparently the president of this company, as she reports to no one.

Suppose you're tasked with writing a SQL query to retrieve a list of employees and their managers. You can't write a basic SQL SELECT statement to retrieve this information, as you need to cross reference information contained in other records within the same table. Fortunately, you can use a self-join to solve this dilemma by joining the table to itself. Following syntax will retrieve the desired results: mysql>SELECTe.first_nameAS'EmployeeFN',e.last_nameAS 'EmployeeLN',m.first_nameAS'ManagerFN',m.last_nameAS 'ManagerLN'FROMemployeesASeLEFTOUTERJOINemployeesASm ONe.manager=m.id; Notice that it's extremely important to select the correct join type when writing a self-join. In this case, we used a LEFT OUTER JOIN to ensure we had output records corresponding to each employee. If we used an INNER JOIN instead, we would have omitted Pat Crystal, the company president, from our list, as she does not have a manager.

And following is the corresponding output:


+++++ |EmployeeFN|EmployeeLN|ManagerFN|ManagerLN| +++++ |Pat|Crystal|NULL|NULL| |Dennis|Miller|Pat|Crystal| |Jacob|Smith|Pat|Crystal| |Allen|Hunter|Dennis|Miller| |Marry|Underwood|Jacob|Smith| |Joy|Needham|Jacob|Smith| +++++

CROSS JOIN:
The cross join operation retrieves data between two tables as a Cartesian product of set theory in mathematics. Each row will get multiplied by other rows. If one table has three rows and the second row has two rows, then the Cartesian of two table will be six. Suppose, consider the above created employee table and another table dept whose structure and data is as follows: +++ |deptno|dname| +++ |10|IT| |20|ADMIN| +++

Cartesian product or cross join is formed, when the joining condition is omitted or the specified condition is invalid or by explicitly specifying the CROSS JOIN clause. It is mainly used for testing purpose where we need a table with huge amount of records but practically, we don't have.

mysql>SELECT*FROMdept,employee; mysql>SELECT*FROMdeptCROSSJOINemployee; +++++++ |deptno|dname|id|first_name|last_name|manager| +++++++ |10|IT|1|Pat|Crystal|NULL| |20|ADMIN|1|Pat|Crystal|NULL| |10|IT|2|Dennis|Miller|1| |20|ADMIN|2|Dennis|Miller|1| |10|IT|3|Jacob|Smith|1| |20|ADMIN|3|Jacob|Smith|1| |10|IT|4|Allen|Hunter|2| |20|ADMIN|4|Allen|Hunter|2| |10|IT|5|Marry|Underwood|3| |20|ADMIN|5|Marry|Underwood|3| |10|IT|6|Joy|Needham|3| |20|ADMIN|6|Joy|Needham|3| +++++++

You might also like