What LEFT JOIN does
Keep all rows from the left table and matched rows from the right table. SQL syntax can vary by database, but the pattern below is a useful starting point for reports and analysis.
Syntax or pattern
SELECT * FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;5 practical examples
Customers with their orders
Keep all customers, even those with no orders.
SELECT c.customer_id, c.email, o.order_id
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id;Rows from customers remain even when no order matches.
Products with sales if any
Show products even if they have not sold.
SELECT p.product_name, SUM(oi.quantity) AS units_sold
FROM products p
LEFT JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY p.product_name;LEFT JOIN is useful when zero-activity items matter.
Find customers without orders
Filter the unmatched side after a LEFT JOIN.
SELECT c.customer_id, c.email
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;This is a common anti-join pattern.
Join optional lookup data
Keep all tickets even when priority details are missing.
SELECT t.ticket_id, t.subject, p.priority_name
FROM tickets t
LEFT JOIN priorities p ON t.priority_id = p.priority_id;Optional reference tables often need LEFT JOIN.
Avoid turning it into an inner join
Put right-table filters in the join condition when needed.
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
AND o.status = 'Completed';Filtering the right table in WHERE can accidentally remove unmatched rows.
Common mistakes to avoid
- Forgetting that SQL dialects vary across PostgreSQL, SQL Server, MySQL, BigQuery and SQLite.
- Using SELECT * in production reports when only a few columns are needed.
- Not checking join keys, duplicate rows or NULL values before trusting results.
FAQ
Will this SQL work in every database?
The idea is portable, but function names and date syntax may vary. Check your database dialect if a function is not recognized.
Should I use this in a report query?
Yes, if the pattern matches the business question and you have checked filters, joins and row counts.
Why does my result have too many rows?
The most common reasons are duplicate join keys, missing filters or grouping at the wrong level of detail.
Here are some ideas for you
Optional resources that may help if you are learning SQL, building reports, writing queries or improving your data workflow.
- SQL books for beginnersSee ideas
Practice query patterns with structured examples and exercises.
- Database design booksSee ideas
Understand tables, keys, relationships and why joins behave the way they do.
- Mechanical keyboardsSee ideas
Useful if you write queries, code and documentation for long work sessions.
- External monitorsSee ideas
View query editor, result grid and documentation side by side.
- Developer notebooksSee ideas
Sketch table relationships, query logic and report ideas before coding.
- Desk lampsSee ideas
Keep your workspace comfortable while studying or debugging queries.
Some links in this section may be affiliate links. Choose only what is useful for your own work.