SQL Window Functions Interview Checklist

SQL Window Functions Interview Checklist

If a SQL interview question says "for each," "top N per group," "running total," or "compared to the previous row," it's almost always a window function question. Candidates who know window functions solve these in a few lines. Candidates who don't end up with fragile self-joins and nested subqueries that eat the clock.

This SQL window functions interview checklist covers the functions you'll actually use, how to choose between them, and the mistakes that cost the most time.

The core syntax, in one line

function_name(...) OVER (PARTITION BY group_cols ORDER BY sort_cols frame_clause)

Say this out loud in the interview: "A window function computes over a group of rows but keeps every row in the output." That one sentence shows you understand the difference from GROUP BY.

Ranking: pick the right function

FunctionTiesGaps after tiesUse when
ROW_NUMBER()Broken arbitrarilyn/aYou need exactly one row per rank
RANK()Same rankYes (1, 1, 3)Competition-style ranking
DENSE_RANK()Same rankNo (1, 1, 2)"Second highest salary" style questions

Ask the interviewer how ties should be handled before you pick. It's one of the best clarifying questions in a SQL round.

Top N per group

SELECT *
FROM (
  SELECT e.*,
         DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rnk
  FROM employees e
) ranked
WHERE rnk <= 3;

Remember: you can't filter on a window function in the same query's WHERE clause, because WHERE runs before window functions are computed. Wrap it in a subquery or CTE. Some databases support QUALIFY for this; mention it only if you know the engine supports it.

Running totals and moving averages

SELECT order_date,
       amount,
       SUM(amount) OVER (ORDER BY order_date) AS running_total,
       AVG(amount) OVER (ORDER BY order_date
                         ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS avg_7_rows
FROM orders;

Key point to explain: when you add ORDER BY to an aggregate window, many engines default to a frame of RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With duplicate dates, RANGE treats tied rows as peers and includes all of them, which can surprise you. If you want strict row-by-row behavior, say ROWS explicitly.

Comparing to previous or next rows

LAG and LEAD replace most self-joins:

SELECT user_id,
       login_date,
       LAG(login_date) OVER (PARTITION BY user_id ORDER BY login_date) AS prev_login
FROM logins;

Common uses:

Mention the default: LAG returns NULL for the first row in each partition, and you can pass a default value as the third argument.

Other functions worth knowing

A classic: consecutive days

"Find users who logged in three days in a row" is a common pattern. One approach:

  1. Assign ROW_NUMBER() per user ordered by date.
  2. Subtract that number (as days) from the date. Consecutive dates share the same result.
  3. Group by user and that derived value, then filter groups with count of three or more.

Explaining the trick before writing it is worth as much as the query itself.

Mistakes that cost the most time

How to talk through a SQL window answer

  1. Restate the output: "One row per department with its top three salaries."
  2. Name the tool: "That's top N per group, so a ranking window with PARTITION BY."
  3. Clarify ties: "Should equal salaries share a rank?"
  4. Write the inner query, then the outer filter.
  5. Walk through a tiny example of three or four rows.

Practice it under a clock

Window functions click fast once you've written a dozen of them, but under time pressure the syntax details slip. If you want a calm, structured way to rehearse out loud before your interview, Luna Interview is built for practice and prep—not for use during a live assessment.

Checklist

Master these and most "tricky" SQL interview questions become routine.