October 19 · Why databases, then your first query
Statistics classes often begin with a tidy CSV. In practice, someone must:
That end-to-end system is the data pipeline.
Adapted from Alex Reinhart, The Data Pipeline.
A folder of copied CSV files cannot reliably be the shared source of truth.
A relational database gives us:
Adapted from Reinhart, Database Fundamentals.
You set up
Your instructor provides
Which Pennsylvania weather stations sit at the highest elevations—and which records are not usable yet?
We will build the answer one clause at a time.
This course follows and adapts Alex Reinhart’s MADS Computing course. Today’s concepts follow his SQL Basics chapter; the tooling, database, examples, and checkpoints have been updated for Fall 2026.
You should be able to:
WHERE,Our first table is stations:
| Column | Meaning | Example |
|---|---|---|
station_id |
stable station identifier | USW00094823 |
name |
station name | PITTSBURGH ASOS |
state |
two-letter state code | PA |
elevation |
meters above sea level | 366.7 |
lat, lon |
station coordinates | 40.48, -80.21 |
FROM identifies the table.SELECT identifies the columns or expressions returned.SELECT * is useful—but temporary* means every column.
Good for a first look. Less good for a saved analysis because:
Run the query. Then change it so the result contains only name, state, and elevation, in that order.
Before running your revision, predict what the first column will be.
The order of expressions in SELECT controls the order of columns in the result.
Different row order is possible unless a query includes ORDER BY.
An alias changes the output label. It does not rename the stored column.
SQL evaluates the expression for each selected row.
WHERE decides which rows survive= compares values; it does not assign a value.| Intent | SQL |
|---|---|
| equal | state = 'PA' |
| not equal | state <> 'PA' |
| compare numbers | elevation >= 500 |
| both conditions | ... AND ... |
| either condition | ... OR ... |
| one of several values | state IN ('PA', 'OH', 'WV') |
Use parentheses when AND and OR appear together.
Return the ID, name, and elevation of stations in Pennsylvania with elevation at least 500 meters.
Start here:
Compare with a neighbor before running it.
Read it in plain English: choose these columns, from stations, but keep only rows satisfying both conditions.
DISTINCT to remove repeatsWithout DISTINCT, one state code appears once for every station in that state.
DISTINCT applies to the complete selected row:
NULL means a value is missing or unknown.
Do not write elevation = NULL.
This is a data-quality decision, not merely syntax.
The query now says which records count as usable for this question.
ORDER BY makes rank meaningfulDESC: largest to smallestASC: smallest to largest; this is the defaultFirst sort by elevation. If elevations tie, sort those rows by name.
LIMIT is applied after sortingWithout ORDER BY, “the first 10” has no analytical meaning.
This query is supposed to return the ten highest-elevation Pennsylvania stations. Find and repair every problem.
Correct clause order: SELECT → FROM → WHERE → ORDER BY → LIMIT.
PostgreSQL columns have types:
name, stateelevation, lat, lonTypes prevent nonsensical operations and determine which functions are available.
Functions can appear anywhere SQL expects a value or expression.
We can now inspect the highest stations—and explain exactly which records were excluded.
Choose one project table and ask a question one table can answer.
Use:
LIMIT.Save today’s transfer query. For homework:
What you wrote in class is the first draft—not disposable practice.
Your SQL file should contain:
Participation credit requires a genuine attempt at at least two checkpoints.
Next: summarize many rows with aggregate functions and GROUP BY.
Adapted from Alex Reinhart · MADS Computing