Uncorp
Log In
Data Engineering4 min readUpdated 2026-10-26

Top SQL Interview Questions & Window Functions Guide for Backend, Data & Analytics Roles

Master the exact SQL patterns—ROW_NUMBER vs RANK vs DENSE_RANK, LAG/LEAD, Running Totals, Recursive CTEs, Index Tuning, and ACID isolation levels—that decide live SQL rounds.

Naitik Tailor & Uncorp Engineering Faculty
Top SQL Interview Questions & Window Functions Guide for Backend, Data & Analytics Roles — Advanced SQL Window Functions, CTEs & Query Optimization
TL;DR — Key Engineering & Career Takeaways
  • Over 80% of live SQL coding rounds at MNCs and GCCs test 5 core patterns: Top-N per group (`DENSE_RANK`), Period-over-Period comparison (`LAG`/`LEAD`), Rolling aggregations (`SUM OVER`), Self-Joins/CTEs, and NULL-safe anti-joins.
  • Understanding `WHERE` vs `HAVING` execution order and B-Tree index sargability is mandatory for Backend and Data Engineering system design rounds.
  • Never use `NOT IN` when the subquery column can contain `NULL` values—it silently returns 0 rows; always use `NOT EXISTS` or `LEFT JOIN ... WHERE id IS NULL`.

1. Why SQL Rounds Eliminate Otherwise Strong Programmers

Many backend developers rely on ORM auto-generated queries (`findByUserId` in Hibernate/JPA) for years and freeze when asked to write a multi-CTE analytical query in a 45-minute live pad. Meanwhile, in Data Engineering and Data Analytics interviews, SQL is the #1 gatekeeper round.

The good news? Almost every live SQL question is a variation of 5 repeatable patterns.

2. The 5 SQL Patterns Tested in 90% of Interviews

Memorize the differences and use cases in this table before your next technical round:

High-Frequency SQL Interview Patterns & Solutions
Interview Question ArchetypeCore SQL Function / PatternWhy Candidates Fail ItCorrect Production Approach
1. Find the Top 3 highest-paid employees in each department (handling ties)`DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC)` inside a CTEUsing `LIMIT 3` (only returns 3 globally) or `RANK()` (skips numbers after a tie)Use `DENSE_RANK()` when ties shouldn't skip ranks; `ROW_NUMBER()` when you need exactly 1 row per group
2. Calculate Month-over-Month (MoM) revenue growth %`LAG(revenue, 1) OVER (ORDER BY month)`Writing messy inequality self-joins that break when a month is missingCompute `prev_month_rev` via `LAG()` in a CTE, then calculate `(rev - prev) * 100.0 / NULLIF(prev, 0)`
3. Calculate a 7-day rolling average of daily active users`AVG(dau) OVER (ORDER BY dt ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)`Confusing `ROWS BETWEEN` with `RANGE BETWEEN` on duplicate or missing datesAggregate to 1 row per date first, then apply the explicit window frame
4. Deduplicate a table keeping only the latest updated record per customer`ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) = 1`Using `GROUP BY customer_id` while selecting non-aggregated columnsFilter `WHERE rn = 1` in the outer query (or `QUALIFY rn = 1` in Snowflake/Databricks)
5. Find customers who never placed an order in 2026`LEFT JOIN` + `IS NULL` or `NOT EXISTS`Using `WHERE customer_id NOT IN (SELECT customer_id FROM orders)` when `customer_id` has a single `NULL`Always prefer `NOT EXISTS` for NULL-safety and superior query planner short-circuiting

3. Senior Follow-Up: 'Your Query Works, Now Make It Run 100x Faster'

Once your SQL query passes the test cases, a senior interviewer will ask how it behaves on 500 million rows. Keep these 4 query tuning principles ready:

  • Keep Predicates Sargable: Never wrap an indexed column in a function like `WHERE YEAR(created_at) = 2026`—that forces a full table scan. Write `WHERE created_at >= '2026-01-01' AND created_at < '2027-01-01'` so the B-Tree index is used.
  • Composite Index Left-Prefix Rule: An index on `(merchant_id, status, created_at)` accelerates queries filtering by `merchant_id` or `(merchant_id, status)`, but is useless for a query filtering only by `status`.
  • Avoid `SELECT *` in Columnar Warehouses: In Snowflake, BigQuery, Redshift, and Parquet Lakehouses, scanning only the 4 columns you need cuts I/O and compute cost by 90%.
  • Push Filters Down Early: Filter rows with `WHERE` before expensive `JOIN` and `WINDOW` operations.

Frequently Asked Questions

What is the exact difference between ROW_NUMBER(), RANK(), and DENSE_RANK()?

For values `[100, 100, 90]`: `ROW_NUMBER()` assigns `[1, 2, 3]` (arbitrary tie-breaking, unique sequential integers). `RANK()` assigns `[1, 1, 3]` (ties get the same rank, next rank skips to 3). `DENSE_RANK()` assigns `[1, 1, 2]` (ties get the same rank, no gaps).

Where is advanced SQL and query tuning taught hands-on at Uncorp?

Advanced SQL (Joins, Subqueries, CTEs, Window Functions, Indexing, and Execution Plan Optimization) is taught in deep detail in both 'Data Engineering Module 1 / Master Program' and 'Java Backend Development with Gen AI'.

Written & Reviewed By Uncorp Engineering Faculty

Naitik Tailor & Uncorp Engineering Faculty

Founder & Principal Engineering Mentors (12+ Yrs Enterprise Experience) · VIT CSE '21 · Symbiosis MBA '24 · Ex-Delhivery & Ex-Aditya Birla Capital

About Uncorp

Continue Reading in This Topic Cluster

All 73 Guides