SQL Interview Questions 2026
The SQL patterns that appear in data engineering, data science, and analytics interviews: window functions, complex joins, aggregation traps, and real questions from top companies.
Who Gets SQL Interviews
SQL rounds are required for: data engineers, data scientists, analytics engineers, and business intelligence roles. Some product manager roles at data-heavy companies also include SQL screens. For SWE roles, SQL rarely appears unless the role is backend with heavy database work.
The difficulty progression: SQL screens for analyst roles test joins, GROUP BY, and HAVING. Data scientist SQL tests window functions and self-joins. Data engineer SQL tests query optimization, partitioning, and schema design. Know which level applies to your target role.
Window Functions: The Differentiator
Window functions separate senior candidates from junior ones in SQL interviews. The most important to know: ROW_NUMBER() (unique rank per partition), RANK() and DENSE_RANK() (tied rank handling), LAG() and LEAD() (access previous/next row values), and SUM/AVG/COUNT OVER (running totals and moving averages).
The most common interview pattern using window functions: "Find the second-highest salary in each department." Solution: use DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC), then filter WHERE rank = 2. Know why DENSE_RANK() is preferred over RANK() here (handles ties correctly).
Running total pattern: SUM(amount) OVER (PARTITION BY user_id ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). The ROWS clause is frequently tested and frequently confused.
Complex Join Patterns
Know these join types cold: INNER JOIN (only matching rows), LEFT JOIN (all left rows, null for non-matching right), SELF JOIN (join table to itself for comparative queries), and CROSS JOIN (cartesian product, used for generating all combinations).
High-frequency SQL interview questions using joins: find users who made a purchase but never made a return (NOT EXISTS or LEFT JOIN with NULL check), find consecutive days of activity (self-join on date-1), find employees who earn more than their manager (self-join on manager_id).
Top SQL Interview Questions
Window Functions Mastery
Window functions are the discriminator between junior and senior SQL candidates. They compute aggregations across rows related to the current row without collapsing rows the way GROUP BY does.
Core functions: ROW_NUMBER (unique sequential), RANK (gaps after ties), DENSE_RANK (no gaps), LAG / LEAD (previous / next row), FIRST_VALUE / LAST_VALUE / NTH_VALUE, SUM / AVG / COUNT over a window. Frame clauses: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW (running total), ROWS BETWEEN 6 PRECEDING AND CURRENT ROW (7-day rolling). PARTITION BY divides input into groups; ORDER BY orders within partition. Strong candidates default to window functions for "rank within group" or "cumulative" questions rather than self-joins.
JOIN Strategies and Performance
Senior SQL interviews probe whether you understand JOIN execution strategies. The query optimizer picks among hash join (best for equality joins on large tables, builds hash on smaller side), nested loop join (best for small inputs or indexed inner side), and merge join (best when both inputs are pre-sorted on join keys).
Common probes: explain why this query is slow (the answer usually involves missing indexes, function calls on indexed columns preventing index use, or wrong join order). Explain the difference between EXISTS and IN with a subquery (often EXISTS short-circuits and is faster). When does INNER JOIN beat LEFT JOIN performance-wise (LEFT JOIN preserves unmatched rows on the left, which can prevent some optimizer rewrites).
Common Table Expressions and Recursive Queries
CTEs (WITH clauses) improve readability for complex queries. They are not necessarily a performance optimization; most databases inline CTEs unless marked MATERIALIZED. Use them to give intermediate results a name when the query has multiple stages.
Recursive CTEs handle hierarchical or graph-like data: organizational reports, category trees, finding all descendants/ancestors. Pattern: anchor query (base case) UNION ALL recursive query (references itself). Termination is whatever condition makes the recursive query return zero rows. Interview probe: write a recursive CTE to find the chain of managers above a given employee.
SQL for Analytics Roles vs Engineering Roles
Analytics-focused SQL interviews (Data Analyst, Data Scientist, Analytics Engineer at Meta/Airbnb/Stripe) emphasize complex aggregation, cohort analysis, retention queries, and funnel conversion calculations. Window functions, CTEs, and PIVOT-style aggregations are everyday tools.
Engineering-focused SQL interviews emphasize query performance, indexing, transaction isolation levels, and the schema-design tradeoffs that produce maintainable queries. Be able to articulate ACID guarantees, lock granularity (row vs table vs predicate locks), isolation levels (read uncommitted, read committed, repeatable read, serializable) and the phenomena each prevents (dirty reads, non-repeatable reads, phantom reads).
Browse Real SQL Questions by Company
See actual SQL questions from data engineering and analytics interviews, from verified candidate reports.
Browse SQL Questions