What Gaps in Dates does
Find missing days, months or sequence numbers. SQL syntax can vary by database, but the pattern below is a useful starting point for reports and analysis.
Syntax or pattern
calendar LEFT JOIN sales ON calendar.date = sales.date5 practical examples
Use Gaps in Dates in a sales report
Apply the Gaps in Dates pattern to a sales table.
-- Gaps in Dates example for sales
SELECT customer_id, order_date, total_amount
FROM orders
WHERE total_amount > 100;This shows how the Gaps in Dates pattern can support a simple sales analysis.
Use Gaps in Dates for customers
Apply the Gaps in Dates pattern to customer records.
-- Gaps in Dates example for customers
SELECT customer_id, email, status
FROM customers
WHERE status = 'Active';This is useful when customer records need filtering, labeling or summarizing.
Use Gaps in Dates for products
Apply the Gaps in Dates pattern to product or inventory data.
-- Gaps in Dates example for products
SELECT product_id, product_name, category
FROM products;Product tables are good practice data for this SQL pattern.
Use Gaps in Dates for monthly reporting
Apply the Gaps in Dates pattern to a monthly reporting query.
-- Gaps in Dates example for monthly reporting
SELECT DATE_TRUNC('month', order_date) AS month, SUM(total_amount) AS sales
FROM orders
GROUP BY DATE_TRUNC('month', order_date);This turns row-level transactions into a report-friendly result.
Use Gaps in Dates during data checks
Apply the Gaps in Dates pattern to find data quality issues.
-- Gaps in Dates example for data checks
SELECT customer_id, COUNT(*) AS records
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 1;This is a useful pattern for auditing data before building a report.
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.