SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
Let's explore these four primary types of joins used in SQL:
An INNER JOIN
returns rows from both tables where there is a match based on the conditions specified. If there's no match, the row will not appear in the result.
Syntax:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
Example:
If you have orders
and customers
tables and want to match them on the customer ID:
SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;
A LEFT JOIN
returns all rows from the left table and the matching rows from the right table. If there's no match, the result will contain NULL for every column from the right table.
Syntax:
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
Example:
To fetch all customers and their orders (even if they haven't placed any):
SELECT customers.customer_name, orders.order_id FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id;
A RIGHT JOIN
returns all rows from the right table and the matching rows from the left table. If there's no match, the result will contain NULL for every column from the left table.
Syntax:
SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
Example:
To fetch all orders and the customers who placed them (even if a customer doesn't exist for an order):
SELECT customers.customer_name, orders.order_id FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
A FULL JOIN
returns rows when there's a match in either the left or right table records. If there's no match, the missing side will contain NULL.
Syntax:
SELECT columns FROM table1 FULL JOIN table2 ON table1.column = table2.column;
Example:
To fetch all customers and all orders, matching where possible:
SELECT customers.customer_name, orders.order_id FROM customers FULL JOIN orders ON customers.customer_id = orders.customer_id;
Note: Not all databases support all types of joins. For instance, MySQL doesn't support FULL JOIN
. Always refer to the documentation for your specific database system to understand supported features.
Left Join in SQL explained with use cases:
SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
Right Join vs Left Join in SQL differences:
-- Left Join SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column; -- Right Join SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
Full Join in SQL with practical examples:
SELECT * FROM table1 FULL JOIN table2 ON table1.column = table2.column;
Combining multiple tables using Inner Join in SQL:
SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;
Right Outer Join in SQL queries and scenarios:
SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
Full Outer Join in SQL for complete result sets:
SELECT * FROM table1 FULL JOIN table2 ON table1.column = table2.column;