SQL Joins
Learn to combine data from multiple tables using INNER, LEFT, RIGHT, and FULL Joins.
π€ Introduction: Combining the Data
In any real-world scenario, data is stored across multiple tables. For example, one table stores Customer Details, and another stores Order History. We use SQL JOINs to combine these tables based on a common field (like CustomerID).
A **JOIN** operation combines columns from two or more tables into a single result set based on a related column between them.
π JOIN Syntax:
SELECT Columns FROM TableA JOIN TableB ON TableA.Key = TableB.Key;
π― Topic 1: INNER JOIN (The Intersection)
The **INNER JOIN** is the most common type. It returns only the rows that have matching values in *both* tables. It ignores any records that do not have a match in the other table.
β Purpose:
To see the **overlap**βfor example, which customers *have* placed an order. Customers without orders are ignored.
π» Example: Finding Matching Records
SELECT T1.Name, T2.OrderDateFROM Customers T1INNER JOIN Orders T2ON T1.CustomerID = T2.CustomerID;
β¬
οΈ Topic 2: LEFT JOIN (Preserving the Left Table)
A **LEFT JOIN** returns all records from the left table (`Customers`), and the matching records from the right table (`Orders`). If there is no match in the right table, the columns from the right table will contain **NULL**.
This is used when you want to preserve all your primary dataβfor example, getting a list of *all customers*, regardless of whether they have placed an order yet.
π» Example: All Customers and Their Orders
SELECT T1.Name, T2.OrderIDFROM Customers T1LEFT JOIN Orders T2ON T1.CustomerID = T2.CustomerID;
β‘οΈ Topic 3: RIGHT and FULL Joins
These are similar to the LEFT JOIN, but their focus shifts:
1. RIGHT JOIN (Preserving the Right Table):
Returns all records from the right table, filling in NULL where there's no match on the left. (e.g., Finding all Orders, even if the customer ID is missing/invalid).
2. FULL OUTER JOIN (The Union):
Returns all records when there is a match in *either* the left or the right table. It's the union of all data. NULL is returned for cells without a match.
π» Example: FULL OUTER JOIN
-- List all Customer IDs and all Order IDs, showing NULLs where no match existsSELECT T1.CustomerID, T2.OrderIDFROM Customers T1FULL OUTER JOIN Orders T2ON T1.CustomerID = T2.CustomerID;
βοΈ Topic 4: JOIN vs UNION
The **UNION** operator is often confused with **JOIN**. They perform entirely different tasks:
1. JOIN:
Combines data **horizontally** (adds columns) based on a key. Requires a related column.
2. UNION:
Combines data **vertically** (adds rows) from two or more tables. Requires tables to have the same number of columns and compatible data types.
π» Example: UNION (Combining Rows)
-- Combine employee names from two different tables, removing duplicatesSELECT Name, City FROM Employees_NYUNIONSELECT Name, City FROM Employees_CA;
-- Use UNION ALL to keep duplicate names
π Module Summary
- INNER JOIN: Matching records in both tables (Intersection).
- LEFT JOIN: All records from the left table + matches from the right.
- FULL JOIN: All records from both tables (Union).
- JOIN vs UNION: JOIN adds columns (Horizontal); UNION adds rows (Vertical).
π€ Interview Q&A
Tap on the questions below to reveal the answers.