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.

- 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:
| Interview Question Archetype | Core SQL Function / Pattern | Why Candidates Fail It | Correct 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 CTE | Using `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 missing | Compute `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 dates | Aggregate 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 columns | Filter `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'.
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
Continue Reading in This Topic Cluster
All 73 Guides
What Exactly is Data Engineering—and How is It Different from a Data Analyst?
Clear breakdown of day-to-day responsibilities, tech stacks (Spark, Airflow, dbt, Kafka vs BI/Excel), interview rounds, and salary bands for Data Engineers vs Data Analysts.

Modern Data Engineering Roadmap: From SQL & Python to Spark, Databricks, Airflow, dbt & AWS
The complete 12-week curriculum blueprint to master batch ETL, real-time Kafka streaming, Databricks PySpark, dbt analytics engineering, and AWS Lakehouse architecture.

How to Prepare for a Technical Software Interview in 15 vs 30 Days: Sprint Study Plan
Tactical day-by-day execution plans when you have an upcoming interview at a Tier-1 MNC, GCC, or product company in 2 to 4 weeks.

