Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Given fact_sales(date_key, product_key, store_key, amount) and dim_product(product_key, category), dim_date(date_key, year, month), write SQL for total sales per category per month in 2026.

We join the fact table to dim_date and dim_product on their surrogate keys, filter on d.year = 2026, and group by month and category while summing the amount. The filter belongs on the date dimension, which lets the warehouse prune data, and the integer keys keep the joins cheap. The one trap is a many-to-many relationship, such as a product in several categories, which double counts unless we use a bridge table with an allocation.

Why this question is asked

This is the most common kind of SQL an analytics engineer writes, and the interviewer uses it to check three things at once: that we join facts and dimensions correctly, that we know where filters go, and that we can spot a model that silently inflates totals. In this answer, we will write the query step by step, look at sample data and the result, explain why surrogate keys and dimension filters matter for speed, and then break the query with a many-to-many relationship and fix it with a bridge table.

1A Quick Look at the Tables

The fact table fact_sales has one row per sale with three keys and a measure. Each key points into a dimension: date_key into dim_date, which stores year and month for each day, and product_key into dim_product, which stores each product's category. store_key points to a store dimension we do not need for this question. The keys are surrogate keys, plain integers generated by the warehouse, so the date 2026-01-15 might have the key 20260115.

SQL
-- dim_date dim_product
-- date_key | year | month product_key | category
-- 20260115 | 2026 | 1 1 | Books
-- 20260203 | 2026 | 2 2 | Games
-- 20251230 | 2025 | 12
-- fact_sales
-- date_key | product_key | store_key | amount
-- 20260115 | 1 | 7 | 20.00
-- 20260115 | 2 | 7 | 50.00
-- 20260203 | 1 | 9 | 30.00
-- 20251230 | 1 | 7 | 99.00

2Writing the Query

We build it in the order we would explain it. Start from the fact table, because it holds the amounts. Join dim_date to get the year and month of each sale, and dim_product to get its category. Keep only 2026. Then group by month and category and add up the amounts.

SQL
SELECT d.month,
p.category,
SUM(f.amount) AS total
FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key
JOIN dim_product p ON p.product_key = f.product_key
WHERE d.year = 2026
GROUP BY d.month, p.category
ORDER BY d.month, p.category;
-- month | category | total
-- 1 | Books | 20.00
-- 1 | Games | 50.00
-- 2 | Books | 30.00
Step throughHow the warehouse runs the query
  1. Filter dim_date to year 2026365 rows, a tiny set of date keys
  2. Scan fact_sales for those date keyspartitions for other years are skipped
  3. Join each row to dim_productinteger key lookup
  4. Group by month and category
  5. Sum amount per group and sort

5 steps in order. Play them, or step through them one at a time.

Notice that the 99.00 sale from December 2025 is gone, because the WHERE clause filters on the dimension's year. We use inner joins on purpose: every fact row should have a matching dimension row, and warehouses usually guarantee that with an unknown member row (key 0 or -1) for missing values, so no sale is dropped silently. If month alone could be ambiguous across years, we would group by year too, but the filter fixes the year here.

3Why Surrogate Keys and Dimension Filters Matter

The fact table may hold billions of rows, so what we join on matters. Integer surrogate keys are 4 or 8 bytes, compress well, and compare in a single CPU instruction, while joining on text such as a product code is larger and slower. They also decouple the warehouse from the source systems: if the shop renumbers its products or we merge two shops whose ids overlap, the warehouse keys stay the same.

Filtering on the dimension, d.year = 2026, rather than on some expression of the fact's columns also helps the engine. Many warehouses partition or cluster the fact table by date, and column stores keep minimum and maximum values per block. Modern engines evaluate the small dimension filter first and push the resulting date keys into the scan of the fact table, a technique often called dynamic partition pruning or a runtime filter. Because the date key values for 2026 fall in a narrow range (20260101 to 20261231), the engine can then skip every partition or block outside that range and read only a fraction of the table. A filter such as WHERE EXTRACT(YEAR FROM some_timestamp) = 2026 on the fact table hides the range from the engine and often forces a full scan.

4The Trap: Many-to-Many Double Counting

Suppose the business decides a product can belong to several categories, and someone models it by giving dim_product one row per product and category. Product 1 is now both Books and Gifts. The join from the fact table matches two dimension rows for each sale of product 1, so every such sale appears twice.

SQL
-- dim_product now has two rows for product 1
-- product_key | category
-- 1 | Books
-- 1 | Gifts
-- the same query, January 2026
-- month | category | total
-- 1 | Books | 20.00
-- 1 | Games | 50.00
-- 1 | Gifts | 20.00
-- grand total of the month: 90.00, but real sales were 70.00

Each category total looks correct on its own, but the grand total is inflated, and anyone who adds the rows up reports revenue that never happened. This is one of the most common silent errors in warehouses.

5Fixing It With a Bridge Table

The fix is to keep dim_product at one row per product and put the many-to-many relationship in a separate bridge table, product_category_bridge(product_key, category, weight). The weight says what share of a product's sales each category should get, and the weights for one product add up to 1.0.

SQL
-- product_category_bridge
-- product_key | category | weight
-- 1 | Books | 0.5
-- 1 | Gifts | 0.5
-- 2 | Games | 1.0
SELECT d.month, b.category, SUM(f.amount * b.weight) AS total
FROM fact_sales f
JOIN dim_date d ON d.date_key = f.date_key
JOIN product_category_bridge b ON b.product_key = f.product_key
WHERE d.year = 2026
GROUP BY d.month, b.category;
-- 1 | Books | 10.00
-- 1 | Games | 50.00
-- 1 | Gifts | 10.00 -- month total 70.00, correct

Notice what changed: the relationship is explicit, so anyone using the bridge sees that a product can appear in several categories, and the weights make the allocated totals add up to real sales. When the business wants full, unallocated amounts per category, which is a valid question too, we can sum without the weight, but then the report must state that categories overlap and must not show a grand total.

6Follow-up questions

Why not put the year filter in the JOIN condition instead of WHERE?

For an inner join the result is the same, and the optimizer usually produces the same plan. Putting it in WHERE is clearer to readers. It matters only with outer joins, where a condition in the ON clause keeps unmatched rows that a WHERE condition would remove.

How would you include months with no sales?

Start from the dimensions instead of the fact table: cross join the 2026 months from dim_date with the categories, then left join the aggregated sales and use COALESCE(total, 0). The fact table alone can only return groups that have rows.

How can we detect double counting in an existing model?

Compare the grand total of the report with SUM(amount) directly from the fact table for the same filter. If the report is larger, some join multiplies rows. Checking that every dimension key is unique, with a uniqueness test in the transformation tool, catches it before reports are built.

7Conclusion

In this answer, we wrote the monthly sales by category query by joining fact_sales to dim_date and dim_product on surrogate keys, filtering on the date dimension's year and grouping by month and category. We saw why integer keys keep joins cheap and decouple the warehouse from source ids, why a filter on the dimension lets partitioning and block statistics skip data, and how a many-to-many relationship double counts unless we model it with a bridge table and weights.

What to say in the interviewAbout 80 seconds aloud

Open with

I'd start from the fact table, because that's where the amounts are, and join the two dimensions on their keys: SELECT d.month, p.category, SUM(f.amount) AS total FROM fact_sales f JOIN dim_date d ON d.date_key = f.date_key JOIN dim_product p ON p.product_key = f.product_key WHERE d.year = 2026 GROUP BY d.month, p.category ORDER BY d.month, p.category.

Then, if they want more

A few things I'd point out. The keys are integer surrogate keys, so the joins are cheap, and the warehouse isn't tied to the source system's ids. The filter is on the date dimension's year, which lets the engine prune partitions or blocks of the fact table by date key, instead of scanning everything.

Inner joins are fine as long as the model has an unknown member for missing references, otherwise rows could drop. And the main thing I'd check is that each dimension key is unique.

If a product could belong to several categories and someone stored that as two rows in dim_product, every sale of that product would be counted twice and the grand total would be inflated. The right model is a bridge table between product and category, with weights that add up to one, so allocated totals still match real sales.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Given fact_sales(date_key, product_key, store_key, amount) and dim_product(product_key...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/6dc43c3180/