Databases & SQL Master Course — 030. Sales Summary Practice Lab
Lesson 030 Sales Summary Practice Lab
Deliver a one-day sales summary for fictional Maple Stationery: a store comparison, weighted amount per item, discounted-line percentage, and reconciliation evidence. Someone else must be able to reproduce the numbers without guessing your filters or denominators.
Prerequisites and objectives
Complete Lesson 029. Use familiar filtering, conditional aggregation, grouping, NULLIF, and ROUND; no joins or subqueries are needed.
Outcome: Deliver a grouped sales report with documented grain, filters, and reconciliation checks.
Document grain, inspect eligible lines, group measures, audit exclusions, and validate against hand calculations and source controls.
SQLite lab setup
The examples were executed with Python’s standard-library sqlite3 module, SQLite 3.53.1. Use a fresh SQLite database and run this complete setup once. These examples use SQLite’s arithmetic and aggregate behavior; check the corresponding rules before adapting them to another SQL dialect.
CREATE TABLE daily_sales (
line_id INTEGER PRIMARY KEY,
store TEXT NOT NULL,
category TEXT NOT NULL,
sale_day TEXT NOT NULL,
state TEXT NOT NULL,
units INTEGER NOT NULL,
unit_cents INTEGER NOT NULL,
discount_cents INTEGER
);
INSERT INTO daily_sales VALUES
(1,'North','Books','2026-09-01','complete',2,1000,200),
(2,'North','Gifts','2026-09-01','complete',1,600,0),
(3,'South','Books','2026-09-01','complete',3,1000,300),
(4,'South','Gifts','2026-09-01','complete',0,800,0),
(5,'North','Books','2026-09-02','complete',1,1000,0),
(6,'South','Gifts','2026-09-01','cancelled',2,600,0),
(7,'North','Gifts','2026-09-01','complete',1,500,NULL),
(8,'South','Gifts','2026-09-01','complete',1,700,0);
Step 1: Write the delivery specification
Input grain is one sales line, identified by line_id. Output grain is one store for sale_day equal to 2026-09-01. Dates are normalized day labels, not timestamps. Include complete lines with known discounts. Quantities are nonnegative item counts, unit_cents is cents per item, and discount_cents applies once to the whole line. Every amount is in one fictional currency. Taxes, shipping, refunds, and costs are absent.
Step 2: Define each measure before querying
Eligible line count includes a recorded zero-quantity line. Item quantity is the sum of units. Gross amount is quantity times unit price; after-discount amount subtracts the whole-line discount once. Weighted amount per item divides combined after-discount cents by combined units. Discounted-line percentage counts eligible lines with a positive discount over all eligible lines, including the zero-quantity line. This is a line-based promotion measure, not an item share or a customer conversion rate.
Step 3: Inspect the selected identities
Inspect included identities and amounts; a total alone can hide incorrect scope. Exclude the next-day, cancelled, and unknown-discount lines. Unknown discounts do not mean zero. The zero-quantity line remains eligible and contributes zero cents. Document these choices.
SELECT line_id,
units * unit_cents - discount_cents AS amount_cents
FROM daily_sales
WHERE sale_day = '2026-09-01' AND state = 'complete'
AND discount_cents IS NOT NULL
ORDER BY line_id;
Expected line/amount pairs are 1/1800, 2/600, 3/2700, 4/0, and 8/700. Their amount totals 5800 cents. Eligible identifiers let a reviewer check membership directly. The source is a sales extract, so the output describes sales measures within this definition and cannot establish profit or accounting compliance.
Exercise 1
Audit the reporting day before excluding incomplete monetary records. Count day rows, complete day rows, and complete day rows with missing discounts. Use CASE to preserve a consistent day population while defining the narrower counters.
Worked answer and explanation
SELECT COUNT(*) AS day_lines,
SUM(CASE WHEN state = 'complete' THEN 1 ELSE 0 END)
AS complete_lines,
SUM(CASE WHEN state = 'complete' AND discount_cents IS NULL
THEN 1 ELSE 0 END) AS incomplete_lines
FROM daily_sales WHERE sale_day = '2026-09-01';
The result is 7, 6, and 1. Seven lines belong to the day; six are complete; one complete line has an unknown discount. Therefore five lines support the amount report. The remaining day line is cancelled. The next-day sale never entered this audit. Report the one unresolved monetary line instead of silently presenting the eligible subset as fully valued daily activity.
Exercise 2
Produce the store report with eligible lines, units, gross cents, discount cents, after-discount cents, and weighted cents per item. Include every component needed to recalculate the overall weighted measure. Use a deterministic store order and round only the final ratio.
Worked answer and explanation
SELECT store, COUNT(*) AS lines, SUM(units) AS units,
SUM(units * unit_cents) AS gross_cents,
SUM(discount_cents) AS discounts_cents,
SUM(units * unit_cents - discount_cents) AS amount_cents,
ROUND(1.0 * SUM(units * unit_cents - discount_cents)
/ NULLIF(SUM(units),0),2) AS cents_per_item
FROM daily_sales
WHERE sale_day = '2026-09-01' AND state = 'complete'
AND discount_cents IS NOT NULL
GROUP BY store ORDER BY store;
North returns 2 lines, 3 items, 2600 gross cents, 200 discount cents, 2400 after-discount cents, and 800.0 cents per item. South returns 3, 4, 3700, 300, 3400, and 850.0. Overall weighted cents per item is 5800 divided by seven, about 828.57. The simple mean of 800 and 850 is 825 and incorrectly weights the stores equally.
Exercise 3
Add a discounted-line percentage for each store using exactly the same eligibility rule. Show the discounted-line numerator beside the eligible-line denominator. Explain why South’s zero-quantity line changes this percentage’s denominator but not its total item quantity.
Worked answer and explanation
SELECT store,
SUM(CASE WHEN discount_cents > 0 THEN 1 ELSE 0 END)
AS discounted_lines,
COUNT(*) AS eligible_lines,
ROUND(100.0 * SUM(CASE WHEN discount_cents > 0 THEN 1 ELSE 0 END)
/ NULLIF(COUNT(*),0),2) AS discounted_line_pct
FROM daily_sales
WHERE sale_day = '2026-09-01' AND state = 'complete'
AND discount_cents IS NOT NULL
GROUP BY store ORDER BY store;
North returns 1, 2, 50.0; South returns 1, 3, 33.33. Line 4 is a real eligible line without a positive discount, even though it contains zero items. Across both stores there are two discounted lines among five eligible lines, or forty percent. Combining subgroup percentages requires their line-count weights, not the item quantities used by the price measure.
Exercise 4
Create an ungrouped source control that can reconcile the store report and a separate category split. Include gross, discounts, overall amount, Books amount, and Gifts amount. Compare the components with the inspected identities rather than merely accepting a query that runs.
Worked answer and explanation
SELECT COUNT(*) AS lines, SUM(units) AS units,
SUM(units * unit_cents) AS gross_cents,
SUM(discount_cents) AS discounts_cents,
SUM(units * unit_cents - discount_cents) AS amount_cents,
SUM(CASE WHEN category = 'Books'
THEN units * unit_cents - discount_cents ELSE 0 END)
AS books_cents,
SUM(CASE WHEN category = 'Gifts'
THEN units * unit_cents - discount_cents ELSE 0 END)
AS gifts_cents
FROM daily_sales
WHERE sale_day = '2026-09-01' AND state = 'complete'
AND discount_cents IS NOT NULL;
Expected output is 5, 7, 6300, 500, 5800, 4500, and 1300. Store amounts reconcile because 2400 plus 3400 is 5800. Gross less discounts gives 6300 minus 500, also 5800. Category amounts give 4500 plus 1300, again 5800. These categories exhaust this fixture; a future new category would need coverage checks before that split remained complete.
Delivery notes and failure tests

Attach date, units, grain, eligibility, exclusions, and denominator definitions. If the recipient requests a positive-quantity-only measure, change the documented scope and rerun every control. For an absent reporting day, a grouped query returns no store rows; an ungrouped control still returns one row with zero count and NULL sums. Preserve that distinction. Equal amounts on separate line identifiers must remain separate observations.
Question 1
Which rows belong in the amount report?
- Every complete line from both dates
- Only positive-discount lines
- Complete lines on the reporting day with known discounts
- Only lines with more than one item
Answer and explanation
C. The documented scope applies all three conditions. Adding or removing quantity or discount thresholds would change the requested report and require new controls and labels.
Question 2
What is the overall weighted after-discount amount per item?
- 825 cents
- About 828.57 cents
- 5800 cents
- Forty percent
Answer and explanation
B. Divide the total 5800 cents by seven items. The 825-cent alternative averages store results equally, while 5800 is an amount total and forty percent describes a separate line-based measure.
Question 3
What belongs in South’s discounted-line denominator?
- Its three eligible lines
- Only its one discounted line
- Its four items
- Only its two positive-quantity lines
Answer and explanation
A. The specification counts lines, including its recorded zero-quantity line. Quantity and eligible line count are different populations even when both appear in the same report.
Question 4
What does the unknown discount on line 7 require?
- Assume a zero discount
- Assume a full discount
- Include only its quantity in the weighted measure
- Exclude its amount and quantity from that measure and disclose it
Answer and explanation
D. The monetary calculation is unresolved, so the weighted numerator and denominator must remain aligned. A visible incompleteness count prevents the known-valued subset from masquerading as complete coverage.
Question 5
Which evidence best supports this report’s totals?
- A convincing chart
- Alphabetically sorted stores
- Matching components, eligible identities, and a source control
- A large number of decimal places
Answer and explanation
C. Independent membership and arithmetic checks address different failure modes. Formatting and precision may aid presentation, but neither establishes that the selected population and calculations match the specification.
Summary
Deliver grain, units, eligibility, distinct denominator definitions, and reconciliation evidence beside the grouped report. Check an ordinary row, an excluded row, a zero quantity, and an empty population before sharing.
Official references
- SQLite SELECT documentation: WHERE, GROUP BY, and result ordering.
- SQLite aggregate functions: COUNT and SUM semantics.
- SQLite scalar functions: NULLIF and final display rounding.