Scenario
Imagine your online store now has two tables: Products Table:| product_id | product_name | category | price | stock |
|---|---|---|---|---|
| 1 | Wireless Mouse | Electronics | 1200 | 25 |
| 2 | Yoga Mat | Fitness | 800 | 50 |
| 3 | Bluetooth Speaker | Electronics | 2500 | 15 |
| 4 | Water Bottle | Fitness | 300 | 100 |
| 5 | Smartwatch | Electronics | 5000 | 10 |
| order_id | product_id | order_date | quantity |
|---|---|---|---|
| 101 | 1 | 2025-01-01 | 2 |
| 102 | 2 | 2025-01-02 | 5 |
| 103 | 3 | 2025-01-02 | 1 |
| 104 | 5 | 2025-01-03 | 2 |
| 105 | 4 | 2025-01-03 | 10 |
What is a Join?
A join connects two or more tables by matching rows using a common column, typically a primary key in one table and a foreign key in the other.
Types of Joins
SQL Server supports several types of joins:- INNER JOIN: Returns rows with matching values in both tables.
- LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and matching rows from the right table. Missing matches are filled with
NULL. - RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table and matching rows from the left table. Missing matches are filled with
NULL. - FULL JOIN (or FULL OUTER JOIN): Returns all rows when there’s a match in either table. Missing values are filled with
NULL. - CROSS JOIN: Returns the Cartesian product of both tables (every combination of rows).
INNER JOIN: Matching Rows Only
Suppose you want to find out the products that have been ordered along with the quantity.| product_name | quantity | order_date |
|---|---|---|
| Wireless Mouse | 2 | 2025-01-01 |
| Yoga Mat | 5 | 2025-01-02 |
| Bluetooth Speaker | 1 | 2025-01-02 |
| Smartwatch | 2 | 2025-01-03 |
| Water Bottle | 10 | 2025-01-03 |
product_id values in both tables are returned.
LEFT JOIN: Include All Rows from the Left Table
What if you want to see all products, even those that haven’t been ordered? Use a LEFT JOIN.| product_name | quantity | order_date |
|---|---|---|
| Wireless Mouse | 2 | 2025-01-01 |
| Yoga Mat | 5 | 2025-01-02 |
| Bluetooth Speaker | 1 | 2025-01-02 |
| Water Bottle | 10 | 2025-01-03 |
| Smartwatch | 2 | 2025-01-03 |
products even if there’s no matching product_id in orders.
RIGHT JOIN: Include All Rows from the Right Table
Similarly, if you want to see all orders, even if the products are missing in theproducts table, use a RIGHT JOIN.
| product_name | quantity | order_date |
|---|---|---|
| Wireless Mouse | 2 | 2025-01-01 |
| Yoga Mat | 5 | 2025-01-02 |
| Bluetooth Speaker | 1 | 2025-01-02 |
| Smartwatch | 2 | 2025-01-03 |
| Water Bottle | 10 | 2025-01-03 |
FULL JOIN: All Rows from Both Tables
To get all rows from both tables, use a FULL JOIN.| product_name | quantity | order_date |
|---|---|---|
| Wireless Mouse | 2 | 2025-01-01 |
| Yoga Mat | 5 | 2025-01-02 |
| Bluetooth Speaker | 1 | 2025-01-02 |
| Smartwatch | 2 | 2025-01-03 |
| Water Bottle | 10 | 2025-01-03 |
NULL values where necessary.
CROSS JOIN: Every Combination
A CROSS JOIN produces a combination of every row in the first table with every row in the second table. We have 5 rows in products table and orders table. So we will get the result with rows.| product_name | order_date |
|---|---|
| Wireless Mouse | 2025-01-01 |
| Wireless Mouse | 2025-01-02 |
| Wireless Mouse | 2025-01-03 |
| … |