Data Engineering · Module 02
SQL for data engineering
- Lessons
- 12
- Free
- 0
- Reading
- ~3 h
Your progress
0 / 12 lessons done
Sign in to trackLessons
- Join pitfalls: duplicated and missing rowsA join that adds or drops rows raises no error. The query runs and the total is quietly wrong. Every table that combines two sources, from revenue by city to the features a model trains on, depends on catching this before someone reads the number.
- NULLs and messy values in SQLEvery source a pipeline reads has missing and messy values, and SQL handles them without raising an error. The numbers come out wrong instead: a total that doesn’t add up, an average that is too high, a query that returns nothing.
- Window functions: running totals and rankingsAlmost every report a data engineer builds needs a number that depends on other rows: revenue so far this month, the change since yesterday, the top 3 products in each city. Window functions compute these in one pass, and their defaults decide whether ties quietly change the answer.
- Sessionization: grouping events into visits with SQLVisits per day, conversion per visit and time on site all come from turning clicks into visits. The SQL is short; the choices inside it, and the clicks that arrive late or twice, decide whether a growth team can trust the numbers.
- Pivoting and unpivoting data in SQLFinance asks for grids and partners send grids, but pipelines need rows. Every reporting pipeline turns one shape into the other at least twice, and that turn is where new cities, new statuses and empty cells quietly go missing.
- Dates, times and time zones in SQLEvery daily, weekly and monthly number depends on which day each row belongs to. A query that leaves the time zone to the server gives a different revenue on a different machine, and no error says so.
- Semi-structured data: querying JSON in SQLApp events, API responses and webhooks arrive as JSON whose fields change with every event type and app release. A query over them never fails when a field is renamed or missing; it returns a smaller number, and only a pipeline that checks will notice.
- Recursive queries: hierarchies and graphs in SQLProduct categories, org charts, account hierarchies and data lineage are all stored as rows that point at other rows. Reports need totals at every level, and only a recursive query, or a table built from one, can follow those pointers to any depth.
- Deduplication: keeping one row per entityRetries, resent events and repeated loads put the same order or click into a pipeline more than once. Every count and sum built on top depends on keeping exactly one row per entity, and on keeping the same row every time the job runs.
- MERGE and upserts: updating tables in placeOnce a table is too big to rebuild every night, a pipeline has to change it in place. The statement that does this is short, and nearly every way it goes wrong is silent: a rerun, a repeated key or a late old change leaves a wrong row and no error.
- Writing fast analytical queriesAnalytical queries run over months of data, inside a nightly job with a fixed budget or behind a dashboard someone waits on. The same question can read 1 TB or 20 GB depending on how the SQL is written, and the plan shows which one you wrote.
- SQL style, reviews and tests for pipelinesA SQL change raises no error when it is wrong. It runs, and a number moves. Readable queries, a second reader and tests that run before every night are how a team keeps the numbers people rely on right as more people change the SQL.
Practise with
No tools or paper guides for this module yet.
Terms this module teaches 41
Open glossaryAnti join
A join that returns the left rows that have no match on the right.
Blocking operator
A query step that can’t output any row until it has read all of its input, such as a GROUP BY, join, sort or window function.
Closure table
A table with one row for every pair of a node and one of its ancestors, so queries over a hierarchy of any depth become ordinary joins.
Common table expression (CTE)
A named query written inside a WITH clause, which later parts of the same statement can read like a table.
Conditional aggregation
An aggregate that counts or sums only the rows matching a condition, written with CASE inside the aggregate or a FILTER clause; the portable way to pivot.
Data test
A query that looks for rows breaking a rule about the data; it passes when it returns no rows.
Deterministic query
A query that returns the same result every time it runs on the same data, because nothing in it is left for the engine to choose.
Filter pushdown
Applying a query’s filter inside the scan that reads the data, so the scan can skip blocks and pass up fewer rows.
Full rebuild
Recomputing a whole derived table from its raw inputs on every run, instead of changing only the rows that changed.
Half-open range
A range that includes its start and excludes its end, so back-to-back ranges cover a timeline with no overlap and no gap.
Hard delete
Removing a row from a table outright, so no record that it existed is left in that table.
Hash key
A single key column made by hashing the fields that identify a row, used when the source gives no id of its own.
Identity stitching
Linking events recorded under different ids, such as several devices or a logged-out device, to the one person they belong to, usually after that person logs in.
Inner join
A join that returns only rows that found at least one match on the other side, one output row per matching pair.
Join fan-out
A join returning a row more than once because it matches several rows on the other side, so sums over that row grow.
JSON path
An expression that names a place inside a JSON value, starting at $ for the whole value, with .key for an object field and [n] for an array element.
Left join
A join that returns every row of the left table at least once, with the right side’s columns empty (NULL) where nothing matched.
Long format
A table layout with one row per measurement, where categories such as city or status are values in a column rather than column names.
MERGE statement
A SQL statement that joins a source of rows to a target table on a key, then updates, deletes or inserts each row depending on whether it matched.
Natural key
A key that comes from the data itself, such as the id the source app gives a record, rather than one the pipeline makes up.
Near-duplicate
Two records that mean the same thing but differ in formatting, such as case or extra spaces, so an exact comparison treats them as different.
Peer rows
Rows that a window’s ORDER BY sorts as equal; in RANGE mode the frame always includes all of a row’s peers together.
Pivot
Turning rows into columns: the values of one column become new column names, each filled by an aggregate.
Recursive CTE
A WITH RECURSIVE query that runs a starting query, then repeats a step on the rows the last round produced, until a round returns no rows.
Row group
A block of consecutive rows in a columnar table or file, stored column by column, with statistics such as min and max kept for each column.
Semi join
A join that returns each left row once if it has at least one match on the right, and never adds the right side’s columns.
Semi-structured data
Data where each record carries its own field names, such as JSON, so records in the same column can hold different fields and types.
Sentinel value
An ordinary value, such as an empty string, 'NA', 0 or 1900-01-01, that a source writes to mean the real value is missing.
Session time zone
A database connection’s time zone setting, used to display instants, cast them to dates and read times written without an offset.
Sessionization
Grouping one visitor’s events into visits, where a gap longer than a set inactivity timeout ends one visit and starts the next.
Test fixture
A small set of input rows written by hand, so the right output is known before the code under test runs.
Three-valued logic
SQL’s rule that a condition can be true, false or unknown, where any comparison with NULL is unknown and WHERE keeps only rows that are true.
Timestamp with time zone (TIMESTAMPTZ)
A SQL type that stores one exact instant, usually as time since a fixed epoch in UTC; a time zone is applied only when it is shown or cut into days.
Unnest
Turning each element of an array or list into its own row, repeating the other columns; an empty or missing array produces no rows.
Unpivot
Turning columns into rows: each chosen column becomes a row holding the column’s name and its value.
UTC offset
How far a local clock is ahead of or behind UTC at a given moment, such as +05:30; in places with daylight saving it changes during the year.
Value mapping table
A small table of raw values and the clean value each one stands for, joined in the transform so every report uses the same cleaning rules.
Wide format
A table layout with one column per category value, such as one column per city, so the categories live in the column names.
Window frame
The rows inside a window function’s partition that feed the current row’s value, set with ROWS, RANGE or GROUPS; with ORDER BY and no frame, the default runs from the partition’s first row to the current row and its peers.
Window function
A SQL function that computes a value for each row from a set of related rows, its window, and keeps every row in the result, unlike GROUP BY.
Zone map
The smallest and largest value of a column in each row group, which lets an engine skip any group a filter can’t match. Also called a min-max index.