SQL Questions
The six SQL answers below were run on SQLite 3.50.4 on October 16, 2026, and the outputs shown are the actual results. Warehouse dialects differ in small ways, noted where they matter.
6. Write a query that keeps only the latest version of each order.
Number the rows per key with ROW_NUMBER(), newest first, and keep row 1. Add a tiebreaker so the result is deterministic when two versions share a timestamp.
SELECT order_id, status, updated_at
FROM (
SELECT r.*,
ROW_NUMBER() OVER (
PARTITION BY order_id
ORDER BY updated_at DESC, _loaded_at DESC
) AS rn
FROM raw_orders r
) AS t
WHERE rn = 1;
-- order 1 has three versions, two with the same updated_at
-- result: (1, 'shipped', '2026-10-02 14:00'), (2, 'pending', '2026-10-01 10:00')
Follow-ups worth asking. Why not GROUP BY order_id with MAX(updated_at)? Because it returns the timestamp, not the rest of the row, and a join back duplicates rows on ties. Why not DISTINCT?
Two versions differ in a load column, so both survive. Snowflake, BigQuery and Databricks also support QUALIFY rn = 1, which removes the subquery.
7. What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
They differ only on ties. ROW_NUMBER breaks ties arbitrarily, RANK gives ties the same number and leaves a gap, and DENSE_RANK gives ties the same number with no gap.
SELECT rep, total,
ROW_NUMBER() OVER (PARTITION BY region ORDER BY total DESC) AS rn,
RANK() OVER (PARTITION BY region ORDER BY total DESC) AS rnk,
DENSE_RANK() OVER (PARTITION BY region ORDER BY total DESC) AS drnk
FROM sales;
-- ana 500 1 1 1
-- ben 500 2 1 1
-- cy 300 3 3 2
-- dee 200 4 4 3
The question behind the question is "top 2 reps per region." With ROW_NUMBER, Ben or Ana is dropped at random. With RANK() <= 2 both appear and Cy does not. With DENSE_RANK() <= 2 all three appear.
A senior candidate asks the interviewer which answer the business wants before writing it.
8. Find each user's streaks of consecutive login days (gaps and islands).
Subtract a row number from the date. Consecutive days share the same difference, so the difference becomes a group key for each island. Deduplicate first, or a user who logs in twice on one day breaks the streak.
WITH days AS (
SELECT DISTINCT user_id, login_date FROM logins
),
grp AS (
SELECT user_id, login_date,
julianday(login_date)
- ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date) AS island
FROM days
)
SELECT user_id,
MIN(login_date) AS streak_start,
MAX(login_date) AS streak_end,
COUNT(*) AS streak_days
FROM grp
GROUP BY user_id, island;
-- (7, '2026-10-01', '2026-10-03', 3)
-- (7, '2026-10-06', '2026-10-07', 2)
-- (9, '2026-10-05', '2026-10-05', 1)
julianday is SQLite. In Snowflake or PostgreSQL the same idea is login_date - ROW_NUMBER() ... cast to an interval of days.
The pattern also solves sessionization, outage windows and "longest run of failed jobs", so ask the candidate where else they have used it.
9. What is the difference between ROWS and RANGE in a window frame?
ROWS counts physical rows. RANGE groups rows with equal ORDER BY values as peers.
When a window has ORDER BY but no frame clause, the SQL default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so tied rows all get the same running total.
SELECT day, amt,
SUM(amt) OVER (ORDER BY day) AS range_default,
SUM(amt) OVER (ORDER BY day
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rows_frame
FROM ev;
-- 2026-10-01 10 30 10
-- 2026-10-01 20 30 30
-- 2026-10-02 5 35 35
The second trap is a "7-day moving average" written as ROWS BETWEEN 6 PRECEDING AND CURRENT ROW. That is seven rows, not seven days. If a day has no data, the window reaches further back.
In the test data, a three-row average on October 4 included October 1 because October 3 was missing. The fix is a date spine: left join a calendar table so every day has a row, then apply the frame.
10. Write an incremental upsert that is safe to run twice and never lets stale data win.
Upsert on the business key, and only overwrite when the incoming row is newer. Without the newer-than check, a late-arriving old record overwrites a fresh one.
INSERT INTO dim_customer (customer_id, email, updated_at)
SELECT customer_id, email, updated_at FROM stg_customer WHERE true
ON CONFLICT (customer_id) DO UPDATE SET
email = excluded.email,
updated_at = excluded.updated_at
WHERE excluded.updated_at > dim_customer.updated_at;
-- before: (1, a@old.com, 10-01), (2, b@x.com, 10-05)
-- stage: (1, a@new.com, 10-03), (2, b@stale.com, 10-02), (3, c@x.com, 10-04)
-- after: (1, a@new.com, 10-03), (2, b@x.com, 10-05), (3, c@x.com, 10-04)
-- second run: identical result
This is SQLite and PostgreSQL syntax (the WHERE true resolves a SQLite parsing ambiguity).
On Snowflake, BigQuery, Databricks and Iceberg tables the same logic is a MERGE INTO ... WHEN MATCHED AND s.updated_at > t.updated_at THEN UPDATE ... WHEN NOT MATCHED THEN INSERT; that version was not executed for this guide.
Two senior details: deduplicate the staging rows first (question 6), because MERGE fails or behaves unpredictably when two source rows match one target row, and filter the target scan to recent partitions so the merge does not read the whole table.
11. Why does this NOT IN query return no rows?
Because the subquery contains a NULL. x NOT IN (2, NULL) means x <> 2 AND x <> NULL, and any comparison with NULL is unknown, so no row qualifies.
-- churned contains (2) and (NULL)
SELECT id FROM customers
WHERE id NOT IN (SELECT customer_id FROM churned);
-- returns nothing
SELECT id FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM churned x WHERE x.customer_id = c.id
);
-- returns 1 and 3
Use NOT EXISTS or a left anti join for "rows with no match." It is a short question, but it shows whether the candidate thinks in three-valued logic, which matters every time a pipeline joins on a nullable key.