SQL remains the single most-tested skill in data analyst, data engineer, and business analyst interviews across every major company in 2026 — from TCS and Infosys to Amazon and Google.

After analyzing thousands of interview experiences shared on Glassdoor, LinkedIn, and our own user reports, we've compiled the 30 most-asked SQL questions with clear, interview-ready answers you can use directly.

Pro Tip: Don't just memorize answers. Practice writing each query by hand. Interviewers test your thinking process, not just the final query. Use GhostMode's Code Playground to practice live.

📗 Basic SQL Questions (Fresher / 1-2 Years)

Q1
What is the difference between WHERE and HAVING clause?
Model Answer
WHERE filters rows before grouping — it cannot use aggregate functions. HAVING filters after GROUP BY and can use aggregate functions like COUNT(), SUM(), AVG().

Example: SELECT dept, COUNT(*) FROM emp GROUP BY dept HAVING COUNT(*) > 5
Q2
What are the different types of JOINs in SQL?
Model Answer
INNER JOIN — Returns matching rows from both tables.
LEFT JOIN — All rows from left + matching from right (NULL if no match).
RIGHT JOIN — All rows from right + matching from left.
FULL OUTER JOIN — All rows from both tables.
CROSS JOIN — Cartesian product of both tables.
Q3
What is the difference between DELETE, TRUNCATE and DROP?
Model Answer
DELETE: Removes specific rows. Can use WHERE. Can be rolled back. Logged operation.
TRUNCATE: Removes all rows. Cannot use WHERE. Cannot be rolled back. Faster.
DROP: Removes the entire table structure. Cannot be rolled back.
Q4
What is a PRIMARY KEY vs UNIQUE KEY?
Model Answer
PRIMARY KEY: Uniquely identifies each row. Cannot be NULL. Only one per table. Automatically creates a clustered index.
UNIQUE KEY: Also enforces uniqueness but can have NULL values (one NULL per column). Multiple unique keys allowed per table.
Q5
What is the difference between CHAR and VARCHAR?
Model Answer
CHAR(n): Fixed-length. Always stores n characters, padding with spaces if shorter. Faster for fixed-size data like country codes.
VARCHAR(n): Variable-length. Only stores actual characters used. More storage-efficient for varying data.

📘 Intermediate SQL Questions (2-5 Years)

Q6
Write a query to find the 2nd highest salary from an Employee table.
Model Answer — Multiple Approaches
-- Method 1: Using LIMIT/OFFSET SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT 1 OFFSET 1; -- Method 2: Using Subquery SELECT MAX(salary) FROM Employee WHERE salary < (SELECT MAX(salary) FROM Employee); -- Method 3: DENSE_RANK() — Best for interviews SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM Employee ) t WHERE rnk = 2;
Q7
What are Window Functions? Name some commonly used ones.
Model Answer
Window functions perform calculations across a set of rows related to the current row, without collapsing them (unlike GROUP BY).

Common window functions:
ROW_NUMBER() — Unique row number per partition
RANK() — Rank with gaps for ties
DENSE_RANK() — Rank without gaps
LAG() / LEAD() — Access previous/next row value
SUM() OVER / AVG() OVER — Running totals/averages
SELECT name, salary, DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC) as dept_rank FROM Employee;
Q8
What is a CTE (Common Table Expression)? When do you use it?
Model Answer
A CTE is a named temporary result set defined using the WITH clause. It improves readability and can be referenced multiple times in the same query.
WITH SalesRanked AS ( SELECT salesperson_id, SUM(amount) as total_sales, RANK() OVER (ORDER BY SUM(amount) DESC) as rnk FROM Sales GROUP BY salesperson_id ) SELECT * FROM SalesRanked WHERE rnk <= 5;
Use CTEs when: the query is complex, you need to reference the same subquery multiple times, or for recursive queries.
Q9
Find duplicate records in a table.
Model Answer
-- Find duplicates SELECT email, COUNT(*) as count FROM Users GROUP BY email HAVING COUNT(*) > 1; -- Delete duplicates, keep one DELETE FROM Users WHERE id NOT IN ( SELECT MIN(id) FROM Users GROUP BY email );
Q10
What is the difference between UNION and UNION ALL?
Model Answer
UNION: Combines result sets and removes duplicates. Slower due to deduplication step.
UNION ALL: Combines result sets and keeps all rows including duplicates. Faster. Use when you know there are no duplicates or duplicates are acceptable.

📕 Advanced SQL Questions (5+ Years / Senior Roles)

Q11
What is query optimization? How do you optimize a slow SQL query?
Model Answer
Key optimization techniques:

1. Use indexes on columns in WHERE, JOIN, ORDER BY
2. Avoid SELECT * — select only needed columns
3. Use EXISTS instead of IN for existence checks
4. Avoid functions on indexed columns in WHERE clause
5. Use EXPLAIN/EXPLAIN ANALYZE to understand query plan
6. Partition large tables for faster range scans
7. Avoid correlated subqueries in SELECT clause — use JOINs instead
Q12
Write a query to calculate running total of sales per day.
Model Answer
SELECT sale_date, daily_sales, SUM(daily_sales) OVER ( ORDER BY sale_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS running_total FROM daily_sales_summary ORDER BY sale_date;

🏢 Company-Specific SQL Questions

Amazon / Flipkart
Find customers who placed orders in every month of 2025.
Model Answer
SELECT customer_id FROM Orders WHERE YEAR(order_date) = 2025 GROUP BY customer_id HAVING COUNT(DISTINCT MONTH(order_date)) = 12;
TCS / Infosys / Wipro
Find employees whose salary is greater than their department average.
Model Answer
SELECT e.name, e.department, e.salary FROM Employee e JOIN ( SELECT department, AVG(salary) as avg_sal FROM Employee GROUP BY department ) dept_avg ON e.department = dept_avg.department WHERE e.salary > dept_avg.avg_sal ORDER BY e.department, e.salary DESC;
Google / Data Analyst Role
Calculate Month-over-Month (MoM) revenue growth.
Model Answer
SELECT month, revenue, LAG(revenue) OVER (ORDER BY month) as prev_month_revenue, ROUND( (revenue - LAG(revenue) OVER (ORDER BY month)) * 100.0 / NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 2 ) AS mom_growth_pct FROM monthly_revenue ORDER BY month;

✅ Last-Minute SQL Interview Tips

  1. Always think out loud — explain your approach before writing the query
  2. Ask about data size and edge cases (NULLs, duplicates) before finalizing
  3. Know the difference between RANK vs DENSE_RANK vs ROW_NUMBER — commonly asked
  4. Practice writing queries without references — muscle memory matters
  5. For Data Analyst roles, expect business-context SQL, not just syntax
  6. Know when to use CTE vs subquery vs temp table
🎙️

Practice These SQL Questions in a Live Mock Interview

GhostMode AI's mock interview simulator asks you SQL questions verbally and gives real-time feedback on your answers, confidence, and explanation quality.

Start Free SQL Mock Interview

Share This Guide