Join in MySQL
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
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.
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;
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;
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.
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| +++++++