To perform a SQL join, you can use the JOIN
keyword in your SQL query to combine rows from two or more tables based on a related column between them. There are four types of SQL joins: inner join, left join, right join, and full join.
Here's an example of how to perform an inner join in SQL:
SELECT * FROM table1
INNER JOIN table2 ON table1.column = table2.column;
LEFT JOIN
returns all rows from the left table, and the matched rows from the right table. If there is no match, the result is NULL
on the right side.
In this example, the INNER JOIN
clause is used to combine rows from the table1
table and the table2
table where the columns matches. The SELECT
statement specifies the columns that should be included in the result set.
Here's an example of how to perform a left join in SQL:
SELECT orders.order_id, customers.customer_name
FROM orders
LEFT JOIN customers ON orders.customer_id = customers.customer_id;
In this example, the LEFT JOIN
clause is used to return all rows from the orders
table and the matching rows from the customers
table. If there are no matching rows in the customers
table, NULL
values will be displayed.
You can also perform a right join or a full join using the RIGHT JOIN
or FULL JOIN
clause respectively. The syntax for these joins is similar to the left join.
Here's an example of how to perform a full join in SQL:
SELECT orders.order_id, customers.customer_name
FROM orders
FULL JOIN customers ON orders.customer_id = customers.customer_id;
In this example, the FULL JOIN
clause is used to return all rows from both the orders
table and the customers
table, with NULL
values displayed where there are no matching rows.