Learn with Abdou

INTERACTIVE LECTURES

CASE & UNION: one report, many sources

Guided class activity · apply the ideas from the full SQL lessons.

Preparing for Lab 9 · After practice, try the team challenge.

Opening your saved work…

Sign in to save across devices → · Download your guest work before switching accounts.

Agree on a column contract before stacking sources

Step 5 of 8 · Database: Boreal Board Game Café

READ & UNDERSTAND

A JOIN adds related columns to a row. UNION ALL appends the rows from another SELECT. Before combining sources, write the output columns in order and say what each means. Same column count is necessary, but same meaning and compatible values are just as important.

For a small product-and-event directory, agree on label, dollars and source. The event name must land under label and its fee under dollars. SQLite may accept a swapped text/number position, producing a report that runs with misleading cells.

Aliases for the combined output come from the first SELECT. Apply one ORDER BY at the end using those output names. A source literal tells readers where the row came from; a shared numeric ID by itself does not identify a record across different tables.

Worked example
SELECT title AS label, shelf_price AS dollars, 'Game' AS source FROM games WHERE id <= 2 UNION ALL SELECT name, fee, 'Event' FROM events WHERE id <= 2 ORDER BY source, label;

Each branch returns three columns in the same order. The sample has two records from each table.

Prices and fees share dollar units here; that does not make them received revenue. The source label preserves the distinction.

Your question

Build a directory of games with id <= 3 and events with id <= 3. Return label, dollars, source (Game or Event), using UNION ALL. Order by source, then label.

Explain your evidence

Why can a query have the expected row count and still violate its column contract?

Try a query to unlock the worked-answer discussion.

Loading…
Table & column names only · ↑ ↓ choose · Tab insert · Esc dismiss

CURRENT DATABASE

Quick schema · Boreal Board Game Café

Press Esc or click outside to return to your query. PK = primary key · FK = declared foreign key.

This database has no tables yet. Create a table, then reopen the schema to see its columns.

Control Space also opens names. Enter adds a new line. Control or Command Enter runs your query.

Query results

Predict the result, then run your query.

Predict the result, then run your query.

Read-only activity · One statement per run · First 200 rows displayed · Checks compare the complete bounded result, not just this preview.

Explore this query freely in the playground ↗

Check the save status above. Account-saved work is available in My Progress. Explanations are for discussion, not automatically graded. No prerequisite locks.