The Analyst’s Course · Quant SQL
SELECT, WHERE, ORDER BY — Your First Real Queries
The three-clause skeleton
Nearly every query you need starts as: SELECT columns FROM table WHERE condition ORDER BY sort. "Show me cheap, profitable companies, biggest ROE first" becomes: SELECT symbol, pe, roe FROM stocks WHERE pe < 15 AND roe > 0.15 ORDER BY roe DESC. Read it aloud — that is the sentence you thought. The clauses always appear in this order (SELECT → FROM → WHERE → GROUP BY → ORDER BY → LIMIT) because SQL is a grammar, not a list of tips.
Cleaning up the output
Raw output is for machines. Two upgrades make it human: aliases (rename a column with AS) and functions (ROUND, arithmetic). SELECT symbol, ROUND(roe*100, 1) AS roe_pct, ROUND(pe, 1) AS pe reads like a report. Multiplying by 100 in the display (not in the filter) keeps the decimal convention intact where it matters — filtering — while presenting humans what they expect. Note AS roe_pct: the alias is the column name downstream tools will see, including the CSV export.
Operators you will actually use
Comparison: =, <>, <, >, <=, >=. Logic: AND, OR, NOT (use parentheses freely — humans misread precedence). Ranges: BETWEEN 3 AND 12 (inclusive). Sets: IN ('XOM','CVX'). Text match: LIKE 'A%' (names starting with A). Null handling: IS NULL — crucial here, because missing fundamentals (many foreign or pre-profit names) are NULL, not zero, and pe > 15 silently excludes NULL rows. The trap to internalize: WHERE pe > 15 and WHERE NOT (pe <= 15) are NOT the same query, because of NULLs.
The null that ate the screen
A classic beginner session: "screen for P/E between 5 and 12" returns a list curiously missing half the energy sector. The cause: those companies' `pe` fields are NULL in the source (loss-making quarters make P/E undefined), and NULL comparisons evaluate to NULL — neither true nor false — so the rows drop out silently. The fix (WHERE pe BETWEEN 5 AND 12 OR pe IS NULL, with a conscious decision about which you want) is less important than the habit: always ask what your query is silently excluding. Professional data work is 20% asking and 80% discovering what the question assumed.
What you'll practise
WHERE pe > 15 excludes rows where pe is NULL. To include them you need…
3 graded checkpoints · certification exam at the end of the track
Sources
Fama & French (1992, 2015); Daniel & Moskowitz (2016); screener traps per standard practice
Learn content is for education only — not individualized financial advice, a recommendation, or a solicitation to buy or sell any security. Options involve substantial risk. Examples are simplified and historical patterns never guarantee future results.