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.