Judge queries against their purpose
A query can be valid SQL and still answer the wrong question. Start with the requested output, then check the source relationships, row filters, time boundary, and grouping.
- A query must be judged against a stated purpose: the required output columns, population, period, and level of detail. State these before reviewing the SQL.
- Extracting and integrating data: Confirm each source's documented key, the matching key in the other source, and the row grain before accepting a join.
- In standard SQL, an inner JOIN keeps combinations that satisfy its ON condition. A wrong key can remove valid rows, create incorrect matches, or change the row count.
- A WHERE search condition keeps a row only when the condition is TRUE. Rows that are FALSE or UNKNOWN, including many NULL comparisons, are not returned.
- For a full period stored as timestamps, use a closed lower bound and an open upper bound. Use the first instant and the first instant after the period.
- Every selected nonaggregate expression must satisfy the query's grouping rules. A grouped result must have the requested grain, and ORDER BY is needed when a specific output order is required.
January gross sales query
Prepared by the data analyst; the query does not tie to its stated purpose.
| SQL line | Prepared query |
|---|---|
| 1 | SELECT c.customer_id, |
| 2 | c.customer_name, |
| 3 | SUM(oi.quantity * oi.unit_price) AS gross_sales |
| 4 | FROM customers c |
| 5 | JOIN orders o ON o.customer_id = c.customer_id |
| 6 | JOIN order_items oi ON oi.order_id = c.customer_id |
| 7 | WHERE o.order_ts >= TIMESTAMP '2025-01-01 00:00:00' |
| 8 | AND o.order_ts <= TIMESTAMP '2025-01-31 00:00:00' |
| 9 | GROUP BY c.customer_id, c.customer_name |
| 10 | ORDER BY gross_sales DESC; |
| Source | Facts |
|---|---|
| Purpose | Show January 2025 completed gross sales by customer, including all order lines belonging to qualifying orders, with one row per customer. |
| customers | customer_id is the unique customer key. Customer 10 is Ada Lee. Customer 20 is Ben Ortiz. |
| orders | Order 1001 belongs to customer 10, has order timestamp 2025-01-01 09:00:00, and status Completed. |
| orders | Order 1002 belongs to customer 10, has order timestamp 2025-01-31 15:30:00, and status Completed. |
| orders | Order 1003 belongs to customer 20, has order timestamp 2025-01-20 12:00:00, and status Cancelled. |
| orders | Order 1004 belongs to customer 20, has order timestamp 2025-02-01 08:00:00, and status Completed. |
| order_items | Each row belongs to an order through order_items.order_id = orders.order_id. |
| order_items | Order 1001 has quantity 2 at unit price $50. |
| order_items | Order 1002 has quantity 1 at unit price $80. |
| order_items | Order 1003 has quantity 4 at unit price $25. |
| order_items | Order 1004 has quantity 3 at unit price $40. |
Wrong joins, dates and status
The wrong join key breaks the relationship between orders and their lines before any WHERE condition can help. The date and status problems then show that a query can have the right tables and still be incomplete.
| What is wrong | Where it sits | Correct figure or treatment | Rule and chapter | Error tag |
|---|---|---|---|---|
| The order-line join compares an order-line order ID with a customer ID. | Line 6: JOIN order_items oi ON oi.order_id = c.customer_id | Use JOIN order_items oi ON oi.order_id = o.order_id. | Extracting and integrating data: Match each source through the documented key relationship. Reviewing SQL queries: inspect each ON condition against the source facts. | cross-document reconciliation |
| The upper date condition stops at midnight at the start of January 31, so later January 31 timestamps are not included. | Line 8: o.order_ts <= TIMESTAMP '2025-01-31 00:00:00' | Use AND o.order_ts < TIMESTAMP '2025-02-01 00:00:00'. This includes every timestamp in January and excludes February. | Reviewing SQL queries: use the first instant after a timestamp period as the open upper bound. | rule selection |
| The query includes cancelled orders even though the purpose says completed sales. | The WHERE clause has no status condition. | Add AND o.status = 'Completed'. | Reviewing SQL queries: every population condition in the stated purpose must appear in the filtering logic. | fact extraction |
The corrected join reaches the intended order lines. The half-open date range covers January, and the status condition limits the population to completed orders.
March shipped sales by customer
This review changes the business status and month, so the purpose must be read again. The input-to-output trace also parallels Business process models and diagrams: identify the intended output, trace each input and transformation, and remove any break in that path.
Prepared by the reporting analyst; the query does not tie to its stated purpose.
| SQL line | Prepared query |
|---|---|
| Purpose | Show March 2025 shipped sales by customer, with total value and one row per customer. |
| 1 | SELECT c.customer_id, |
| 2 | c.customer_name, |
| 3 | SUM(oi.quantity * oi.unit_price) AS total_value |
| 4 | FROM customers c |
| 5 | JOIN orders o ON o.customer_id = c.customer_id |
| 6 | JOIN order_items oi ON oi.order_id = o.order_id |
| 7 | WHERE o.order_ts >= TIMESTAMP '2025-03-01 00:00:00' |
| 8 | AND o.order_ts < TIMESTAMP '2025-04-01 00:00:00' |
| 9 | GROUP BY c.customer_id |
| 10 | ORDER BY total_value DESC; |
| Source note | customer_id is the customer key. The status values include Shipped and Cancelled. Strict standard SQL grouping is required. |
Practice
The March query must return only orders whose status is Shipped. Which change supplies that condition without changing the grouping or display order?
Which key condition connects each order line to the order that contains it?
One fact changes: the period field is DATE order_day rather than TIMESTAMP order_ts, and the displayed filter is order_day >= DATE '2025-03-01' and order_day < DATE '2025-04-01'. Does the date filter need a change to include every March date?
Output, keys, grain and boundaries
- Start with the output promise; every table, join, filter, selected field, and group must support it.
- Check key fields and row grain before trusting an aggregate.
- Test the population and both period boundaries, especially when timestamps are involved.
Unlock the full lesson and continue reading
Signup to continue reading this lesson and unlimited access questions, flashcards, AI notes, and more

