Use SQL to answer practical questions about fictional orders. Filter records, handle missing values, join tables, group results and use window functions, then produce a report whose totals you can reconcile.
Data & PythonTool stack
PostgreSQL
Technology marks for SQL for Data Analysis, sourced from the CC0-licensed Simple Icons project.
Select and filter rows with explicit columns and ordering.
Handle NULL values without confusing missing information with zero.
Join and aggregate at the intended row grain.
Use CTEs and window functions to explain an analytical query.
Reconcile a final report against its input records.
Course outline and revision prompts
Module 1
Read and filter records
Select columns and control row order; Filter records with explicit conditions; Treat missing amounts as unknown.
Lesson
Key terms
Revision question
Select columns and control row order
Columns, Rows, Order
What establishes a predictable order for this result?
Filter records with explicit conditions
Predicate, Combination, Selection
Which condition requires a sale to be both paid and at least 100?
Treat missing amounts as unknown
NULL, Inspection, Policy
Which predicate finds the fixture's missing amount?
Instructional figureProcess flow
Select columns and control row order: the working sequence
Trace the three stages of select columns and control row order and compare them with the example.
1Columns
Choose the fields needed for the question, rather than every available field.
then
2Rows
Each returned row represents one sales record in this fixture.
then
3Order
Sort by the unique identifier to obtain a deterministic sequence.
Takeaway: Explicit columns make a report's meaning visible. One row's meaning is its grain. ORDER BY controls the order of the displayed result. The sequence describes this example; use the code and output to check each stage.
Module 2
Join and summarise
Count records and summarise known amounts; Keep customers without orders in a SQL report; Separate row filters from group filters.
Lesson
Key terms
Revision question
Count records and summarise known amounts
Population, Coverage, Aggregate
Why do COUNT(*) and COUNT(amount) differ here?
Keep customers without orders in a SQL report
exploration, SQL, data
Which expression counts zero orders for an unmatched customer?
Separate row filters from group filters
WHERE, GROUP BY, HAVING
Where does a condition on SUM(amount) belong in this grouped query?
Instructional figureProcess flow
Count records and summarise known amounts: the working sequence
Trace the three stages of count records and summarise known amounts and compare them with the example.
1Population
Four records make up the input population.
then
2Coverage
Only three records contain a known amount.
then
3Aggregate
The sum of those known amounts is 250; the missing amount remains unresolved.
Takeaway: Counts and sums answer different questions. Aggregates over values exclude NULL inputs. A total needs a visible population and coverage. The sequence describes this example; use the code and output to check each stage.
Module 3
Explain analytical queries
Classify a record with CASE; Break an analysis into named query stages; Compare each sale with its customer total.
Lesson
Key terms
Revision question
Classify a record with CASE
Condition, CASE, Label
Why does the missing-value branch come first?
Break an analysis into named query stages
Fixture, CTE, Summary
What does the paid CTE name in this example?
Compare each sale with its customer total
Row grain, Partition, Window total
How many rows does this window query return for the four-row fixture?
Instructional figureProcess flow
Classify a record with CASE: the working sequence
Trace the three stages of classify a record with case and compare them with the example.
1Condition
The original amount remains in the result for inspection.
then
2CASE
CASE first checks NULL, then the large-sale threshold.
then
3Label
The report adds a label without changing the underlying record.
Takeaway: CASE branch order is part of the rule. Missing values need a deliberate classification. Derived labels should remain traceable to original fields. The sequence describes this example; use the code and output to check each stage.
Module 4
Validate and deliver a report
Find duplicate identifiers before reporting; Reconcile included and excluded records; Deliver a report with zero-order customers included.
Lesson
Key terms
Revision question
Find duplicate identifiers before reporting
Identifier, Grouping, Duplicate check
What does the repeated identifier check establish?
Reconcile included and excluded records
Population, Partitioned counts, Reconciliation
With disjoint, exhaustive categories, what should their counts sum to?
Deliver a report with zero-order customers included
Population, Matching rules, Coverage report
Why does Brian's displayed total of zero need the missing_amounts column?
Instructional figureProcess flow
Find duplicate identifiers before reporting: the working sequence
Trace the three stages of find duplicate identifiers before reporting and compare them with the example.
1Identifier
The input contains three rows but repeats sale identifier 1.
then
2Grouping
Grouping by the sale identifier produces a count per event key.
then
3Duplicate check
The duplicate check flags key 1 for investigation before totals are trusted.
Takeaway: Uniqueness checks depend on the correct key. Detection does not establish which record should be retained. Investigate duplicate inputs before calculating event totals. The sequence describes this example; use the code and output to check each stage.
Course practical outcome
Produce a customer order report that includes customers with no orders.
Expected output: A runnable query, fixture records, expected-result table and reconciliation note.
Tools: PostgreSQL scratch database, SQL editor, Fictional customer and order fixtures
Production steps
Define the output grain as one row per customer.
Create a small fixture with multiple orders, one unmatched customer and a pending order.
Join and count paid orders while preserving customers with no matches.
Check missing values and whether filters change the intended population.
Reconcile counts and amounts to the source fixture and document the query and limitations.
Success criteria
Every intended customer appears once.
Unmatched customers have a zero matching-order count.
Paid and pending orders are handled by an explicit rule.
Report totals agree with an independent input calculation.
Self-review
What does one report row represent?
Which rows were excluded and why?
Can I explain every difference between source totals and report totals?
Next step: Add a fourth customer and an additional order, predict the new result and rerun the reconciliation.
Glossary
SELECT
A statement that retrieves rows and computes output columns.
NULL
A marker for missing or unknown information; it is not a number or an empty string.
grain
What a single output row represents, such as one customer or one order.
join
An operation combining rows according to a matching condition.
aggregate
A function that summarises a set of input rows.
CTE
A named query introduced with WITH, used within a single statement.
window
A set of rows related to the current row over which a window function is evaluated.
reconciliation
Checking that a report's totals agree with independently calculated input totals.
References
Querying a table - PostgreSQL Global Development Group (accessed 2026-10-07)
This guide is generated from DigiLearn course material. Product versions, regulations and professional standards can change; consult the linked authoritative source before applying version-sensitive guidance.