Reconciling
Aggregates in SQL
Learn how to verify and match aggregate results across different query approaches, ensuring your totals, counts, and averages tell a consistent story.
What Are Aggregates?
Aggregate functions collapse many rows into a single summary value. They answer "big picture" questions about your data: how much, how many, or what's the average.
What Does Reconciling Mean?
Reconciling means comparing aggregate results from two or more queries that should represent the same underlying fact, and verifying they agree. If they don't, you have a data bug.
Why Reconciling Matters
In real applications, the same fact can be queried many ways. If a dashboard total disagrees with a detail report, stakeholders lose trust. Reconciliation catches bugs before they become business problems.
The Golden Rule of Aggregates
Formally, if Query A and Query B both aim to compute the total revenue for January:
The key insight: aggregate functions are deterministic over a fixed set of rows. If the row sets differ, the aggregates will differ.
Same Rows, Same Result
GROUP BY vs. Total Aggregate
A crucial reconciliation check: the sum of grouped subtotals must equal the ungrouped total.
SELECT SUM(amount) FROM ordersReturns one row: the grand total
SELECT region, SUM(amount) FROM orders GROUP BY regionReturns one row per region
COUNT(*) vs. COUNT(column)
| COUNT(*) | COUNT(column) | |
|---|---|---|
| Counts what? | Every row, regardless of NULLs | Only rows where column is NOT NULL |
| NULL row present? | Included | Excluded |
| Can they differ? | Yes, when NULLs exist: COUNT(*) > COUNT(col) | |
| Reconciliation risk | Overcounting if expected non-null | Undercounting if NULLs unexpected |
JOINs and Duplicate Rows
When you JOIN two tables, a one-to-many relationship multiplies rows. If you aggregate on the "one" side after joining, you'll count values multiple times.
How to Reconcile Step by Step
COUNT(*) from both. If they differ, investigate duplicate or missing rows.SUM, AVG, MIN, MAX. All should match.Example 1: Basic Reconciliation
Two analysts compute total sales for Q1. Do their queries agree?
Example 2: JOIN Inflation
An analyst joins orders to order_items and computes total order amounts. Something looks off.
Example 3: NULL Mismatch
Two queries count students. One uses COUNT(*), the other uses COUNT(email).
Drilling Down with EXCEPT
When aggregates don't match, use EXCEPT to find the exact rows that differ between two result sets.
Common Mistakes
Summary: Reconciliation Checklist
| Check | What to Compare | Common Cause of Mismatch |
|---|---|---|
| Row count | COUNT(*) from both queries | Duplicate rows from JOINs |
| Total sum | SUM(col) from both queries | 1:N JOIN inflation |
| NULL audit | COUNT(*) vs. COUNT(col) | Unexpected NULL values |
| Group rollup | SUM of grouped SUMs vs. total SUM | Missing group or filter mismatch |
| Exact rows | EXCEPT in both directions | Filter or JOIN logic errors |
Question 1 of 5
Query A returns SUM(amount) = 12,000 from the orders table. Query B joins orders to line_items (a 1:N relationship) and returns SUM(orders.amount) = 36,000. What is the most likely cause?
Question 2 of 5
COUNT(*) returns 500 and COUNT(phone) returns 480 on the same table. What can you conclude?
Question 3 of 5
You GROUP BY region and compute SUM(revenue) for each region. The sum of those subtotals is 95,000, but a direct SUM(revenue) without GROUP BY returns 100,000. What is the most likely cause?
Question 4 of 5
Which SQL technique is best for identifying the specific rows that cause a mismatch between two query results?
Question 5 of 5
An analyst joins customers to orders and computes COUNT(*). The result is higher than COUNT(*) from the customers table alone. Which of the following is NOT a possible explanation?