SQL for Pulling Training Data
Querying relational databases and warehouses to build datasets, since most company data lives behind SQL rather than in CSV files.
Beginner · about 8 hours · difficulty 2/5
In industry you rarely get a tidy CSV. Training data is extracted from Postgres, BigQuery, Snowflake or similar with SQL, and interviewers for data roles almost always test it.
What to know
SELECT,WHERE,ORDER BY,LIMITand filtering on dates.GROUP BYwith aggregates andHAVING.- Joins (
INNER,LEFT) and how they create or drop rows. - Common table expressions (
WITH) to keep complex queries readable. - Window functions (
ROW_NUMBER,LAG, running sums): essential for time-based features.
ML angle
Build features as of a point in time: when predicting churn on 1 March, only use events before 1 March. Getting this wrong in SQL is one of the most common sources of target leakage. DuckDB lets you run SQL directly over local Parquet files, which is great for practice.
Good enough to move on when you can write a query that produces one row per customer with aggregated activity features over the previous 30 days.
Key concepts
- SELECT, WHERE and ORDER BY
- GROUP BY and HAVING
- Joins
- CTEs
- Window functions
- DuckDB over Parquet
Mini project
Load a public e-commerce dataset (for example the Olist Brazilian E-Commerce data on Kaggle) into DuckDB and write a query producing per-customer features such as order count, total spend and days since last order.
Check yourself
How would you compute a feature like 'purchases in the 30 days before each order' without leaking future data?
Free resources
- SQLBolt interactive lessons (SQLBolt)
- Kaggle Learn: Intro to SQL (Kaggle)
- DuckDB documentation (DuckDB)