SQL course · Fundamentals lesson
SQL Interview Fundamentals: SELECT, WHERE and Joins
Learn SELECT, WHERE and joins through a complete PostgreSQL customers and orders example, expected results and an independent check.
Original PostgreSQL examples · Learn the method, test the output, explain the result.
Core idea
A LEFT JOIN keeps the left row even when no order matches.
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
The direct answer
Start with the result you need, then select the rows.
SQL interview fundamentals begin with one question: what should one output row represent? SELECT chooses the result columns; WHERE filters rows; a join combines rows across tables using a stated relationship. Writing the clauses from memory is less valuable than predicting the resulting rows when an order is missing or a customer has several orders.
This lesson uses PostgreSQL and a tiny customers/orders dataset. You will see the data, run the query, compare it with an expected result, repair a common wrong answer, and try a new task. The broader DBMS foundations guide explains keys and relationships if those terms are new. Course access and options appear immediately below.
Course access
Choose your next step in SQL.
Review the current course options and access details here.
Heycademy Courses · Coming Soon
Master SQL with Heycademy
We’re preparing an interactive learning experience with practical exercises, skill assessments and personalized learning to help you build confidence and master new skills.
We’re putting the finishing touches on this course. Please check back for updates.
Prerequisites and terms
Know what a row and key mean.
A table stores rows. A primary key identifies one row in its table; a foreign key column can point to a related row in another table. Here customers.id identifies a customer, and orders.customer_id says who placed an order. One customer can place zero, one or many orders, so joining orders can increase the number of rows per customer.
An INNER JOIN keeps only matched pairs. A LEFT JOIN also keeps every row from the left table and fills the right-table columns with NULL when no match exists. PostgreSQL’s join documentation describes this behavior. A result with one row per order differs from a result with one row per customer; make that choice before coding.
Original worked dataset
Three customers, two orders.
| customer id | name |
|---|---|
| 1 | Ada |
| 2 | Ben |
| 3 | Cleo |
| order id | customer id | amount |
|---|---|---|
| 101 | 1 | 40 |
| 102 | 1 | 60 |
CREATE TEMP TABLE customers (id integer PRIMARY KEY, name text NOT NULL);
CREATE TEMP TABLE orders (order_id integer PRIMARY KEY,
customer_id integer NOT NULL, amount integer NOT NULL);
INSERT INTO customers VALUES (1,'Ada'),(2,'Ben'),(3,'Cleo');
INSERT INTO orders VALUES (101,1,40),(102,1,60);Ada has two orders. Ben and Cleo have none. Those facts are enough to predict the output without a database. The example intentionally leaves customers without orders so that an outer join has a visible effect.
Repeatable method
Check cardinality before choosing a join.
- Restate the question: “show each customer and each matching order, including customers with no orders.”
- Start from
customers, because those are the rows that must be retained. - Use
LEFT JOINon the customer key. - Predict Ada twice, Ben once and Cleo once. An unmatched order id must be
NULL. - Run the query and compare row count and values. Then alter the dataset to see whether the rule survives.
SELECT c.name, o.order_id, o.amount
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
ORDER BY c.id, o.order_id;| name | order id | amount |
|---|---|---|
| Ada | 101 | 40 |
| Ada | 102 | 60 |
| Ben | NULL | NULL |
| Cleo | NULL | NULL |
The two Ada rows come from her two matching orders. The Ben and Cleo rows are preserved by the left join. This is a row-level result, not yet a per-customer total.
Keep building this skill
Apply the method on fresh SQL problems.
The course options are above. Return to the synced pricing block whenever you are ready.
Common wrong method
A right-table WHERE filter can remove preserved rows.
Suppose the question changes to “show every customer and any of their orders worth at least 50.” The filter belongs in the join condition when customers with no qualifying order must remain. Placing o.amount >= 50 in WHERE removes the NULL-extended Ben and Cleo rows because the comparison is unknown, not true.
-- Keeps Ada, Ben and Cleo
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.id AND o.amount >= 50
ORDER BY c.id, o.order_id;| name | qualifying order id |
|---|---|
| Ada | 102 |
| Ben | NULL |
| Cleo | NULL |
A wrong repair is to add OR o.order_id IS NULL to a WHERE filter without thinking through customers who have only nonqualifying orders. Filtering in ON states the actual matching rule and preserves all left rows.
Try the transfer task
Can you return one row per customer?
New requirement: show every customer and the number of orders they placed. Predict Ada → 2, Ben → 0, Cleo → 0. Use the same data, then write your query before revealing the answer.
Show the query and explanation
SELECT c.name, COUNT(o.order_id) AS order_count
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.id;Expected: Ada 2, Ben 0, Cleo 0. COUNT(o.order_id) ignores the null order id on preserved rows. COUNT(*) would count each preserved customer row, producing 1 for Ben and Cleo. Grouping changes the result grain from one row per joined match to one row per customer.Transfer rule: whenever a join feeds an aggregate, count or sum the intended entity, not just the joined rows. This prepares you for the separate join and aggregation lesson.
Review checklist
Explain the output before calling it correct.
For each join task, name the preserved table, identify the join key, state one-to-one or one-to-many cardinality, and count the rows you expect on a tiny dataset. Test a customer with no orders and a customer with two orders. If your query produces the right row count by accident, change the data and check again.
Next learning step: practise WHERE versus ON with another condition, then move to grouping. The official PostgreSQL reference is useful when you need exact join semantics.
Three more small checks
Make SELECT, WHERE and joins work together.
Use the original three-customer dataset. First, return only Ada’s order ids. The row filter is on the customer name, and the output should contain 101 and 102. Second, show customers with no orders. A LEFT JOIN plus WHERE o.order_id IS NULL should return Ben and Cleo. Third, ask for orders worth at least 50 while keeping every customer. Put that condition in ON; the result is Ada–102, Ben–NULL and Cleo–NULL.
-- Check 1: Ada's order ids
SELECT o.order_id FROM customers c JOIN orders o
ON o.customer_id=c.id WHERE c.name='Ada' ORDER BY o.order_id;
-- Check 2: customers without an order
SELECT c.name FROM customers c LEFT JOIN orders o
ON o.customer_id=c.id WHERE o.order_id IS NULL ORDER BY c.id;These prompts have different output grains. The first returns order ids, so Ada appears twice. The second returns customers without a match, so each of Ben and Cleo appears once. The third retains every customer but only matches qualifying orders. Before running them, write that expected row count beside each query. If the count differs, inspect the join and filter before changing the SELECT list.
FAQ
Questions about this skill.
Does LEFT JOIN always return one row per left-table row?
No. It preserves each left row but can return several output rows when several right-table rows match.
Why does COUNT(*) return 1 for a customer with no orders here?
A LEFT JOIN still produces one preserved output row for that customer. COUNT(*) counts that row; COUNT(o.order_id) ignores its null order id.
Can I use this syntax in another SQL engine?
The relational idea is widely shared, but check your engine’s syntax and null behavior for the specific task.
Continue with the SQL course
Make this skill reliable on new data.
Return to the course options and choose the next learning step.