The practice database here is PostgreSQL. Job ads for analysts name Snowflake most often, then Databricks and BigQuery. The tables below lead with those after PostgreSQL. A blank cell means I am not sure enough to state the syntax, so check the docs for that engine. "n/a" means the feature does not exist there.
In an interview, say which dialect you are writing. If you are unsure of a function name, write the logic in plain SQL (CASE, window functions, CAST) and say so. That is almost always accepted.
Dates
d is a date, ts a timestamp.
| Task | PostgreSQL | Snowflake | Databricks | BigQuery |
|---|---|---|---|---|
| First day of month | date_trunc('month', ts) |
date_trunc('month', ts) |
date_trunc('month', ts) |
DATE_TRUNC(d, MONTH) or TIMESTAMP_TRUNC(ts, MONTH) |
| Add 7 days | d + 7 or ts + interval '7 days' |
DATEADD(day, 7, d) |
date_add(d, 7) |
DATE_ADD(d, INTERVAL 7 DAY) |
| Days between | d2 - d1 |
DATEDIFF(day, d1, d2) |
datediff(d2, d1) |
DATE_DIFF(d2, d1, DAY) |
| Year of a date | extract(year from d) |
extract(year from d) |
extract(year from d) |
EXTRACT(YEAR FROM d) |
| Cast to date | ts::date or CAST(ts AS date) |
ts::date or CAST(ts AS DATE) |
CAST(ts AS DATE) |
DATE(ts) or CAST(ts AS DATE) |
| Task | SQL Server | MySQL | Redshift |
|---|---|---|---|
| First day of month | DATETRUNC(month, ts) (2022 and later) or DATEFROMPARTS(YEAR(d), MONTH(d), 1) |
DATE_FORMAT(d, '%Y-%m-01') (returns text) |
date_trunc('month', ts) |
| Add 7 days | DATEADD(day, 7, d) |
DATE_ADD(d, INTERVAL 7 DAY) |
DATEADD(day, 7, d) |
| Days between | DATEDIFF(day, d1, d2) |
DATEDIFF(d2, d1) |
DATEDIFF(day, d1, d2) |
| Year of a date | YEAR(d) or DATEPART(year, d) |
YEAR(d) |
extract(year from d) |
| Cast to date | CAST(ts AS date) |
DATE(ts) |
ts::date or CAST(ts AS DATE) |
Watch the argument order. DATEDIFF(day, start, end) puts the unit first in Snowflake, SQL Server and Redshift. In Databricks and MySQL it takes two arguments with the later date first. DATE_DIFF in BigQuery puts the later date first and the unit last.
Limiting rows and filtering on windows
| Task | PostgreSQL | Snowflake | Databricks | BigQuery |
|---|---|---|---|---|
| First 10 rows | LIMIT 10 |
LIMIT 10 |
LIMIT 10 |
LIMIT 10 |
| Filter on a window result | CTE and WHERE |
QUALIFY |
QUALIFY |
QUALIFY |
| Task | SQL Server | MySQL | Redshift |
|---|---|---|---|
| First 10 rows | SELECT TOP 10 ... or OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY |
LIMIT 10 |
LIMIT 10 |
| Filter on a window result | CTE and WHERE |
CTE and WHERE |
QUALIFY goes after HAVING and filters on a window function directly, for example qualify row_number() over (partition by k order by d desc) = 1. Where it does not exist, wrap the query in a CTE.
Conditional aggregation, patterns and division
| Task | PostgreSQL | Snowflake | Databricks | BigQuery |
|---|---|---|---|---|
| Count matching rows | count(*) FILTER (WHERE x) |
COUNT_IF(x) |
count(*) FILTER (WHERE x) |
COUNTIF(x) |
| Same, with CASE (works everywhere) | sum(CASE WHEN x THEN 1 ELSE 0 END) |
same | same | same |
| Case-insensitive match | col ILIKE 'a%' |
col ILIKE 'a%' |
col ILIKE 'a%' |
LOWER(col) LIKE 'a%' |
7 / 2 |
3 (integer division) |
3.5 |
3.5, integer form is 7 div 2 |
3.5, integer form is DIV(7, 2) |
| Divide, NULL on zero | a / NULLIF(b, 0) |
DIV0NULL(a, b) |
a / NULLIF(b, 0) |
SAFE_DIVIDE(a, b) |
| NULL-safe equals | a IS NOT DISTINCT FROM b |
a IS NOT DISTINCT FROM b |
a <=> b |
a IS NOT DISTINCT FROM b |
| Task | SQL Server | MySQL | Redshift |
|---|---|---|---|
| Count matching rows | sum(CASE WHEN cond THEN 1 ELSE 0 END) |
sum(x) where x is a condition (true is 1), or CASE |
sum(CASE WHEN x THEN 1 ELSE 0 END) |
| Case-insensitive match | LOWER(col) LIKE 'a%' |
col LIKE 'a%' (case-insensitive under the default collation) |
col ILIKE 'a%' |
7 / 2 |
3 (integer division) |
3.5, integer form is 7 DIV 2 |
3 (integer division) |
| Divide, NULL on zero | a / NULLIF(b, 0) |
a / b returns NULL on zero by default |
a / NULLIF(b, 0) |
| NULL-safe equals | a IS NOT DISTINCT FROM b (2022 and later) |
a <=> b |
(a = b OR (a IS NULL AND b IS NULL)) |
FILTER is not available in Snowflake, BigQuery, SQL Server or MySQL, so use CASE there. In CASE, remember COUNT counts non-NULL values: count(CASE WHEN x THEN 1 END) counts matches, but count(CASE WHEN x THEN 1 ELSE 0 END) counts every row.
Strings and aggregation of text
| Task | PostgreSQL | Snowflake | Databricks | BigQuery |
|---|---|---|---|---|
| Join text | a || b or concat(a, b) |
a || b or CONCAT(a, b) |
concat(a, b) or a || b |
CONCAT(a, b) |
| Length | length(s) |
LENGTH(s) |
length(s) |
LENGTH(s) |
Part before the @ |
split_part(s, '@', 1) |
SPLIT_PART(s, '@', 1) |
split_part(s, '@', 1) |
SPLIT(s, '@')[OFFSET(0)] |
| Concatenate values per group | string_agg(x, ', ' ORDER BY x) |
LISTAGG(x, ', ') WITHIN GROUP (ORDER BY x) |
array_join(collect_list(x), ', ') |
STRING_AGG(x, ', ' ORDER BY x) |
| Task | SQL Server | MySQL | Redshift |
|---|---|---|---|
| Join text | CONCAT(a, b) or a + b (NULL makes + NULL) |
CONCAT(a, b) |
a || b or CONCAT(a, b) |
| Length | LEN(s) (ignores trailing spaces) |
CHAR_LENGTH(s) |
LEN(s) |
Part before the @ |
LEFT(s, CHARINDEX('@', s) - 1) |
SUBSTRING_INDEX(s, '@', 1) |
SPLIT_PART(s, '@', 1) |
| Concatenate values per group | STRING_AGG(x, ', ') WITHIN GROUP (ORDER BY x) |
GROUP_CONCAT(x ORDER BY x SEPARATOR ', ') |
LISTAGG(x, ', ') WITHIN GROUP (ORDER BY x) |
Medians and percentiles
| Engine | Median |
|---|---|
| PostgreSQL | percentile_cont(0.5) WITHIN GROUP (ORDER BY x) as an aggregate |
| Snowflake | MEDIAN(x) or percentile_cont(0.5) WITHIN GROUP (ORDER BY x) as an aggregate |
| Databricks | median(x) or percentile_cont(0.5) WITHIN GROUP (ORDER BY x) |
| BigQuery | PERCENTILE_CONT(x, 0.5) OVER (PARTITION BY g), window form only. Approximate: APPROX_QUANTILES(x, 100)[OFFSET(50)] |
| SQL Server | PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x) OVER (PARTITION BY g), window form only |
| MySQL | n/a built in. Number the rows with ROW_NUMBER() and take the middle one or two |
| Redshift | MEDIAN(x) or PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY x) |
In the window-only engines, add SELECT DISTINCT or a second step to get one row per group.
Date series
A date spine gives one row per day, even days with no data. Left join your facts to it.
-- PostgreSQL
select d::date as day
from generate_series(date '2025-01-01', date '2025-01-31', interval '1 day') as g(d);
-- BigQuery
select day
from unnest(generate_date_array(date '2025-01-01', date '2025-01-31')) as day;
-- Databricks
select explode(sequence(date '2025-01-01', date '2025-01-31', interval 1 day)) as day;
-- Snowflake
select dateadd(day, row_number() over (order by seq4()) - 1, date '2025-01-01') as day
from table(generator(rowcount => 31));
-- MySQL 8: recursive CTE
with recursive days as (
select date '2025-01-01' as day
union all
select day + interval 1 day from days where day < date '2025-01-31'
)
select day from days;The last block is MySQL syntax. SQL Server also uses a recursive CTE, but it uses DATEADD(day, 1, day) in place of + interval 1 day, drops the word recursive and writes CAST('2025-01-01' AS date) for the literal. A Redshift version is left out.
As-of joins
An as-of join matches each row to the latest row of another table on or before its date, such as the exchange rate in force on the order date.
-- Snowflake
select o.order_id, r.usd_per_unit
from orders o
asof join fx_rates r
match_condition (o.order_date >= r.rate_date)
on o.currency = r.currency;
-- PostgreSQL: lateral join
select o.order_id, r.usd_per_unit
from orders o
left join lateral (
select usd_per_unit from fx_rates r
where r.currency = o.currency and r.rate_date <= o.order_date
order by r.rate_date desc limit 1
) r on true;
-- SQL Server: outer apply
select o.order_id, r.usd_per_unit
from orders o
outer apply (
select top 1 usd_per_unit from fx_rates r
where r.currency = o.currency and r.rate_date <= o.order_date
order by r.rate_date desc
) r;The portable version works everywhere: join on r.rate_date <= o.order_date, number the matches per order with row_number() over (partition by o.order_id order by r.rate_date desc), and keep number 1.
Try it on the practice database
This runs on the PostgreSQL practice data. It shows date_trunc, a FILTER count and a guarded division in one query.
select date_trunc('month', ordered_at)::date as month,
count(*) as orders,
count(*) filter (where status = 'refunded') as refunded,
round(100.0 * count(*) filter (where status = 'refunded') / nullif(count(*), 0), 1) as refund_pct
from orders
group by 1
order by 1
limit 6;