You are on page 1of 18

MySQL JOINS

What are JOINS?

 Joins help retrieving data from two or more database tables.


 The tables are mutually related using primary and foreign keys.
MySQL Cross Join

 Cross JOIN is a simplest form of JOINs which matches each row from one database table to all
rows of another.
 In other words it gives us combinations of each row of first table with all records in second table.

Cross Join

All rows from both tables


MySQL Cross Join

 All mentioned bellow lines will produce the same result:

SELECT * FROM table_1 CROSS JOIN table_2;


OR
SELECT table_1.id, table_1.greeting, table_2.id, table_2.question FROM table_1 CROSS JOIN table_2;
OR
SELECT table_1.id, table_1.greeting, table_2.id, table_2.question FROM table_1 , table_2;
MySQL Cross Join

table_1 table_2
id greeting id question
1 hello 1 How are you doing?

2 hi 2 Are you there?

1 1
Result

2 2
SELECT * FROM table_1 CROSS JOIN table_2;
MySQL Inner Join

 The inner JOIN is used to return rows from both tables that satisfy the given condition.
 The inner JOIN is same as JOIN clause, combining rows from two or more tables.

Inner Join

Only matching rows


MySQL Inner Join

 Both lines will produce the same result:

SELECT * FROM table_1 INNER JOIN table_2 ON table_1.id = table_2.id;


OR
SELECT * FROM table_1 JOIN table_2 ON table_1.id = table_2.id;;
MySQL Inner Join

table_1 table_2
id greeting id question
1 hello 1 How are you doing?

2 hi 2 Are you there?

SELECT * FROM table_1 INNER JOIN table_2 ON table_1.id = table_2.id;

Result
MySQL Outer Join

 MySQL does not support outer join.


MySQL Left Join

 The LEFT JOIN returns all the rows from the table on the left even if no matching rows
have been found in the table on the right.
 Where no matches have been found in the table on the right, NULL is returned

Left Join

All rows from the left table


MySQL Left Join

 Both lines will produce the same result:

SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.id = table_2.id;


OR
SELECT * FROM table_1 LEFT OUTER JOIN table_2 ON table_1.id = table_2.id;;
MySQL Left Join

table_1 table_2
id greeting id question
1 hello 1 How are you doing?

2 hi 2 Are you there?

3 hey

SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.id = table_2.id;


Result
MySQL Right Join

 The RIGHT JOIN returns all the columns from the table on the right even if no matching
rows have been found in the table on the left.
 Where no matches have been found in the table on the left, NULL is returned.

Right Join

All rows from the right table


MySQL Right Join

 Both lines will produce the same result:

SELECT * FROM table_1 RIGHT JOIN table_2 ON table_1.id = table_2.id;


OR
SELECT * FROM table_1 RIGHT OUTER JOIN table_2 ON table_1.id = table_2.id;;
MySQL Right Join

table_1 table_2
id greeting id question
1 hello 1 How are you doing?

2 hi 2 Are you there?


3 Are you ready?

SELECT * FROM table_1 RIGHT JOIN table_2 ON table_1.id = table_2.id;


Result
MySQL Right Join with USING clause

table_1 table_2
id greeting id question
1 hello 1 How are you doing?

2 hi 2 Are you there?


3 Are you ready?

SELECT * FROM table_1 RIGHT JOIN table_2 USING(id);


Result
Why we use JOINs

 Using JOINs, you can get the work done by using only a one query with any search
parameters.
 MySQL can achieve better performance with JOINs as it can use Indexing. Simply use of single
JOIN query instead running multiple queries do reduce server overhead. Using multiple
queries instead that leads more data transfers between MySQL and applications (software).
Further it requires more data manipulations in application end also.

You might also like