Catalogue

Concepts SQL

Explore, learn and master the essentials, at your own pace.

147 notions
Level
147 results
DQL – Scalar functions & manipulationLevelBeginner

ABS – absolute value

ABS returns the absolute value of a number (it drops the sign).

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

no SELECT *

Avoiding SELECT * makes things clearer. You know exactly what you are returning. Less exposure when the schema changes.

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

explicit aliases

Explicit aliases make the columns clear. Prefer order_total over t. It avoids name collisions.

Discover
DQL – Reading & filteringLevelBeginner

BETWEEN / NOT BETWEEN

BETWEEN checks that a value falls inside a range (both bounds included). Works with numbers and dates.

Discover
DQL – Joins & relationshipsLevelBeginner

primary and foreign keys

A primary key identifies a row uniquely. A foreign key points at another table. orders.customer_id to customers.id, products...

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

columns listed explicitly

Listing the columns makes a query stable. It keeps useless columns out. It makes review easier.

Discover
DQL – Scalar functions & manipulationLevelBeginner

CONCAT and putting strings together

CONCAT joins several strings. In PostgreSQL you can also use the || operator.

Discover
DDL – Modelling & schemaLevelBeginner

CREATE TABLE

CREATE TABLE creates a table. You set the columns, types and constraints. It runs once.

Discover
DQL – Scalar functions & manipulationLevelBeginner

DATE_PART – pulling one component out of a date

DATE_PART pulls one component out of a date (year, month, day, hour and so on). An alternative syntax to EXTRACT.

Discover
DQL – Reading & filteringLevelBeginner

DISTINCT

DISTINCT removes duplicates. It applies to the selected columns. Careful: it can cost more in performance.

Discover
DDL – Modelling & schemaLevelBeginner

DROP TABLE

DROP TABLE removes the table. A destructive action. Best avoided in production.

Discover
DQL – Scalar functions & manipulationLevelBeginner

Rounding functions: ROUND, CEIL, FLOOR

ROUND rounds a number. CEIL rounds up. FLOOR rounds down.

Discover
DDL – Modelling & schemaLevelBeginner

FOREIGN KEY

A FOREIGN KEY links two tables. It keeps the data consistent. It blocks invalid references.

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

standardised SQL formatting

Standardised SQL is easier to read back. The same style everywhere. Less ambiguity.

Discover
DQL – Reading & filteringLevelBeginner

FROM

FROM names the source table. A query reads its data from that table first. You can then filter with WHERE.

Discover
DQL – Reading & filteringLevelBeginner

handling NULL

A NULL means "unknown or missing value". = NULL does not work. Use IS NULL or IS NOT NULL.

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

handling NULL explicitly

Handling NULLs keeps surprises out of your results. Use COALESCE where needed. Use IS NULL to filter.

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

granularity under control

Granularity = the level of detail of one row. An order is not an order line. A JOIN can change the granularity.

Discover
DQL – Scalar functions & manipulationLevelBeginner

GREATEST and LEAST

GREATEST returns the largest value out of a list of arguments. LEAST returns the smallest. They are scalar functions, unlike the MAX/MIN aggregates.

Discover
DQL – Reading & filteringLevelBeginner

IN / NOT IN

IN tests whether a value is in a list. NOT IN excludes it. Careful with NOT IN and NULL.

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

consistent indentation

Consistent indentation makes the blocks obvious. Especially useful with JOINs and CTEs. It cuts down misreadings.

Discover
DQL – Scalar functions & manipulationLevelBeginner

LENGTH

LENGTH returns the number of characters in a string.

Discover
DQL – Scalar functions & manipulationLevelBeginner

LIKE and ILIKE

LIKE filters by pattern with % (anything) and _ (a single character). ILIKE is the case-insensitive variant.

Discover
DQL – Reading & filteringLevelBeginner

LIMIT

LIMIT caps the number of rows returned. Handy for trying a query out. Often paired with ORDER BY.

Discover
DQL – Scalar functions & manipulationLevelBeginner

LTRIM, RTRIM, BTRIM

Removes spaces or characters from the start and/or end of a string.

Discover
DQL – Scalar functions & manipulationLevelBeginner

MAKE_DATE, CURRENT_TIME, CURRENT_TIMESTAMP

MAKE_DATE builds a date from its components. CURRENT_TIME returns the current time. CURRENT_TIMESTAMP returns the current date and time with time zone.

Discover
DQL – Scalar functions & manipulationLevelBeginner

MOD, POWER, SQRT, TRUNC

MOD returns the remainder of a division, POWER raises to a power, SQRT takes the square root, and TRUNC cuts a number off without rounding.

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

SQL keywords in upper case

Putting keywords in upper case makes a query easier to read. SELECT, FROM, WHERE. Identifiers stay in lower case.

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

descriptive CTE names

A CTE needs a name that describes what it does. For instance: order_totals, active_customers.

Discover
DQL – Reading & filteringLevelBeginner

logical and comparison operators

Operators compare and combine conditions. Comparison: =, <, >=. Logical: AND, OR, NOT

Discover
DQL – Reading & filteringLevelBeginner

ORDER BY

ORDER BY sorts the result. ASC = ascending. DESC = descending.

Discover
DDL – Modelling & schemaLevelBeginner

PRIMARY KEY

A PRIMARY KEY identifies a row. Unique. Never NULL.

Discover
DQL – Reading & filteringLevelBeginner

SELECT

SELECT picks the columns to display. You can show one column or several. Avoid SELECT * where you can.

Discover
DQL – Scalar functions & manipulationLevelBeginner

TRIM

TRIM removes the spaces at the start and end of a string.

Discover
DDL – Modelling & schemaLevelBeginner

data types

The type sets the stored format. VARCHAR: text. INT: whole number. DECIMAL: amount. TIMESTAMP: date and time

Discover
SQL quality – Readability, maintainability & craftLevelBeginner

one intent per CTE

One CTE = one intent. One clear step. Not a catch-all.

Discover
DQL – Scalar functions & manipulationLevelBeginner

UPPER and LOWER

UPPER puts a string in upper case, LOWER in lower case — to normalise it before comparing.

Discover
DQL – Reading & filteringLevelBeginner

WHERE

WHERE filters rows. Without WHERE you get everything. With WHERE you narrow it down to a condition.

Discover
DDL – Modelling & schemaLevelIntermediate

ALTER TABLE

ALTER TABLE changes the structure. Add a column. Add a constraint.

Discover
DQL – ArraysLevelIntermediate

ANY, ALL with arrays

ANY tests whether a value matches at least one element of an array (like IN). ALL tests whether it matches every one. @>, <@ and && are the containment operators.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

Date arithmetic and intervals

You add a duration to a date with INTERVAL. Subtracting two dates gives a number of days. AGE() works out the gap between two dates.

Discover
DQL – Aggregation & groupingLevelIntermediate

ARRAY_AGG

ARRAY_AGG aggregates the values of a group into a PostgreSQL array. Handy for gathering ids or values without flattening them into text.

Discover
DQL – ArraysLevelIntermediate

array_append, array_prepend, array_cat, array_remove

Adds, removes or concatenates elements in a PostgreSQL array.

Discover
DQL – ArraysLevelIntermediate

array_to_string, array_position, cardinality

Turns an array into a string, looks for an element, or counts the elements.

Discover
DQL – ArraysLevelIntermediate

ARRAY[], UNNEST, array_length

ARRAY[] builds an array literal. UNNEST expands an array into rows. array_length returns the length of an array for a given dimension.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

AT TIME ZONE

Converts a timestamp into a different time zone.

Discover
DQL – Joins & relationshipsLevelIntermediate

Self-join

A self-join joins a table to itself. Handy for comparing rows of the same table.

Discover
DQL – Aggregation & groupingLevelIntermediate

AVG

AVG works out an average. Skips NULLs. Often what you want on price or basket size.

Discover
DQL – Aggregation & groupingLevelIntermediate

BOOL_AND and BOOL_OR

BOOL_AND returns TRUE when every row of a group satisfies a condition. BOOL_OR returns TRUE when at least one row does.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

Working out the difference between dates

To get the gap between two dates: date1 - date2 gives days, AGE() gives a readable interval. PostgreSQL has no DATE_DIFF.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

CAST and type conversion

CAST converts a value from one type to another. PostgreSQL also supports the :: syntax (e.g. '2024-01-01'::date).

Discover
DDL – Modelling & schemaLevelIntermediate

CHECK

CHECK enforces a rule on a column. For instance: price > 0. Or: quantity > 0.

Discover
SQL quality – Readability, maintainability & craftLevelIntermediate

choosing between WHERE and HAVING

WHERE filters before aggregation, HAVING after. WHERE = raw rows. HAVING = aggregated results.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

COALESCE and NULLIF

COALESCE returns the first argument that is not NULL. NULLIF returns NULL when both arguments are equal.

Discover
DQL – Aggregation & groupingLevelIntermediate

COUNT

COUNT counts rows. COUNT(*) counts every row. COUNT(col) skips NULLs.

Discover
DDL – Modelling & schemaLevelIntermediate

CREATE INDEX and index types

Creates an index to speed queries up, with different types depending on the use case.

Discover
DCL – Security & governanceLevelIntermediate

CREATE ROLE and ALTER ROLE

Creates and configures database roles, with their attributes and memberships.

Discover
DDL – Modelling & schemaLevelIntermediate

CREATE SEQUENCE

Creates a sequential number generator, standalone or tied to a column.

Discover
DDL – Modelling & schemaLevelIntermediate

CREATE TYPE ENUM

Creates an enumerated type with a fixed list of allowed values.

Discover
DDL – Modelling & schemaLevelIntermediate

CREATE VIEW

A VIEW is a query saved as a virtual table. It simplifies complex queries and hides business logic away.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

DATE_TRUNC – truncating a date

DATE_TRUNC truncates a date to the precision you want (month, week, year and so on). Essential for aggregating over time.

Discover
DML – Writing & updatingLevelIntermediate

DELETE

DELETE removes rows. Without WHERE, you remove everything. Watch out for cascades (order_items, for instance).

Discover
DQL – Advanced queriesLevelIntermediate

EXCEPT and INTERSECT

EXCEPT returns the rows of the first SELECT that are absent from the second. INTERSECT returns the rows common to both SELECTs.

Discover
DQL – Advanced queriesLevelIntermediate

EXISTS / NOT EXISTS

EXISTS checks that a subquery returns at least one row. More efficient than IN on large tables.

Discover
Performance and tuningLevelIntermediate

EXPLAIN and EXPLAIN ANALYZE

Shows the execution plan of a query so you can spot the bottlenecks.

Discover
DQL – Aggregation & groupingLevelIntermediate

FILTER (WHERE) in aggregations

A conditional filter applied to one aggregate, leaving the other aggregates alone.

Discover
SQL quality – Readability, maintainability & craftLevelIntermediate

filters applied as early as possible

Filtering early cuts the volume of data down. WHERE before JOIN where you can. A filtered subquery before the join.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

Date functions: NOW, CURRENT_DATE, EXTRACT

NOW() returns the current date and time. CURRENT_DATE returns today's date. EXTRACT pulls one part out of a date.

Discover
DQL – Joins & relationshipsLevelIntermediate

FULL OUTER JOIN

FULL OUTER JOIN returns every row from both tables, with NULL wherever there is no match.

Discover
SQL quality – Readability, maintainability & craftLevelIntermediate

granularity under control

Granularity = the level of detail of one row. An order is not an order line. A JOIN can change the granularity.

Discover
DQL – Aggregation & groupingLevelIntermediate

GROUP BY

GROUP BY groups rows so they can be aggregated. Every non-aggregated column has to be in the GROUP BY. Each group produces one row.

Discover
DQL – Aggregation & groupingLevelIntermediate

HAVING

HAVING filters groups, after aggregation. WHERE filters rows. HAVING filters aggregated results.

Discover
Performance and tuningLevelIntermediate

Indexes and query performance

Indexing strategies to tune PostgreSQL query performance.

Discover
DQL – Joins & relationshipsLevelIntermediate

INNER JOIN

INNER JOIN keeps only the matches. If nothing matches, the row disappears. Handy for linking facts to dimensions.

Discover
DML – Writing & updatingLevelIntermediate

INSERT

INSERT adds rows to a table. It respects the constraints (NOT NULL, CHECK). Watch out for foreign keys.

Discover
DML – Writing & updatingLevelIntermediate

data integrity

Data integrity keeps the data consistent. NOT NULL, UNIQUE, CHECK and FK constraints. It keeps orphans and inconsistent values out.

Discover
SQL quality – Readability, maintainability & craftLevelIntermediate

justified joins

Every JOIN should bring in information you actually use. A pointless JOIN weighs the query down and can create duplicates.

Discover
DQL – Joins & relationshipsLevelIntermediate

LEFT JOIN

LEFT JOIN keeps every row on the left. With no match, the right-hand columns come back NULL. Perfect for finding what is missing.

Discover
DQL – Advanced queriesLevelIntermediate

simple conditional logic (CASE WHEN)

CASE WHEN returns a different value depending on conditions, right inside the SELECT.

Discover
SQL quality – Readability, maintainability & craftLevelIntermediate

keeping duplicates in check

Duplicates skew your aggregates. A JOIN can multiply rows. Check with COUNT(*) against COUNT(DISTINCT).

Discover
DQL – Aggregation & groupingLevelIntermediate

MAX

MAX returns the largest value. Works on numbers and dates. Often paired with a filter.

Discover
DQL – Aggregation & groupingLevelIntermediate

MIN

MIN returns the smallest value. Works on numbers and dates. Handy for finding a first purchase.

Discover
DQL – JSON & semi-structured dataLevelIntermediate

The -> and ->> operators

-> returns a JSON sub-document, ->> returns text. #> and #>> give access by path. Used on JSONB columns.

Discover
DQL – JSON & semi-structured dataLevelIntermediate

JSONB containment operators: @>, <@, ?

Tests whether a JSON document contains another, is contained in it, or holds certain keys.

Discover
SQL quality – Readability, maintainability & craftLevelIntermediate

guarding against division by zero

A division by zero raises an error. Use NULLIF(x, 0) or CASE WHEN. Common in ratio calculations.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

RANDOM and GENERATE_SERIES

RANDOM() produces a random number between 0 and 1. GENERATE_SERIES produces a series of numbers or dates with a given step.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

REPLACE and SPLIT_PART

REPLACE swaps every occurrence of a substring. SPLIT_PART takes the nth part of a string split on a delimiter.

Discover
DML – Writing & updatingLevelIntermediate

RETURNING

Returns the rows that were inserted, changed or removed, with no extra SELECT.

Discover
DQL – Joins & relationshipsLevelIntermediate

RIGHT JOIN

RIGHT JOIN keeps every row on the right. Usually avoidable: a LEFT JOIN the other way round does the job. Worth knowing to read existing SQL.

Discover
DQL – JSON & semi-structured dataLevelIntermediate

row_to_json and jsonb_each

Turns a row into JSON, or breaks a JSON object into key-value pairs.

Discover
DQL – Aggregation & groupingLevelIntermediate

STRING_AGG

STRING_AGG concatenates the values of a group into one string, separated by a delimiter. Supports ORDER BY inside the aggregate.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

SUBSTRING and pulling text apart

SUBSTRING extracts part of a string. LEFT/RIGHT take from the ends. STRPOS finds the position of a substring.

Discover
DQL – Aggregation & groupingLevelIntermediate

SUM

SUM adds numeric values up. Often used with GROUP BY. Mind the types and the NULLs.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

TO_CHAR – formatting a date as text

TO_CHAR formats a date (or a number) as a string, following a format mask.

Discover
DQL – Scalar functions & manipulationLevelIntermediate

TO_DATE and TO_TIMESTAMP – parsing a date

TO_DATE and TO_TIMESTAMP turn text into a date or a timestamp, following a given format.

Discover
DQL – JSON & semi-structured dataLevelIntermediate

to_jsonb, jsonb_build_object, jsonb_agg

to_jsonb turns a value into JSONB. jsonb_build_object builds a JSON object from key/value pairs. jsonb_agg aggregates rows into a JSON array.

Discover
DDL – Modelling & schemaLevelIntermediate

TRUNCATE TABLE

Empties a whole table fast, with an option to reset the sequences.

Discover
DDL – Modelling & schemaLevelIntermediate

UNIQUE

UNIQUE keeps duplicates out. Often used on email. It can span several columns.

Discover
DML – Writing & updatingLevelIntermediate

UPDATE

UPDATE changes existing rows. Without WHERE, you change the whole table. Try it with a SELECT first.

Discover
SQL quality – Readability, maintainability & craftLevelAdvanced

no duplicated logic

Duplicated logic breeds bugs. Use CTEs to factor it out. One calculation, one place.

Discover
DQL – Aggregation & groupingLevelAdvanced

conditional aggregations

A conditional aggregation aggregates according to a condition. Usually through SUM(CASE WHEN ... THEN ... END). Lets you get several metrics from one query.

Discover
DQL – Analytic functionsLevelAdvanced

Rolling aggregates: SUM() OVER, AVG() OVER

SUM, AVG, COUNT, MIN and MAX can all be used as window functions, to build running totals or moving averages over N rows.

Discover
DQL – Joins & relationshipsLevelAdvanced

anti-join

An anti-join finds the rows with no match. Usually: LEFT JOIN + WHERE ... IS NULL. Handy for spotting inactive customers.

Discover
DQL – Joins & relationshipsLevelAdvanced

cardinalities

Cardinality describes the relationship between tables. 1-N: categories to products. 1-N: orders to order_items. Understanding it keeps duplicates away.

Discover
DDL – Modelling & schemaLevelAdvanced

CREATE FUNCTION and CREATE PROCEDURE

Creates reusable stored functions and procedures inside the database.

Discover
DQL – Advanced queriesLevelAdvanced

CTE (WITH)

A CTE (WITH) is a readable temporary table. It improves clarity. It lets you break a query into steps.

Discover
DQL – Analytic functionsLevelAdvanced

DENSE_RANK

DENSE_RANK ranks with ties but no gaps. Two equal values get the same rank. The next rank does not skip.

Discover
DQL – Scalar functions & manipulationLevelAdvanced

Regular expressions: ~, REGEXP_REPLACE, REGEXP_MATCHES

~ and ~* filter with regular expressions. REGEXP_REPLACE swaps out what matches a pattern. REGEXP_MATCHES returns the captured groups.

Discover
DQL – Analytic functionsLevelAdvanced

FIRST_VALUE, LAST_VALUE, NTH_VALUE

FIRST_VALUE, LAST_VALUE and NTH_VALUE reach specific positions inside the window. Mind the default frame of LAST_VALUE.

Discover
DQL – Analytic functionsLevelAdvanced

Window frames: ROWS BETWEEN / RANGE BETWEEN

Pins down exactly which subset of rows a window function looks at.

Discover
DCL – Security & governanceLevelAdvanced

GRANT

GRANT hands out privileges. It depends on the engine (PostgreSQL and so on). It matters in production.

Discover
DQL – Joins & relationshipsLevelAdvanced

multiple joins

A multiple join chains several tables together. Use short aliases. Check the granularity, or you will get duplicates.

Discover
DQL – JSON & semi-structured dataLevelAdvanced

jsonb_array_elements, jsonb_object_keys

jsonb_array_elements expands a JSON array into rows. jsonb_object_keys lists the keys of a JSON object. Set-returning functions, very handy for exploring JSON.

Discover
DQL – JSON & semi-structured dataLevelAdvanced

jsonb_set and jsonb_insert

jsonb_set changes a value inside a JSONB document. jsonb_insert inserts into a JSONB array. The path is given as a text[] array.

Discover
DQL – Analytic functionsLevelAdvanced

LAG

LAG reads the previous value in a given order. Very handy for comparing over time. Works with OVER (PARTITION BY ... ORDER BY ...).

Discover
DQL – Analytic functionsLevelAdvanced

LEAD

LEAD reads the next value in a given order. Handy for looking ahead to the next event. Same idea as LAG, but forwards.

Discover
DQL – Advanced queriesLevelAdvanced

advanced conditional logic

Conditional logic usually goes through CASE. It lets you build categories. Handy for reporting.

Discover
DML – Writing & updatingLevelAdvanced

MERGE / UPSERT

An UPSERT inserts or updates depending on whether the row exists. Very handy on unique keys (customers.email, for instance). The syntax depends on the engine.

Discover
DDL – Modelling & schemaLevelAdvanced

normalisation

Normalisation cuts down redundancy. You split the entities apart (customers, orders, items). You link them through keys.

Discover
DQL – Analytic functionsLevelAdvanced

NTILE

NTILE(n) spreads the rows over n equal groups following a given order. Ideal for building quartiles, deciles or percentiles.

Discover
DQL – Analytic functionsLevelAdvanced

ORDER BY (windowing)

Inside a window function, ORDER BY sets the order of the computation. It is not the final sort, even though the two are related. It drives LAG, LEAD and ranking.

Discover
DQL – Analytic functionsLevelAdvanced

OVER

OVER defines the window of an analytic function. No OVER, no window function. You set the order and/or the partitioning.

Discover
DQL – Analytic functionsLevelAdvanced

PARTITION BY

PARTITION BY splits the result into groups inside a window. Each group is handled on its own. Very handy per customer_id or category_id.

Discover
DQL – Aggregation & groupingLevelAdvanced

PERCENTILE_CONT and PERCENTILE_DISC

PERCENTILE_CONT computes an interpolated percentile, PERCENTILE_DISC returns the nearest existing value. A fraction of 0.5 gives the median.

Discover
DCL – Security & governanceLevelAdvanced

permissions

Permissions control which actions are allowed. SELECT, INSERT, UPDATE, DELETE. They can be set per table.

Discover
DDL – Modelling & schemaLevelAdvanced

PRIMARY KEY / FOREIGN KEY (recap)

PK/FK define the relational model. PK = identifier. FK = link. Without them, JOINs get risky.

Discover
DCL – Security & governanceLevelAdvanced

least privilege

Least privilege = grant the bare minimum needed. It lowers the risk. It limits human error.

Discover
DQL – Analytic functionsLevelAdvanced

RANK

RANK ranks with ties, leaving gaps. Two equal values get the same rank. The next rank can skip a number.

Discover
SQL quality – Readability, maintainability & craftLevelAdvanced

a query that holds up against imperfect data

Real data is imperfect: NULLs, duplicates, outliers. Anticipate the edge cases with COALESCE and NULLIF.

Discover
DQL – Advanced queriesLevelAdvanced

nested queries

A nested query stacks levels on top of each other. Handy for: compute, then filter, then rank. A CTE is often easier to read.

Discover
DCL – Security & governanceLevelAdvanced

REVOKE

REVOKE takes privileges away. The counterpart of GRANT. Handy once an access is no longer needed.

Discover
DCL – Security & governanceLevelAdvanced

roles

A role bundles permissions together. Easier to manage. Fewer mistakes.

Discover
DML – Writing & updatingLevelAdvanced

rollback

ROLLBACK undoes the changes of an uncommitted transaction. Handy while testing. It keeps accidental changes at bay.

Discover
DCL – Security & governanceLevelAdvanced

Row Level Security (RLS)

Restricts access to the rows of a table per user, without changing the queries.

Discover
DQL – Analytic functionsLevelAdvanced

ROW_NUMBER

ROW_NUMBER numbers the rows inside a window. Handy for picking the "first" item of a group. Needs OVER(...).

Discover
DCL – Security & governanceLevelAdvanced

separating environments

Keeping dev / staging / prod apart prevents accidents. Not everyone gets write access in production. Access is controlled by role.

Discover
SQL quality – Readability, maintainability & craftLevelAdvanced

keeping the solution simple

Simplicity is a quality of its own. No subquery where a JOIN will do. The simplest code is the most maintainable.

Discover
DQL – Advanced queriesLevelAdvanced

subqueries

A subquery is a query inside another one. It can sit in WHERE, FROM or SELECT. Handy when you want to filter by an aggregate.

Discover
DQL – Aggregation & groupingLevelAdvanced

STDDEV and VARIANCE

STDDEV measures the standard deviation and VARIANCE the variance of a set of values. The _POP variants work on the whole population, the _SAMP variants on a sample.

Discover
DQL – Analytic functionsLevelAdvanced

top N per group

Top N per group means keeping the N best rows of each partition. The classic answer: ROW_NUMBER, then filter on rn <= N.

Discover
SQL quality – Readability, maintainability & craftLevelAdvanced

traceable business logic

Traceability lets a reader understand the why. Explicit CTE names. Comments about the business, not the mechanics.

Discover
DML – Writing & updatingLevelAdvanced

transactions

A transaction bundles several actions into one unit. Either all of it goes through, or none of it does. Handy for keeping data consistent.

Discover
DQL – Advanced queriesLevelAdvanced

UNION / UNION ALL

UNION combines two results and removes the duplicates. UNION ALL combines without removing them. The columns have to be compatible.

Discover
Performance and tuningLevelAdvanced

VACUUM and ANALYZE

Reclaims space and refreshes statistics to keep performance up.

Discover
DDL – Modelling & schemaLevelAdvanced

Materialized views

A MATERIALIZED VIEW physically stores the result of a query. Faster, but it needs a REFRESH to stay up to date.

Discover
DML – Writing & updatingLevelAdvanced

WITH in DML (data-modifying CTEs)

Use CTEs with INSERT/UPDATE/DELETE for complex changes that stay atomic.

Discover
DQL – Advanced queriesLevelExpert

Recursive CTE (WITH RECURSIVE)

WITH RECURSIVE allows recursive queries, handy for walking through hierarchies.

Discover