Joins: inner vs left, and why row counts change
A join combines rows from two tables on a key; an inner join keeps only rows that match on both sides, and a left join keeps every row of the left table and fills NULLs where the right table has no match.
Four customers and five orders. Asha has two orders; Chitra and Dev have none; order 104 points at a customer who does not exist.
| customer_id | name | city |
|---|---|---|
| 1 | Asha | Pune |
| 2 | Ben | Mumbai |
| 3 | Chitra | Pune |
| 4 | Dev | Delhi |
| order_id | customer_id | amount |
|---|---|---|
| 101 | 1 | 500 |
| 102 | 1 | 300 |
| 103 | 2 | 700 |
| 104 | 9 | 250 |
| 105 | NULL | 400 |
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o
ON o.customer_id
= c.customer_id;| name | order_id |
|---|---|
| Asha | 101 |
| Asha | 102 |
| Ben | 103 |
| Chitra | NULL |
| Dev | NULL |
Notice: Four customers became five rows because Asha matched twice. Orders 104 and 105 match no customer, so neither join returns them.
The trap: Filtering the right table in WHERE (WHERE o.amount > 0) after a LEFT JOIN removes the NULL rows and quietly turns it into an inner join: 3 rows, not 5. Put that condition in the ON clause to keep all customers. Asked about UNION too? UNION stacks rows and removes duplicates; UNION ALL keeps them and is faster; both need the same number of columns with compatible types.
“When outputting a left-table row for which there is no right-table match, empty (null) values are substituted for the right-table columns.”
Where it was asked (6 reported, 1 practice)
- Calculate the repeat rate of orders from a given table. Also: what is the difference between an inner join and an outer join? (Asked at Deloitte, per Glassdoor)Reported on Glassdoor · Jun 2026 · Model answer and what they listen for
- Using transactions, customers and accounts tables, find the top 3 customers by total spend in each city. (Asked at American Express in an MIS Analyst loop, per Glassdoor)Reported on Glassdoor · Model answer and what they listen for
- Solve a self-join question, then a window function question with LEAD and LAG. (Asked at Flipkart, per Glassdoor)Reported on Glassdoor · Mar 2025 · Model answer and what they listen for
- What are the types of joins, and what happens when you join two tables that contain NULLs? Can you UNION tables with different data types? (Asked at PhonePe, per AmbitionBox)Reported on AmbitionBox · Nov 2022 · Model answer and what they listen for
- Explain RANK vs DENSE_RANK, with a join question. (Asked at Swiggy in a Business Analyst loop, per AmbitionBox)Reported on AmbitionBox · Feb 2024 · Model answer and what they listen for
- Find customers who ordered in January but not in February.Practice question we wrote · Model answer and what they listen for
- Given customer orders with several dishes each, find the pair of dishes that appear together in the most orders. (Asked at Swiggy in a Data Science intern loop, per Glassdoor)Reported on Glassdoor · Jun 2026 · Model answer and what they listen for