Top 30 SQL Queries Asked in Data Analyst & Backend Developer Interviews with Solutions
Master the most frequently asked SQL coding questions in technical interviews: 2nd highest salary, duplicate detection, window functions, self joins, and running totals.
Whether you are applying for a Data Analyst, Business Intelligence Engineer, or Backend Software Developer role at companies like Amazon, Swiggy, TCS, or modern tech startups, SQL live coding rounds are the primary barrier between you and the offer letter.
Recruiters don't just test basic SELECT * FROM table; queries. They want to see if you can handle NULL values, tie-breaking logic, self joins, Common Table Expressions (CTEs), and modern window functions.
Here is an essential collection of the top SQL queries most frequently tested in technical rounds, complete with tested syntax and logic explanations compiled by Er. Sumit Kumar at MSK Institute.
1. How to Find the Nth (or 2nd) Highest Salary in an Employee Table?
This is unanimously the single most asked SQL interview problem in India!
Method A: Using DENSE_RANK() (Recommended Industry Standard)
DENSE_RANK() handles duplicates and ties seamlessly without skipping ranks:
Method B: Subquery with MAX()
Method C: Using LIMIT & OFFSET (MySQL / PostgreSQL)
DENSE_RANK() demonstrates senior engineering maturity!2. How to Find and Delete Duplicate Records from a Table?
Given a table with accidental duplicate rows, how do you keep only one unique copy?
Finding Duplicates:
Deleting Duplicates using CTE and ROW_NUMBER():
3. Difference Between RANK(), DENSE_RANK(), and ROW_NUMBER()
Consider three employees with salaries: 100k, 100k, 80k. Look at how each window function assigns ranks:
| Salary | ROW_NUMBER() | RANK() | DENSE_RANK() |
|---|---|---|---|
| 100k | 1 | 1 | 1 |
| 100k | 2 | 1 | 1 |
| 80k | 3 | 3 (Skips 2!) | 2 (Never skips!) |
ROW_NUMBER(): Assigns a strictly unique incremental integer to every row regardless of duplicate values.RANK(): Gives identical rank to ties, but skips the next rank numbers.DENSE_RANK(): Gives identical rank to ties, and continues sequentially without skipping numbers.
4. How to Calculate a Running Total (Cumulative Sum) in SQL?
A favorite in financial and e-commerce analytics interviews:
5. Self-Join: Find Employees Who Earn More Than Their Managers
Given an employees table with emp_id, emp_name, salary, and manager_id:
6. How to Find Records Present in Table A but NOT in Table B?
Example: Find customers who registered on your platform but never placed an order.
Method A: Using LEFT JOIN (High Performance)
Method B: Using NOT EXISTS
7. How to Calculate Year-over-Year (YoY) Sales Growth Using LAG()?
8. What is the Difference Between WHERE and HAVING?
WHERE: Filters individual rows before any grouping or aggregation takes place. It cannot evaluate aggregate functions likeSUM()orCOUNT().HAVING: Filters grouped summary rows after theGROUP BYclause has aggregated the data.
9. Summary & 3 Best Practices for SQL Interviews
- 1Always State Your Assumptions: Before writing queries, confirm whether column values can be
NULLor contain duplicate keys. - 2Format Queries with Clean Uppercase Keywords: Writing clauses on new indented lines makes your solution look professional and readable.
- 3Practice on Live Datasets: Don't just read queries passively. Test and modify complex joins inside our live MSK Code Playground or join our Data Analysis Combo Cohort to master 100+ business SQL queries under direct mentor feedback!
Master These Skills in Our Offline Labs
Data Analysis Mastery Combo Course - 12 Months
Master data analytics from scratch to an industry-ready professional level. This comprehensive 12-month program covers Advanced Excel, SQL, Python Programming, NumPy, Pandas, Data Wrangling, Exploratory Data Analysis (EDA), Matplotlib, Seaborn, Power BI, Tableau, Applied Statistics, and Machine Learning Capstones.
Python Backend Development Mastery Combo - 8 Months
Become a professional Python Backend Developer with this 8-month program covering Django, ORM, REST APIs, Authentication, Deployment, and Advanced Backend Concepts.