Skip to content

PostgreSQL dialect

Interview editors usually run real PostgreSQL. A query written from memory of MySQL, SQL Server, BigQuery or DuckDB often fails on small things: quotes, ROUND, a date function. Each error costs a minute or two, and a silent difference costs the answer.

In PostgreSQL, single quotes make a string, and double quotes make a name (an identifier). A word in double quotes is looked up as a column.

SQLTrap: what will it return?

What is wrong with this query?

Guess the result first, then run the query. Fix the query, then Check.

Output columns: count

Tables, columns and how they connect

transactions — one row = one paid (or trial) period of a subscription

  • channel — north_market / south_market (partner marketplaces) / web (own checkout) / promo (free access, price 0)
channel
north_market
north_market
north_market

"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.

SELECT COUNT(*)
FROM transactions
WHERE channel = "web";
Show answer

It fails: column "web" does not exist. In PostgreSQL, double quotes mark a column name; strings go in single quotes: channel = 'web'.

This is what PostgreSQL returns for the query as it is:

Error
column "web" does not exist
Show the fix
SELECT COUNT(*)
FROM transactions
WHERE channel = 'web';
count
740
1 row

Names without quotes are folded to lower case. Double quotes keep the case and allow spaces. A single quote inside a string is written twice:

SELECT 1 AS Total, 2 AS "Total", 'O''Brien' AS name;
totalTotalname
12O’Brien

Backticks around a name, as in MySQL and BigQuery, are not valid at all. The query fails with syntax error at or near "FROM".

CAST(x AS numeric) is standard SQL; x::numeric is the PostgreSQL short form. Both do the same. :: binds tighter than arithmetic, so 7 / 2::numeric casts only the 2, which is enough to make the division exact.

Typed literals are casts too: DATE '2026-09-01', TIMESTAMP '2026-09-01', INTERVAL '30 days'. Next to a column, a plain quoted date works; in date arithmetic it needs the type (see Filters and dates).

Numbers: integer division, ROUND and float

Section titled “Numbers: integer division, ROUND and float”
SELECT 7 / 2 AS int_div,
7 / 2::numeric AS cast_one,
ROUND(2.5) AS round_numeric,
ROUND(2.5::float8) AS round_float,
0.1::float8 + 0.2::float8 AS float_sum,
0.1 + 0.2 AS numeric_sum;
int_divcast_oneround_numericround_floatfloat_sumnumeric_sum
33.5000000000000000320.300000000000000040.3
  • 7 / 2 is 3: integer division drops the fraction. One numeric side fixes it.
  • ROUND of a numeric rounds a half away from zero: 2.5 becomes 3. ROUND of a double precision usually rounds a half to the even number: here 2.5 becomes 2.
  • A double precision cannot hold 0.1 exactly. Money belongs in numeric.
  • ROUND(x, n) with decimal places exists only for numeric. AVG of a numeric column is numeric; PERCENTILE_CONT and anything cast to float8 is not.
SQLTrap: what will it return?

It works in DuckDB. What about PostgreSQL?

Guess the result first, then run the query. Fix the query, then Check.

Output columns: avg_price

Tables, columns and how they connect

transactions — one row = one paid (or trial) period of a subscription

  • charged_usd — what the customer paid (gross)
charged_usd
0.00
0.00
0.00

"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.

SELECT ROUND(AVG(charged_usd)::float8, 2) AS avg_price
FROM transactions;
Show answer

It fails: function round(double precision, integer) does not exist. In PostgreSQL, ROUND with decimal places exists only for numeric: ROUND(x::numeric, 2). DuckDB, which runs the offline exercises folder, is more forgiving here and shows no error.

This is what PostgreSQL returns for the query as it is:

Error
function round(double precision, integer) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Show the fix
SELECT ROUND(AVG(charged_usd)::numeric, 2) AS avg_price
FROM transactions;
avg_price
13.11
1 row

Division by zero is an error in PostgreSQL: SELECT 1 / 0 fails with division by zero. Guard the divisor with NULLIF(b, 0).

  • date_trunc returns a timestamp, never a date. Cast it for a month column: CAST(date_trunc('month', ts) AS DATE).
  • There is no DATEADD, DATEDIFF or DATE_SUB. Write d + INTERVAL '30 days', and date_b - date_a for whole days. DATEADD(day, 30, DATE '2026-09-01') fails with column "day" does not exist: PostgreSQL reads day as a column name.
  • generate_series builds a calendar, one row per step:
SELECT CAST(d AS DATE) AS day
FROM generate_series(TIMESTAMP '2026-09-01', TIMESTAMP '2026-09-03', INTERVAL '1 day') AS g(d);
day
2026-09-01
2026-09-02
2026-09-03

With plain strings, generate_series('2026-09-01', '2026-09-03', INTERVAL '1 day') also runs, but it returns timestamp with time zone. With TIMESTAMP literals it returns timestamp without time zone, the same type as the columns.

Some tools exist only in PostgreSQL or in a few databases. They make a query shorter, and the interviewer may ask for the portable form.

DISTINCT ON (key) keeps the first row of each key, in the order of ORDER BY. The ORDER BY must start with the same key. The latest transaction of each product:

SELECT DISTINCT ON (product_id)
product_id, transaction_id, start_time
FROM transactions
WHERE NOT is_test
ORDER BY product_id, start_time DESC
LIMIT 3;
product_idtransaction_idstart_time
prd_01t_0000272026-09-28 06:30:16
prd_02t_0015422026-09-24 20:07:27
prd_03t_0042592026-09-25 13:07:30

The portable form is ROW_NUMBER() in a CTE and WHERE rn = 1 outside: see Top N per group.

COUNT(*) FILTER (WHERE …) works in PostgreSQL, DuckDB and SQLite. Elsewhere, write SUM(CASE WHEN … THEN 1 ELSE 0 END). See Conditional aggregation.

LIKE is case-sensitive in PostgreSQL. ILIKE ignores case. name LIKE '%LABS%' finds nothing here, while ILIKE finds two merchants:

SELECT id, name
FROM merchants
WHERE name ILIKE '%LABS%';
idname
mer_02Lumen Labs
mer_03Driftwood Labs

string_agg(x, ', ' ORDER BY x) joins the values of a group into one string, like GROUP_CONCAT in MySQL. The products of merchants with more than two products:

SELECT p.merchant_id, string_agg(p.id, ', ' ORDER BY p.id) AS products
FROM products p
GROUP BY p.merchant_id
HAVING COUNT(*) > 2
ORDER BY p.merchant_id;
merchant_idproducts
mer_02prd_02, prd_13, prd_16
mer_12prd_12, prd_15, prd_19
Written from memoryError in PostgreSQLPostgreSQL form
channel = "web"column "web" does not existchannel = 'web'
SELECT TOP 3 …syntax error at or near "3"… LIMIT 3
DATEADD(day, 30, d)column "day" does not existd + INTERVAL '30 days'
IFNULL(x, y), NVL(x, y)function ifnull(…) does not existCOALESCE(x, y)
GROUP_CONCAT(name)function group_concat(text) does not existstring_agg(name, ', ')
is_test = 1operator does not exist: boolean = integeris_test or NOT is_test
ROUND(x::float8, 2)function round(double precision, integer) does not existROUND(x::numeric, 2)

The offline exercises folder of this site runs the same tasks on DuckDB. DuckDB accepts most PostgreSQL syntax, and it also accepts some queries that PostgreSQL rejects. So a query that passes offline can still fail in the interview:

QueryDuckDBPostgreSQL
ROUND(x::float8, 2)roundserror: no round(double precision, integer)
a SELECT alias in WHERE or HAVINGworkscolumn "…" does not exist
is_test = 1worksoperator does not exist: boolean = integer
IFNULL(x, y)worksthe function does not exist
1 / 0inf by default; NULL with integer_divisiondivision by zero
7 / 23.5 by default3

The exercises folder switches DuckDB to integer division (SET integer_division = true), so 7 / 2 is 3 there too, and 1 / 0 gives NULL instead of inf. Neither is an error, so a division without NULLIF passes offline. The other differences stay. Some mistakes fail in both engines: double quotes around a string, a quoted date minus an interval, and a column missing from GROUP BY. When a query passes offline, run it once more here, on PostgreSQL.

Site content: CC BY 4.0. Code: MIT. The exercises run in your browser: PostgreSQL by PGlite, Python by Pyodide.