Applied edition · 44 planned hours · Unpiloted · Human review pending. Self-review is not a credential; the full 108-hour semester remains unfinished. No browser SQL execution.
Model queries and expected outputs
Attempt first. Frozen expected tables are reference values, not live browser execution. Keep workshop and retained shop fixtures separate. NULL is not zero; no rows may be correct. Use the explained key for interpretation.
sql-applied-course/queries/u01_inventory.sql
-- Original model query; attempt workbook task before reading.
SELECT COUNT(*) AS booking_rows,COUNT(DISTINCT booking_ref) AS booking_refs,
SUM(seats) AS booked_seats FROM registrations;
| booking_rows | booking_refs | booked_seats |
|---|---|---|
| 10 | 8 | 14 |
| booking_rows | booking_refs | booked_seats |
|---|---|---|
| 5 | 4 | 7 |
| booking_rows | booking_refs | booked_seats |
|---|---|---|
| 3 | 3 | 4 |
| booking_rows | booking_refs | booked_seats |
|---|---|---|
| 0 | 0 | NULL |
sql-applied-course/queries/u02_date_window.sql
-- Original model query; attempt workbook task before reading.
SELECT session_id,starts_on FROM sessions
WHERE starts_on>='2026-09-01' AND starts_on<'2026-09-11'
ORDER BY starts_on,session_id;
| session_id | starts_on |
|---|---|
| 101 | 2026-09-03 |
| 102 | 2026-09-04 |
| 103 | 2026-09-10 |
| session_id | starts_on |
|---|---|
| No rows returned. | |
| session_id | starts_on |
|---|---|
| No rows returned. | |
| session_id | starts_on |
|---|---|
| No rows returned. | |
sql-applied-course/queries/u02_fee_display.sql
-- Original model query; attempt workbook task before reading.
SELECT reg_id,fee_cents,ROUND(fee_cents/100.0,2) AS fee_cad
FROM registrations ORDER BY fee_cents DESC,reg_id;
| reg_id | fee_cents | fee_cad |
|---|---|---|
| 1001 | 1200 | 12 |
| 1004 | 1200 | 12 |
| 1005 | 1200 | 12 |
| 1007 | 1200 | 12 |
| 1002 | 600 | 6 |
| 1003 | 600 | 6 |
| 1008 | 600 | 6 |
| 1009 | 600 | 6 |
| 1010 | 600 | 6 |
| 1006 | 0 | 0 |
| reg_id | fee_cents | fee_cad |
|---|---|---|
| 2003 | 500 | 5 |
| 2004 | 500 | 5 |
| 2001 | 250 | 2.5 |
| 2002 | 250 | 2.5 |
| 2005 | 0 | 0 |
| reg_id | fee_cents | fee_cad |
|---|---|---|
| 3003 | 500 | 5 |
| 3001 | 250 | 2.5 |
| 3002 | 250 | 2.5 |
| reg_id | fee_cents | fee_cad |
|---|---|---|
| No rows returned. | ||
sql-applied-course/queries/u02_large_bookings.sql
-- Original model query; attempt workbook task before reading.
SELECT reg_id,session_id,seats FROM registrations
WHERE seats>=2 ORDER BY seats DESC,reg_id;
| reg_id | session_id | seats |
|---|---|---|
| 1001 | 101 | 2 |
| 1004 | 102 | 2 |
| 1005 | 102 | 2 |
| 1007 | 103 | 2 |
| reg_id | session_id | seats |
|---|---|---|
| 2003 | 202 | 2 |
| 2004 | 203 | 2 |
| reg_id | session_id | seats |
|---|---|---|
| 3003 | 302 | 2 |
| reg_id | session_id | seats |
|---|---|---|
| No rows returned. | ||
sql-applied-course/queries/u02_open_sessions.sql
-- Original model query; attempt workbook task before reading.
SELECT session_id,topic,site,starts_on,capacity FROM sessions
WHERE status='open' ORDER BY starts_on,session_id;
| session_id | topic | site | starts_on | capacity |
|---|---|---|---|---|
| 101 | Repair | North | 2026-09-03 | 6 |
| 102 | Repair | South | 2026-09-04 | 4 |
| 103 | Design | North | 2026-09-10 | 5 |
| 105 | Repair | North | 2026-09-17 | 4 |
| 106 | Design | South | 2026-09-18 | 3 |
| session_id | topic | site | starts_on | capacity |
|---|---|---|---|---|
| 201 | Design | North | 2026-10-02 | 3 |
| 202 | Repair | South | 2026-10-03 | 2 |
| 203 | Repair | North | 2026-10-09 | 4 |
| session_id | topic | site | starts_on | capacity |
|---|---|---|---|---|
| 301 | Repair | North | 2026-11-01 | 4 |
| 302 | Design | South | 2026-11-02 | 2 |
| 303 | Design | North | 2026-11-03 | 3 |
| session_id | topic | site | starts_on | capacity |
|---|---|---|---|---|
| No rows returned. | ||||
sql-applied-course/queries/u03_duplicate_intake.sql
-- Original model query; attempt workbook task before reading.
SELECT reg_id,COUNT(*) AS intake_rows FROM intake
WHERE reg_id IS NOT NULL GROUP BY reg_id HAVING COUNT(*)>1 ORDER BY reg_id;
| reg_id | intake_rows |
|---|---|
| 9001 | 2 |
| reg_id | intake_rows |
|---|---|
| 9101 | 2 |
| reg_id | intake_rows |
|---|---|
| No rows returned. | |
| reg_id | intake_rows |
|---|---|
| No rows returned. | |
sql-applied-course/queries/u03_invalid_intake.sql
-- Original model query; attempt workbook task before reading.
SELECT ingest_id,reg_id,seats,attended FROM intake
WHERE reg_id IS NULL OR seats IS NULL OR seats<=0 OR attended<0 OR attended>seats
ORDER BY ingest_id;
| ingest_id | reg_id | seats | attended |
|---|---|---|---|
| 4 | NULL | 1 | NULL |
| 5 | 9003 | 0 | 0 |
| 6 | 9004 | 1 | 2 |
| ingest_id | reg_id | seats | attended |
|---|---|---|---|
| 14 | NULL | 1 | NULL |
| 15 | 9103 | 0 | 0 |
| 16 | 9104 | 1 | 2 |
| ingest_id | reg_id | seats | attended |
|---|---|---|---|
| No rows returned. | |||
| ingest_id | reg_id | seats | attended |
|---|---|---|---|
| No rows returned. | |||
sql-applied-course/queries/u03_missing_attendance.sql
-- Original model query; attempt workbook task before reading.
SELECT reg_id,session_id,seats FROM registrations
WHERE attended IS NULL ORDER BY reg_id;
| reg_id | session_id | seats |
|---|---|---|
| 1003 | 101 | 1 |
| 1007 | 103 | 2 |
| reg_id | session_id | seats |
|---|---|---|
| 2002 | 201 | 1 |
| reg_id | session_id | seats |
|---|---|---|
| 3003 | 302 | 2 |
| reg_id | session_id | seats |
|---|---|---|
| No rows returned. | ||
sql-applied-course/queries/u03_quality_counts.sql
-- Original model query; attempt workbook task before reading.
SELECT COUNT(*) AS booking_rows,COUNT(attended) AS recorded_attendance_rows,
COUNT(*)-COUNT(attended) AS unknown_attendance_rows,SUM(attended) AS recorded_attended_seats
FROM registrations;
| booking_rows | recorded_attendance_rows | unknown_attendance_rows | recorded_attended_seats |
|---|---|---|---|
| 10 | 8 | 2 | 8 |
| booking_rows | recorded_attendance_rows | unknown_attendance_rows | recorded_attended_seats |
|---|---|---|---|
| 5 | 4 | 1 | 4 |
| booking_rows | recorded_attendance_rows | unknown_attendance_rows | recorded_attended_seats |
|---|---|---|---|
| 3 | 2 | 1 | 1 |
| booking_rows | recorded_attendance_rows | unknown_attendance_rows | recorded_attended_seats |
|---|---|---|---|
| 0 | 0 | 0 | NULL |
sql-applied-course/queries/u04_empty_summary.sql
-- Original model query; attempt workbook task before reading.
SELECT COUNT(*) AS booking_rows,SUM(seats) AS booked_seats,AVG(seats) AS mean_booking_size
FROM registrations WHERE seats>100;
| booking_rows | booked_seats | mean_booking_size |
|---|---|---|
| 0 | NULL | NULL |
| booking_rows | booked_seats | mean_booking_size |
|---|---|---|
| 0 | NULL | NULL |
| booking_rows | booked_seats | mean_booking_size |
|---|---|---|
| 0 | NULL | NULL |
| booking_rows | booked_seats | mean_booking_size |
|---|---|---|
| 0 | NULL | NULL |
sql-applied-course/queries/u04_having.sql
-- Original model query; attempt workbook task before reading.
SELECT session_id,SUM(seats) AS booked_seats FROM registrations
GROUP BY session_id HAVING SUM(seats)>=4 ORDER BY session_id;
| session_id | booked_seats |
|---|---|
| 101 | 4 |
| 102 | 4 |
| session_id | booked_seats |
|---|---|
| No rows returned. | |
| session_id | booked_seats |
|---|---|
| No rows returned. | |
| session_id | booked_seats |
|---|---|
| No rows returned. | |
sql-applied-course/queries/u04_known_rates.sql
-- Original model query; attempt workbook task before reading.
SELECT session_id,SUM(attended) AS attended_seats,SUM(seats) AS known_booked_seats,
ROUND(100.0*SUM(attended)/NULLIF(SUM(seats),0),2) AS known_attendance_pct
FROM registrations WHERE attended IS NOT NULL GROUP BY session_id ORDER BY session_id;
| session_id | attended_seats | known_booked_seats | known_attendance_pct |
|---|---|---|---|
| 101 | 2 | 3 | 66.67 |
| 102 | 3 | 4 | 75 |
| 103 | 1 | 1 | 100 |
| 105 | 2 | 3 | 66.67 |
| session_id | attended_seats | known_booked_seats | known_attendance_pct |
|---|---|---|---|
| 201 | 1 | 1 | 100 |
| 202 | 0 | 2 | 0 |
| 203 | 3 | 3 | 100 |
| session_id | attended_seats | known_booked_seats | known_attendance_pct |
|---|---|---|---|
| 301 | 1 | 2 | 50 |
| session_id | attended_seats | known_booked_seats | known_attendance_pct |
|---|---|---|---|
| No rows returned. | |||
sql-applied-course/queries/u04_session_totals.sql
-- Original model query; attempt workbook task before reading.
SELECT session_id,COUNT(*) AS booking_rows,SUM(seats) AS booked_seats,
COUNT(attended) AS recorded_attendance_rows,SUM(attended) AS recorded_attended_seats
FROM registrations GROUP BY session_id ORDER BY session_id;
| session_id | booking_rows | booked_seats | recorded_attendance_rows | recorded_attended_seats |
|---|---|---|---|---|
| 101 | 3 | 4 | 2 | 2 |
| 102 | 2 | 4 | 2 | 3 |
| 103 | 2 | 3 | 1 | 1 |
| 105 | 3 | 3 | 3 | 2 |
| session_id | booking_rows | booked_seats | recorded_attendance_rows | recorded_attended_seats |
|---|---|---|---|---|
| 201 | 2 | 2 | 1 | 1 |
| 202 | 1 | 2 | 1 | 0 |
| 203 | 2 | 3 | 2 | 3 |
| session_id | booking_rows | booked_seats | recorded_attendance_rows | recorded_attended_seats |
|---|---|---|---|---|
| 301 | 2 | 2 | 2 | 1 |
| 302 | 1 | 2 | 0 | NULL |
| session_id | booking_rows | booked_seats | recorded_attendance_rows | recorded_attended_seats |
|---|---|---|---|---|
| No rows returned. | ||||
sql-applied-course/queries/u06_above_mean.sql
-- Original model query; attempt workbook task before reading.
SELECT reg_id,seats FROM registrations
WHERE seats>(SELECT AVG(seats) FROM registrations) ORDER BY seats DESC,reg_id;
| reg_id | seats |
|---|---|
| 1001 | 2 |
| 1004 | 2 |
| 1005 | 2 |
| 1007 | 2 |
| reg_id | seats |
|---|---|
| 2003 | 2 |
| 2004 | 2 |
| reg_id | seats |
|---|---|
| 3003 | 2 |
| reg_id | seats |
|---|---|
| No rows returned. | |
sql-applied-course/queries/u06_attendance_bands.sql
-- Original model query; attempt workbook task before reading.
SELECT reg_id,CASE WHEN attended IS NULL THEN 'unknown'
WHEN attended=0 THEN 'none recorded' WHEN attended<seats THEN 'partial'
ELSE 'all recorded' END AS attendance_band
FROM registrations ORDER BY reg_id;
| reg_id | attendance_band |
|---|---|
| 1001 | all recorded |
| 1002 | none recorded |
| 1003 | unknown |
| 1004 | partial |
| 1005 | all recorded |
| 1006 | all recorded |
| 1007 | unknown |
| 1008 | all recorded |
| 1009 | none recorded |
| 1010 | all recorded |
| reg_id | attendance_band |
|---|---|
| 2001 | all recorded |
| 2002 | unknown |
| 2003 | none recorded |
| 2004 | all recorded |
| 2005 | all recorded |
| reg_id | attendance_band |
|---|---|
| 3001 | all recorded |
| 3002 | none recorded |
| 3003 | unknown |
| reg_id | attendance_band |
|---|---|
| No rows returned. | |
sql-applied-course/queries/u06_orphans.sql
-- Original model query; attempt workbook task before reading.
SELECT i.ingest_id,i.reg_id,i.session_id FROM intake AS i
WHERE NOT EXISTS (SELECT 1 FROM sessions AS s WHERE s.session_id=i.session_id)
ORDER BY i.ingest_id;
| ingest_id | reg_id | session_id |
|---|---|---|
| 3 | 9002 | 999 |
| ingest_id | reg_id | session_id |
|---|---|---|
| 13 | 9102 | 999 |
| ingest_id | reg_id | session_id |
|---|---|---|
| No rows returned. | ||
| ingest_id | reg_id | session_id |
|---|---|---|
| No rows returned. | ||
sql-applied-course/queries/u06_session_report.sql
-- Original model query; attempt workbook task before reading.
WITH booked AS (
SELECT session_id,COUNT(*) AS booking_rows,SUM(seats) AS booked_seats,
SUM(CASE WHEN attended IS NULL THEN seats ELSE 0 END) AS unknown_seats,
SUM(CASE WHEN attended IS NOT NULL THEN seats ELSE 0 END) AS known_seats,
SUM(attended) AS recorded_attended_seats
FROM registrations GROUP BY session_id
)
SELECT s.session_id,s.status,s.capacity,COALESCE(b.booking_rows,0) AS booking_rows,
COALESCE(b.booked_seats,0) AS booked_seats,COALESCE(b.unknown_seats,0) AS unknown_seats,
COALESCE(b.known_seats,0) AS known_seats,b.recorded_attended_seats
FROM sessions AS s LEFT JOIN booked AS b ON b.session_id=s.session_id
ORDER BY s.session_id;
| session_id | status | capacity | booking_rows | booked_seats | unknown_seats | known_seats | recorded_attended_seats |
|---|---|---|---|---|---|---|---|
| 101 | open | 6 | 3 | 4 | 1 | 3 | 2 |
| 102 | open | 4 | 2 | 4 | 0 | 4 | 3 |
| 103 | open | 5 | 2 | 3 | 2 | 1 | 1 |
| 104 | cancelled | 0 | 0 | 0 | 0 | 0 | NULL |
| 105 | open | 4 | 3 | 3 | 0 | 3 | 2 |
| 106 | open | 3 | 0 | 0 | 0 | 0 | NULL |
| session_id | status | capacity | booking_rows | booked_seats | unknown_seats | known_seats | recorded_attended_seats |
|---|---|---|---|---|---|---|---|
| 201 | open | 3 | 2 | 2 | 1 | 1 | 1 |
| 202 | open | 2 | 1 | 2 | 0 | 2 | 0 |
| 203 | open | 4 | 2 | 3 | 0 | 3 | 3 |
| 204 | cancelled | 0 | 0 | 0 | 0 | 0 | NULL |
| session_id | status | capacity | booking_rows | booked_seats | unknown_seats | known_seats | recorded_attended_seats |
|---|---|---|---|---|---|---|---|
| 301 | open | 4 | 2 | 2 | 0 | 2 | 1 |
| 302 | open | 2 | 1 | 2 | 2 | 0 | NULL |
| 303 | open | 3 | 0 | 0 | 0 | 0 | NULL |
| session_id | status | capacity | booking_rows | booked_seats | unknown_seats | known_seats | recorded_attended_seats |
|---|---|---|---|---|---|---|---|
| No rows returned. | |||||||
sql-applied-course/queries/u07_bounds.sql
-- Original model query; attempt workbook task before reading.
WITH totals AS (
SELECT COUNT(*) AS booking_rows,COALESCE(SUM(seats),0) AS booked_seats,
COALESCE(SUM(attended),0) AS recorded_attended_seats,
COALESCE(SUM(CASE WHEN attended IS NULL THEN seats ELSE 0 END),0) AS unknown_seats,
COALESCE(SUM(CASE WHEN attended IS NOT NULL THEN seats ELSE 0 END),0) AS known_seats
FROM registrations
)
SELECT booking_rows,booked_seats,recorded_attended_seats,unknown_seats,known_seats,
ROUND(100.0*recorded_attended_seats/NULLIF(known_seats,0),2) AS known_attendance_pct,
ROUND(100.0*recorded_attended_seats/NULLIF(booked_seats,0),2) AS lower_pct,
ROUND(100.0*(recorded_attended_seats+unknown_seats)/NULLIF(booked_seats,0),2) AS upper_pct
FROM totals;
| booking_rows | booked_seats | recorded_attended_seats | unknown_seats | known_seats | known_attendance_pct | lower_pct | upper_pct |
|---|---|---|---|---|---|---|---|
| 10 | 14 | 8 | 3 | 11 | 72.73 | 57.14 | 78.57 |
| booking_rows | booked_seats | recorded_attended_seats | unknown_seats | known_seats | known_attendance_pct | lower_pct | upper_pct |
|---|---|---|---|---|---|---|---|
| 5 | 7 | 4 | 1 | 6 | 66.67 | 57.14 | 71.43 |
| booking_rows | booked_seats | recorded_attended_seats | unknown_seats | known_seats | known_attendance_pct | lower_pct | upper_pct |
|---|---|---|---|---|---|---|---|
| 3 | 4 | 1 | 2 | 2 | 50 | 25 | 75 |
| booking_rows | booked_seats | recorded_attended_seats | unknown_seats | known_seats | known_attendance_pct | lower_pct | upper_pct |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | NULL | NULL | NULL |
sql-applied-course/queries/u07_capacity.sql
-- Original model query; attempt workbook task before reading.
WITH capacity AS (
SELECT COALESCE(SUM(capacity),0) AS open_capacity FROM sessions WHERE status='open'
), booked AS (
SELECT COALESCE(SUM(r.seats),0) AS booked_open_seats FROM registrations AS r
WHERE EXISTS (SELECT 1 FROM sessions AS s WHERE s.session_id=r.session_id AND s.status='open')
)
SELECT open_capacity,booked_open_seats,
ROUND(100.0*booked_open_seats/NULLIF(open_capacity,0),2) AS booking_capacity_pct
FROM capacity CROSS JOIN booked;
| open_capacity | booked_open_seats | booking_capacity_pct |
|---|---|---|
| 22 | 14 | 63.64 |
| open_capacity | booked_open_seats | booking_capacity_pct |
|---|---|---|
| 9 | 7 | 77.78 |
| open_capacity | booked_open_seats | booking_capacity_pct |
|---|---|---|
| 9 | 4 | 44.44 |
| open_capacity | booked_open_seats | booking_capacity_pct |
|---|---|---|
| 0 | 0 | NULL |
sql-applied-course/queries/u08_controls.sql
-- Original model query; attempt workbook task before reading.
SELECT (SELECT COUNT(*) FROM sessions) AS sessions,
(SELECT COUNT(*) FROM registrations) AS booking_rows,
(SELECT COALESCE(SUM(seats),0) FROM registrations) AS booked_seats,
(SELECT COALESCE(SUM(fee_cents),0) FROM registrations) AS fee_cents,
(SELECT COUNT(*) FROM intake) AS raw_intake_rows;
| sessions | booking_rows | booked_seats | fee_cents | raw_intake_rows |
|---|---|---|---|---|
| 6 | 10 | 14 | 7800 | 6 |
| sessions | booking_rows | booked_seats | fee_cents | raw_intake_rows |
|---|---|---|---|---|
| 4 | 5 | 7 | 1500 | 6 |
| sessions | booking_rows | booked_seats | fee_cents | raw_intake_rows |
|---|---|---|---|---|
| 3 | 3 | 4 | 1000 | 0 |
| sessions | booking_rows | booked_seats | fee_cents | raw_intake_rows |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
sql-applied-course/queries/u08_fee_sanity.sql
-- Original model query; attempt workbook task before reading.
SELECT COUNT(*) AS booking_rows,SUM(fee_cents) AS fee_cents,
SUM(DISTINCT fee_cents) AS wrong_distinct_fee_cents,
ROUND(AVG(fee_cents)/100.0,2) AS mean_fee_cad FROM registrations;
| booking_rows | fee_cents | wrong_distinct_fee_cents | mean_fee_cad |
|---|---|---|---|
| 10 | 7800 | 1800 | 7.8 |
| booking_rows | fee_cents | wrong_distinct_fee_cents | mean_fee_cad |
|---|---|---|---|
| 5 | 1500 | 750 | 3 |
| booking_rows | fee_cents | wrong_distinct_fee_cents | mean_fee_cad |
|---|---|---|---|
| 3 | 1000 | 750 | 3.33 |
| booking_rows | fee_cents | wrong_distinct_fee_cents | mean_fee_cad |
|---|---|---|---|
| 0 | NULL | NULL | NULL |
sql-applied-course/queries/u08_project_session_report.sql
-- Original model query; attempt workbook task before reading.
WITH b AS (
SELECT session_id,COUNT(*) AS booking_rows,SUM(seats) AS booked_seats,
SUM(CASE WHEN attended IS NOT NULL THEN seats ELSE 0 END) AS known_seats,
SUM(CASE WHEN attended IS NULL THEN seats ELSE 0 END) AS unknown_seats,
COALESCE(SUM(attended),0) AS recorded_attended_seats
FROM registrations GROUP BY session_id
)
SELECT s.session_id,s.status,s.capacity,COALESCE(b.booking_rows,0) AS booking_rows,
COALESCE(b.booked_seats,0) AS booked_seats,COALESCE(b.known_seats,0) AS known_seats,
COALESCE(b.unknown_seats,0) AS unknown_seats,COALESCE(b.recorded_attended_seats,0) AS recorded_attended_seats,
ROUND(100.0*COALESCE(b.booked_seats,0)/NULLIF(s.capacity,0),2) AS booking_capacity_pct,
ROUND(100.0*b.recorded_attended_seats/NULLIF(b.booked_seats,0),2) AS lower_attendance_pct,
ROUND(100.0*(b.recorded_attended_seats+b.unknown_seats)/NULLIF(b.booked_seats,0),2) AS upper_attendance_pct
FROM sessions AS s LEFT JOIN b ON b.session_id=s.session_id ORDER BY s.session_id;
| session_id | status | capacity | booking_rows | booked_seats | known_seats | unknown_seats | recorded_attended_seats | booking_capacity_pct | lower_attendance_pct | upper_attendance_pct |
|---|---|---|---|---|---|---|---|---|---|---|
| 101 | open | 6 | 3 | 4 | 3 | 1 | 2 | 66.67 | 50 | 75 |
| 102 | open | 4 | 2 | 4 | 4 | 0 | 3 | 100 | 75 | 75 |
| 103 | open | 5 | 2 | 3 | 1 | 2 | 1 | 60 | 33.33 | 100 |
| 104 | cancelled | 0 | 0 | 0 | 0 | 0 | 0 | NULL | NULL | NULL |
| 105 | open | 4 | 3 | 3 | 3 | 0 | 2 | 75 | 66.67 | 66.67 |
| 106 | open | 3 | 0 | 0 | 0 | 0 | 0 | 0 | NULL | NULL |
| session_id | status | capacity | booking_rows | booked_seats | known_seats | unknown_seats | recorded_attended_seats | booking_capacity_pct | lower_attendance_pct | upper_attendance_pct |
|---|---|---|---|---|---|---|---|---|---|---|
| 201 | open | 3 | 2 | 2 | 1 | 1 | 1 | 66.67 | 50 | 100 |
| 202 | open | 2 | 1 | 2 | 2 | 0 | 0 | 100 | 0 | 0 |
| 203 | open | 4 | 2 | 3 | 3 | 0 | 3 | 75 | 100 | 100 |
| 204 | cancelled | 0 | 0 | 0 | 0 | 0 | 0 | NULL | NULL | NULL |
| session_id | status | capacity | booking_rows | booked_seats | known_seats | unknown_seats | recorded_attended_seats | booking_capacity_pct | lower_attendance_pct | upper_attendance_pct |
|---|---|---|---|---|---|---|---|---|---|---|
| 301 | open | 4 | 2 | 2 | 2 | 0 | 1 | 50 | 50 | 50 |
| 302 | open | 2 | 1 | 2 | 0 | 2 | 0 | 100 | 0 | 100 |
| 303 | open | 3 | 0 | 0 | 0 | 0 | 0 | 0 | NULL | NULL |
| session_id | status | capacity | booking_rows | booked_seats | known_seats | unknown_seats | recorded_attended_seats | booking_capacity_pct | lower_attendance_pct | upper_attendance_pct |
|---|---|---|---|---|---|---|---|---|---|---|
| No rows returned. | ||||||||||
sql-join-unit/queries/all_mean.sql
SELECT AVG(amount_cad) AS mean_cad FROM orders;
| mean_cad |
|---|
| 70 |
| mean_cad |
|---|
| 76 |
| mean_cad |
|---|
| 40 |
| mean_cad |
|---|
| 50 |
| mean_cad |
|---|
| 70 |
sql-join-unit/queries/all_value.sql
SELECT SUM(amount_cad) AS total_cad FROM orders;
| total_cad |
|---|
| 280 |
| total_cad |
|---|
| 380 |
| total_cad |
|---|
| 200 |
| total_cad |
|---|
| 150 |
| total_cad |
|---|
| 280 |
sql-join-unit/queries/customer_summary.sql
SELECT customer_id,COUNT(*) AS order_count,SUM(amount_cad) AS total_cad,
AVG(amount_cad) AS mean_cad
FROM orders GROUP BY customer_id ORDER BY customer_id;
| customer_id | order_count | total_cad | mean_cad |
|---|---|---|---|
| A | 2 | 160 | 80 |
| B | 1 | 40 | 40 |
| C | 1 | 80 | 80 |
| customer_id | order_count | total_cad | mean_cad |
|---|---|---|---|
| A | 2 | 160 | 80 |
| B | 1 | 40 | 40 |
| C | 1 | 80 | 80 |
| D | 1 | 100 | 100 |
| customer_id | order_count | total_cad | mean_cad |
|---|---|---|---|
| D | 2 | 75 | 37.5 |
| E | 1 | 50 | 50 |
| F | 1 | 0 | 0 |
| G | 1 | 75 | 75 |
| customer_id | order_count | total_cad | mean_cad |
|---|---|---|---|
| H | 1 | 30 | 30 |
| I | 1 | 90 | 90 |
| J | 1 | 30 | 30 |
| customer_id | order_count | total_cad | mean_cad |
|---|---|---|---|
| A | 2 | 160 | 80 |
| B | 1 | 40 | 40 |
| C | 1 | 80 | 80 |
sql-join-unit/queries/distinct_amount_wrong.sql
-- Deliberate mistake: equal amounts are not the same order.
SELECT SUM(DISTINCT o.amount_cad) AS wrongly_deduplicated_cad
FROM orders AS o JOIN order_items AS i ON i.order_id=o.order_id;
| wrongly_deduplicated_cad |
|---|
| 200 |
| wrongly_deduplicated_cad |
|---|
| 200 |
| wrongly_deduplicated_cad |
|---|
| 50 |
| wrongly_deduplicated_cad |
|---|
| 120 |
| wrongly_deduplicated_cad |
|---|
| 200 |
sql-join-unit/queries/inner_rows.sql
SELECT o.order_id,o.customer_id,o.amount_cad,i.item_id
FROM orders AS o INNER JOIN order_items AS i ON i.order_id=o.order_id
ORDER BY o.order_id,i.item_id;
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 101 | A | 100 | 1 |
| 101 | A | 100 | 2 |
| 102 | A | 60 | 3 |
| 103 | B | 40 | 4 |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 101 | A | 100 | 1 |
| 101 | A | 100 | 2 |
| 102 | A | 60 | 3 |
| 103 | B | 40 | 4 |
| 105 | D | 100 | 5 |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 201 | D | 50 | 11 |
| 201 | D | 50 | 12 |
| 202 | E | 50 | 13 |
| 204 | F | 0 | 14 |
| 204 | F | 0 | 15 |
| 204 | F | 0 | 16 |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 301 | H | 30 | 21 |
| 302 | I | 90 | 22 |
| 302 | I | 90 | 23 |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 101 | A | 100 | 1 |
| 101 | A | 100 | 2 |
| 102 | A | 60 | 3 |
| 103 | B | 40 | 4 |
sql-join-unit/queries/inner_wrong_mean.sql
-- Deliberate mistake: this averages joined item rows, not unique orders.
SELECT AVG(o.amount_cad) AS wrongly_weighted_mean_cad
FROM orders AS o JOIN order_items AS i ON i.order_id=o.order_id;
| wrongly_weighted_mean_cad |
|---|
| 75 |
| wrongly_weighted_mean_cad |
|---|
| 80 |
| wrongly_weighted_mean_cad |
|---|
| 25 |
| wrongly_weighted_mean_cad |
|---|
| 70 |
| wrongly_weighted_mean_cad |
|---|
| 75 |
sql-join-unit/queries/inner_wrong_value.sql
-- Deliberate mistake for diagnosis: order amount repeats for each matching item.
SELECT SUM(o.amount_cad) AS wrongly_summed_cad
FROM orders AS o INNER JOIN order_items AS i ON i.order_id=o.order_id;
| wrongly_summed_cad |
|---|
| 300 |
| wrongly_summed_cad |
|---|
| 400 |
| wrongly_summed_cad |
|---|
| 150 |
| wrongly_summed_cad |
|---|
| 210 |
| wrongly_summed_cad |
|---|
| 300 |
sql-join-unit/queries/item_mean.sql
SELECT AVG(o.amount_cad) AS mean_cad FROM orders AS o
WHERE EXISTS (SELECT 1 FROM order_items AS i WHERE i.order_id=o.order_id);
| mean_cad |
|---|
| 66.66666666666667 |
| mean_cad |
|---|
| 75 |
| mean_cad |
|---|
| 33.333333333333336 |
| mean_cad |
|---|
| 60 |
| mean_cad |
|---|
| 66.66666666666667 |
sql-join-unit/queries/item_value_distinct_keys.sql
SELECT SUM(o.amount_cad) AS total_cad
FROM orders AS o
JOIN (SELECT DISTINCT order_id FROM order_items) AS present
ON present.order_id=o.order_id;
| total_cad |
|---|
| 200 |
| total_cad |
|---|
| 300 |
| total_cad |
|---|
| 100 |
| total_cad |
|---|
| 120 |
| total_cad |
|---|
| 200 |
sql-join-unit/queries/item_value_exists.sql
SELECT SUM(o.amount_cad) AS total_cad
FROM orders AS o
WHERE EXISTS (SELECT 1 FROM order_items AS i WHERE i.order_id=o.order_id);
| total_cad |
|---|
| 200 |
| total_cad |
|---|
| 300 |
| total_cad |
|---|
| 100 |
| total_cad |
|---|
| 120 |
| total_cad |
|---|
| 200 |
sql-join-unit/queries/join_counts.sql
SELECT COUNT(*) AS joined_rows, COUNT(i.item_id) AS matched_items,
COUNT(DISTINCT o.order_id) AS distinct_orders
FROM orders AS o LEFT JOIN order_items AS i ON i.order_id=o.order_id;
| joined_rows | matched_items | distinct_orders |
|---|---|---|
| 5 | 4 | 4 |
| joined_rows | matched_items | distinct_orders |
|---|---|---|
| 6 | 5 | 5 |
| joined_rows | matched_items | distinct_orders |
|---|---|---|
| 8 | 6 | 5 |
| joined_rows | matched_items | distinct_orders |
|---|---|---|
| 4 | 3 | 3 |
| joined_rows | matched_items | distinct_orders |
|---|---|---|
| 5 | 4 | 4 |
sql-join-unit/queries/left_rows.sql
SELECT o.order_id,o.customer_id,o.amount_cad,i.item_id
FROM orders AS o LEFT JOIN order_items AS i ON i.order_id=o.order_id
ORDER BY o.order_id,i.item_id;
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 101 | A | 100 | 1 |
| 101 | A | 100 | 2 |
| 102 | A | 60 | 3 |
| 103 | B | 40 | 4 |
| 104 | C | 80 | NULL |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 101 | A | 100 | 1 |
| 101 | A | 100 | 2 |
| 102 | A | 60 | 3 |
| 103 | B | 40 | 4 |
| 104 | C | 80 | NULL |
| 105 | D | 100 | 5 |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 201 | D | 50 | 11 |
| 201 | D | 50 | 12 |
| 202 | E | 50 | 13 |
| 203 | D | 25 | NULL |
| 204 | F | 0 | 14 |
| 204 | F | 0 | 15 |
| 204 | F | 0 | 16 |
| 205 | G | 75 | NULL |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 301 | H | 30 | 21 |
| 302 | I | 90 | 22 |
| 302 | I | 90 | 23 |
| 303 | J | 30 | NULL |
| order_id | customer_id | amount_cad | item_id |
|---|---|---|---|
| 101 | A | 100 | 1 |
| 101 | A | 100 | 2 |
| 102 | A | 60 | 3 |
| 103 | B | 40 | 4 |
| 104 | C | 80 | NULL |
sql-join-unit/queries/left_wrong_value.sql
-- Deliberate mistake: preserving unmatched orders does not remove fan-out.
SELECT SUM(o.amount_cad) AS wrongly_summed_cad
FROM orders AS o LEFT JOIN order_items AS i ON i.order_id=o.order_id;
| wrongly_summed_cad |
|---|
| 380 |
| wrongly_summed_cad |
|---|
| 480 |
| wrongly_summed_cad |
|---|
| 250 |
| wrongly_summed_cad |
|---|
| 240 |
| wrongly_summed_cad |
|---|
| 380 |
sql-join-unit/queries/no_item_orders.sql
SELECT o.order_id,o.customer_id,o.amount_cad
FROM orders AS o
WHERE NOT EXISTS (SELECT 1 FROM order_items AS i WHERE i.order_id=o.order_id)
ORDER BY o.order_id;
| order_id | customer_id | amount_cad |
|---|---|---|
| 104 | C | 80 |
| order_id | customer_id | amount_cad |
|---|---|---|
| 104 | C | 80 |
| order_id | customer_id | amount_cad |
|---|---|---|
| 203 | D | 25 |
| 205 | G | 75 |
| order_id | customer_id | amount_cad |
|---|---|---|
| 303 | J | 30 |
| order_id | customer_id | amount_cad |
|---|---|---|
| 104 | C | 80 |
sql-join-unit/queries/per_order_counts.sql
SELECT o.order_id,COUNT(i.item_id) AS item_rows
FROM orders AS o LEFT JOIN order_items AS i ON i.order_id=o.order_id
GROUP BY o.order_id ORDER BY o.order_id;
| order_id | item_rows |
|---|---|
| 101 | 2 |
| 102 | 1 |
| 103 | 1 |
| 104 | 0 |
| order_id | item_rows |
|---|---|
| 101 | 2 |
| 102 | 1 |
| 103 | 1 |
| 104 | 0 |
| 105 | 1 |
| order_id | item_rows |
|---|---|
| 201 | 2 |
| 202 | 1 |
| 203 | 0 |
| 204 | 3 |
| 205 | 0 |
| order_id | item_rows |
|---|---|
| 301 | 1 |
| 302 | 2 |
| 303 | 0 |
| order_id | item_rows |
|---|---|
| 101 | 2 |
| 102 | 1 |
| 103 | 1 |
| 104 | 0 |
sql-join-unit/queries/staging_orphans.sql
SELECT s.item_id,s.order_id FROM staging_items AS s
LEFT JOIN orders AS o ON o.order_id=s.order_id
WHERE o.order_id IS NULL ORDER BY s.item_id;
| item_id | order_id |
|---|---|
| 9 | 999 |