100 Most Asked SQL Interview Questions in 2025 (With Real-Life Examples)

If you’re preparing for your dream tech job, you won’t want to miss our ‘SQL Interview Questions Blog for 2025’. In today’s competitive market, having a strong grasp of SQL is crucial. This guide will walk you through the essential interview questions you might encounter, helping you unravel mysteries of data management and unlock your full coding potential. Ready to ace your interviews and level up your skills? Keep reading and let’s tackle these questions together!

Basic SQL Interview Questions

Suitable for freshers or entry-level data jobs

1. What is the difference between WHERE and HAVING clauses?

Asked by Infosys in Jan 2025 for Data Analyst role.
Answer:
WHERE filters rows before grouping, while HAVING filters groups after aggregation.
Use-case: Candidate was asked to filter sales data based on region and group total sales per product.

2. Explain primary key vs. unique key.

Question from Capgemini’s off-campus drive 2025.
Answer:
A primary key uniquely identifies each row and doesn’t allow nulls. A unique key allows one null value and ensures uniqueness.

3. What is the purpose of the GROUP BY clause?

Asked in Wipro’s SQL assessment for Graduate Trainee role.
Answer:
It groups rows that share a value in specified columns to perform aggregate functions like COUNT or SUM.

4. Difference between DELETE, TRUNCATE, and DROP.

Asked by Tech Mahindra, Feb 2025.
Answer:

  • DELETE: Removes rows, can use WHERE, and is rollback-able.
  • TRUNCATE: Removes all rows, faster, cannot be rolled back.
  • DROP: Deletes table structure entirely.

5. What is a NULL value in SQL?

Asked by Cognizant in an online aptitude test.
Answer:
NULL represents missing or unknown data, not zero or empty string. It needs to be checked using IS NULL.

6. How is the DISTINCT keyword used?

Question from Hexaware SQL MCQ round, March 2025.
Answer:
DISTINCT removes duplicate rows from the result set of a SELECT query.

7. What is the function of the LIKE operator?

Asked in Mindtree coding interview round.
Answer:
Used for pattern matching in WHERE clauses. E.g., LIKE 'A%' matches strings starting with ‘A’.

8. Difference between IN and EXISTS.

Asked in L&T Infotech’s database interview for interns.
Answer:
IN checks values in a list, EXISTS checks if a subquery returns rows. EXISTS is often faster in correlated queries.

9. What does the ORDER BY clause do?

Question in HCL campus interview 2025.
Answer:
Sorts result set by specified column(s), ascending by default or descending using DESC.

10. What is a foreign key?

Asked in Accenture’s Junior SQL Developer round.
Answer:
A foreign key links a column in one table to the primary key in another, enforcing referential integrity.

11. What is a subquery?

Asked by TCS Digital for system engineer trainee, Jan 2025.
Answer:
A query nested inside another query, often used to filter results or calculate values dynamically.

12. What are aggregate functions?

Question from Infosys SP interview (Final year students).
Answer:
Functions like SUM(), AVG(), COUNT(), MIN(), MAX() that operate on sets of values.

13. What is the purpose of the LIMIT clause?

Asked in an internship screening test by Zoho.
Answer:
Used to restrict the number of rows returned by a query, commonly used for pagination.

14. How does BETWEEN work in SQL?

Question from CTS SQL module quiz.
Answer:
Used to filter rows within a range of values. E.g., BETWEEN 100 AND 500.

15. What is the default sort order in ORDER BY?

Asked in Virtusa SQL basics interview.
Answer:
Ascending (ASC) is the default sorting order unless DESC is specified.

16. What is the use of the COUNT() function?

Asked in campus round by Mphasis.
Answer:
Returns the number of rows that match a specified condition.

17. Explain the difference between CHAR and VARCHAR.

Asked by Oracle for campus selection, March 2025.
Answer:
CHAR is fixed-length, while VARCHAR is variable-length. VARCHAR saves space for dynamic strings.

18. What are constraints in SQL?

Question from HDFC Tech interview (Data Ops role).
Answer:
Rules enforced on table columns to ensure data integrity, e.g., NOT NULL, UNIQUE, CHECK, PRIMARY KEY.

19. What is normalization?

Asked in Deloitte Business Analyst interview.
Answer:
The process of organizing data to reduce redundancy and improve integrity across tables.

20. What is an alias in SQL?

Question from EY Genpact data stream test.
Answer:
A temporary name for a column or table, created using the AS keyword, for readability.

Intermediate SQL Interview Questions (Real Company-Based)

Ideal for 1–3 years of experience

1. Write an SQL query to find the second highest salary from a table.

Commonly asked by TCS and Amazon in L1 rounds.
Answer:

SELECT MAX(salary) 
FROM employees 
WHERE salary < (SELECT MAX(salary) FROM employees);

2. What is a CTE and when would you use it over a subquery?

Asked in Zoho interview for Business Intelligence Developer.
Answer:
A CTE (Common Table Expression) is a temporary result set used to simplify complex joins and subqueries. Preferable when working with recursive queries or improving query readability.

3. How would you remove duplicate rows from a table but keep one?

Asked in Paytm SQL assessment for Junior Data Analyst.
Answer:
Using ROW_NUMBER() in a CTE and deleting rows where row number > 1:

WITH CTE AS (
  SELECT *, ROW_NUMBER() OVER(PARTITION BY col1, col2 ORDER BY id) AS rn
  FROM your_table
)
DELETE FROM CTE WHERE rn > 1;

4. What is the difference between RANK() and DENSE_RANK()?

Asked by Oracle Financial Services for MIS role.
Answer:
RANK() skips ranks when there’s a tie, while DENSE_RANK() doesn’t. Useful in leaderboard or ranking systems.

5. What is the use of CASE WHEN in SQL?

Question from Capgemini 2025 Analyst Interview.
Answer:
Conditional logic similar to IF-ELSE. Used to transform values dynamically in SELECT, WHERE, etc.

6. Explain indexing and how it impacts performance.

Asked in Deloitte Data Consultant round.
Answer:
Indexing speeds up data retrieval by creating quick lookup structures, but can slow down insert/update operations due to overhead.

7. What are window functions and how do they differ from aggregate functions?

Asked by ZS Associates in Business Analyst interview.
Answer:
Window functions operate on a set of rows related to the current row, without collapsing them into a single row (e.g., ROW_NUMBER(), LEAD()).

8. How do you handle NULLs in aggregate functions?

Asked in Accenture reporting project interview.
Answer:
Most aggregate functions ignore NULLs. To account for them explicitly, use COALESCE() or conditionals.

9. Explain INNER JOIN vs LEFT JOIN with an example.

Asked in IBM SQL test for Data Analyst.
Answer:
INNER JOIN returns matching rows; LEFT JOIN returns all from the left table, matching from the right.
Used in multi-table reporting scenarios.

10. How would you identify duplicate records in a table?

Question from Wipro analytics training test.
Answer:

SELECT col1, COUNT(*) 
FROM table 
GROUP BY col1 
HAVING COUNT(*) > 1;

11. What is a composite key?

Asked by Infosys for Data Operations Support.
Answer:
A primary key made up of two or more columns to uniquely identify a record.

12. How do you pivot data in SQL?

Asked in Microsoft Data Associate case round.
Answer:
Using CASE statements or built-in PIVOT() function (in T-SQL) to convert rows to columns.

13. How to find employees with the highest salary in each department?

Asked by TCS BFSI Division.
Answer:

SELECT * FROM (
  SELECT *, RANK() OVER(PARTITION BY department ORDER BY salary DESC) AS rk
  FROM employees
) t WHERE rk = 1;

14. What is the use of COALESCE()?

Asked in EY GDS SQL round.
Answer:
Returns the first non-null value in the given list. Used for handling NULLs in queries.

15. Explain difference between UNION and UNION ALL.

Asked in Infosys lateral hiring SQL test.
Answer:
UNION removes duplicates, UNION ALL retains them. UNION ALL is faster due to no deduplication.

16. How do you reverse a string in SQL?

Asked in LTI (L&T Infotech) automation analyst interview.
Answer (SQL Server):

SELECT REVERSE('Newtum');

17. Find top 3 highest selling products per category.

Asked by Flipkart for Reporting Analyst position.
Answer:
Use DENSE_RANK() over category and filter where rank <= 3.

18. How would you calculate a running total?

Asked in Swiggy Analyst role screening.
Answer:

SELECT order_id, SUM(amount) OVER(ORDER BY order_date) AS running_total
FROM orders;

19. What is a correlated subquery?

Question from Cognizant Analyst L1 round.
Answer:
A subquery that refers to a column from the outer query. It executes once per outer row.

20. How to count rows from multiple tables with a single query?

Asked by Reliance Jio for Data Operations Analyst.
Answer:

SELECT 
  (SELECT COUNT(*) FROM table1) AS count1,
  (SELECT COUNT(*) FROM table2) AS count2;

21. What is the difference between DELETE and TRUNCATE?

Asked again at Sutherland’s SQL workshop.
Answer:
DELETE can be filtered and rolled back. TRUNCATE is faster, removes all rows without condition, and is not rollback-able.

22. Explain data types in SQL.

Asked in Accenture’s Associate round (Data team).
Answer:
Categories include numeric (INT, FLOAT), string (CHAR, VARCHAR), date/time (DATE, TIMESTAMP).

23. How to join three or more tables?

Asked in TCS Ninja SQL Hackathon.
Answer:
Using multiple JOIN clauses:

SELECT * 
FROM A 
JOIN B ON A.id = B.a_id 
JOIN C ON B.id = C.b_id;

24. What are transactions in SQL?

Question from Mindtree’s back-end analyst test.
Answer:
A transaction is a sequence of operations performed as a single unit, with ACID properties.

25. What is the default isolation level in SQL Server?

Asked in HDFC Bank SQL Tech round.
Answer:
Read Committed — prevents dirty reads, allows non-repeatable reads and phantom reads.

26. How do you handle division by zero errors in SQL?

Asked in Byju’s Data Intern SQL test.
Answer:
Using NULLIF or CASE:

SELECT amount / NULLIF(units, 0) FROM sales;

27. What is the output of COUNT(*) vs COUNT(column_name)?

Question from Zomato’s Data Insights team.
Answer:
COUNT(*) counts all rows; COUNT(col) counts only non-null values in that column.

28. Difference between temp table and CTE?

Asked in Freshworks Data role technical round.
Answer:
CTE is a query-level alias; temp tables are physical objects stored in tempdb. Use CTEs for short, readable code.

29. How to filter based on the result of an aggregate function?

Asked in Mu Sigma SQL simulation round.
Answer:
Use HAVING clause:

SELECT dept, COUNT(*) 
FROM employees 
GROUP BY dept 
HAVING COUNT(*) > 10;

30. Write a query to fetch the 5 most recent orders per customer.

Asked by Meesho Business Data Analyst, April 2025.
Answer:
Use ROW_NUMBER() partitioned by customer ID and filter rows where row_num <= 5.

Advanced SQL Interview Questions (2025 Trends)

For senior-level or data engineering interviews

1. How do you optimize a slow-running SQL query with multiple joins?
Real-life issue raised in an Ola backend data team interview.
Answer:
Begin by analyzing the execution plan. Ensure proper indexing on JOIN columns. Use EXISTS instead of IN where appropriate. Break down complex queries into temp tables or CTEs. Avoid functions on indexed columns in WHERE clauses. Use proper join order — smaller filtered tables first.

2. What are window functions? Give an example use-case.
Asked by Flipkart for Data Engineer Level 2 position.
Answer:
Window functions perform calculations across a set of table rows related to the current row. Example: calculating a running average of sales over time:

SELECT order_id, 
       AVG(sales_amount) OVER (PARTITION BY region ORDER BY order_date) AS avg_sales
FROM orders;

3. What is a materialized view and when would you use it?
Asked in Google Cloud Platform SQL architecture interview.
Answer:
A materialized view stores the result of a query physically, unlike a normal view. It improves performance for frequent complex queries but needs to be refreshed manually or on schedule.

4. How do you detect and handle deadlocks in SQL?
Real scenario in HDFC Bank core transaction engine project.
Answer:
Detect through system logs or tools like SQL Server Profiler. Handle by keeping transactions short, accessing tables in a consistent order, and using lower isolation levels where safe.

5. Write an efficient query to get the latest record per user.
Asked by Swiggy for Operations Analytics Lead.
Answer:

SELECT * FROM (
  SELECT *, 
         ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY updated_at DESC) AS rn
  FROM user_logs
) t WHERE rn = 1;

6. What is the difference between HASH JOIN and MERGE JOIN?
Question from AWS Data Engineering Lab Assessment.
Answer:
HASH JOIN is best for unsorted, large datasets. It builds a hash table on the smaller table.
MERGE JOIN is optimal when both inputs are sorted on the join key. It scans both sequentially, improving performance.

7. How can partitioning improve performance in SQL databases?
Asked during Paytm’s architecture round.
Answer:
Partitioning divides large tables into smaller, manageable pieces (by range, list, or hash). Queries on specific partitions reduce I/O and improve query time.

8. What is a correlated subquery and how does it impact performance?
Asked by Oracle Cloud team for Database Architect.
Answer:
A correlated subquery references a column from the outer query. It runs once for each outer row, making it slower than a non-correlated one. Rewrite using JOINs or apply filters outside when possible.

9. How would you implement row-level security in SQL?
Real case from Microsoft Power BI + SQL backend setup.
Answer:
By applying filters using user context — via session variables or mapped user ID to data access levels in a separate permissions table joined in the WHERE clause.

10. How do you write a recursive SQL query?
Asked in Zoho’s advanced technical round for BI Developer.
Answer:
Using a CTE:

WITH RECURSIVE emp_hierarchy AS (
  SELECT emp_id, manager_id, name FROM employees WHERE manager_id IS NULL
  UNION ALL
  SELECT e.emp_id, e.manager_id, e.name
  FROM employees e
  JOIN emp_hierarchy h ON e.manager_id = h.emp_id
)
SELECT * FROM emp_hierarchy;

11. What is query execution plan and how do you use it?
Asked in Deloitte Architecture-level interview.
Answer:
It shows how the database engine executes a query — including join types, index usage, and row counts. Helps identify performance bottlenecks. Tools like EXPLAIN (PostgreSQL) or SET SHOWPLAN (SQL Server) are used.

12. What is the impact of SELECT * on performance?
Observed in performance audit by Meesho tech team.
Answer:
Using SELECT * retrieves unnecessary columns, increases memory usage, network load, and can invalidate index-only scans. Always select required columns.

13. How to handle millions of rows in a reporting query efficiently?
Case from BigBasket analytics dashboard optimization.
Answer:
Use summary tables, indexed columns, pagination with LIMIT/OFFSET, filtered CTEs, or data cubes for aggregate-heavy reporting.

14. What is denormalization? When is it appropriate?
Asked in Flipkart warehouse reporting design.
Answer:
Denormalization combines data from multiple tables into one for faster reads at the cost of redundancy. Use in read-heavy, performance-critical systems.

15. What is ACID in the context of SQL databases?
Asked by PhonePe in database transaction round.
Answer:
ACID = Atomicity, Consistency, Isolation, Durability. These ensure data integrity and reliability in multi-step transactions, even during system failures.

16. How would you find gaps in a sequence of IDs?
Asked in Razorpay internal ledger validation round.
Answer:

SELECT id + 1 AS start_missing
FROM transactions t1
WHERE NOT EXISTS (
  SELECT 1 FROM transactions t2 WHERE t2.id = t1.id + 1
);

17. What is data sharding and how is it different from partitioning?
Question from Gojek SQL architecture panel.
Answer:
Sharding splits data across multiple physical databases (horizontal scaling), while partitioning divides within a single database/table. Sharding improves distributed performance and scalability.

18. How do you handle slowly changing dimensions in SQL?
Asked in Salesforce Data Warehouse project interview.
Answer:
By versioning records using effective dates (SCD Type 2), overwriting rows (SCD Type 1), or keeping history in a related table.

19. How do you find the median from a table column using SQL?
Asked in Mu Sigma for Decision Scientist role.
Answer:
Using window functions:

SELECT AVG(col) FROM (
  SELECT col,
         ROW_NUMBER() OVER (ORDER BY col) AS rn,
         COUNT(*) OVER () AS cnt
  FROM table
) t WHERE rn IN (FLOOR((cnt+1)/2), CEIL((cnt+1)/2));

20. What are bitmap indexes? When are they used?
Asked in Data Engineering round at Airtel.
Answer:
Bitmap indexes use bitmaps for index entries, suitable for low-cardinality columns (like gender or status). Improves performance in OLAP systems.

21. How do you deal with large joins between fact and dimension tables?
Asked in Nykaa data warehouse optimization.
Answer:
Pre-aggregate fact tables, ensure indexed join keys, reduce granularity if possible, and use surrogate keys for consistency.

22. What is the difference between OLTP and OLAP systems?
Asked by IBM for ETL Architect interview.
Answer:
OLTP: real-time, transactional, normalized data, write-heavy.
OLAP: analytical, historical, denormalized, read-heavy. Used for reporting.

23. How would you load data incrementally using SQL?
Scenario shared by BigQuery-based ETL pipeline at Google.
Answer:
Track last updated timestamp or max ID processed. Use WHERE clause in load query to fetch new/updated records only.

24. Explain the concept of data skew and its impact.
Real case from Snapdeal’s Spark-SQL based pipeline.
Answer:
Data skew happens when a few values dominate, leading to unbalanced partitions. Causes slower query execution and memory pressure.

25. How do you convert rows into columns in SQL (transpose)?
Asked by Freshworks Business Data Engineering round.
Answer:
Use PIVOT() (SQL Server/Oracle) or aggregate with CASE:

SELECT 
  emp_id,
  MAX(CASE WHEN month = 'Jan' THEN salary END) AS Jan_Salary
FROM salaries
GROUP BY emp_id;

26. What is the difference between logical and physical data models in SQL systems?
Asked in ThoughtWorks database schema review.
Answer:
Logical model defines entities, relationships, and attributes without technical details. Physical model includes storage, indexing, partitioning.

27. How to reduce locking issues in highly concurrent environments?
Asked in Zerodha trading platform DB design.
Answer:
Use lower isolation levels (Read Committed Snapshot), keep transactions short, avoid explicit locks, and use optimistic concurrency control.

28. What is a surrogate key and why is it used?
Asked by Dunzo for Data Infrastructure Engineer.
Answer:
A surrogate key is a system-generated primary key (e.g., auto-increment ID) used instead of natural keys for performance and flexibility.

29. What are some anti-patterns in writing SQL queries?
Asked in PhonePe’s SQL review discussion.
Answer:
Using SELECT *, not indexing filters, redundant subqueries, nested joins without proper filtering, over-reliance on DISTINCT.

30. How do you track historical changes in transactional records?
Real-world usage at CRED for account activity logs.
Answer:
Use versioning with valid-from/valid-to dates, append-only tables, or audit logging with triggers.

Advanced SQL Interview Questions (2025 Trends)

For senior-level or data engineering interviews

1. How do you optimize a slow-running SQL query with multiple joins?
Real-life issue raised in an Ola backend data team interview.
Answer:
Begin by analyzing the execution plan. Ensure proper indexing on JOIN columns. Use EXISTS instead of IN where appropriate. Break down complex queries into temp tables or CTEs. Avoid functions on indexed columns in WHERE clauses. Use proper join order — smaller filtered tables first.

2. What are window functions? Give an example use-case.
Asked by Flipkart for Data Engineer Level 2 position.
Answer:
Window functions perform calculations across a set of table rows related to the current row. Example: calculating a running average of sales over time:

SELECT order_id, 
       AVG(sales_amount) OVER (PARTITION BY region ORDER BY order_date) AS avg_sales
FROM orders;

3. What is a materialized view and when would you use it?
Asked in Google Cloud Platform SQL architecture interview.
Answer:
A materialized view stores the result of a query physically, unlike a normal view. It improves performance for frequent complex queries but needs to be refreshed manually or on schedule.

4. How do you detect and handle deadlocks in SQL?
Real scenario in HDFC Bank core transaction engine project.
Answer:
Detect through system logs or tools like SQL Server Profiler. Handle by keeping transactions short, accessing tables in a consistent order, and using lower isolation levels where safe.

5. Write an efficient query to get the latest record per user.
Asked by Swiggy for Operations Analytics Lead.
Answer:

SELECT * FROM (
  SELECT *, 
         ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY updated_at DESC) AS rn
  FROM user_logs
) t WHERE rn = 1;

6. What is the difference between HASH JOIN and MERGE JOIN?
Question from AWS Data Engineering Lab Assessment.
Answer:
HASH JOIN is best for unsorted, large datasets. It builds a hash table on the smaller table.
MERGE JOIN is optimal when both inputs are sorted on the join key. It scans both sequentially, improving performance.

7. How can partitioning improve performance in SQL databases?
Asked during Paytm’s architecture round.
Answer:
Partitioning divides large tables into smaller, manageable pieces (by range, list, or hash). Queries on specific partitions reduce I/O and improve query time.

8. What is a correlated subquery and how does it impact performance?
Asked by Oracle Cloud team for Database Architect.
Answer:
A correlated subquery references a column from the outer query. It runs once for each outer row, making it slower than a non-correlated one. Rewrite using JOINs or apply filters outside when possible.

9. How would you implement row-level security in SQL?
Real case from Microsoft Power BI + SQL backend setup.
Answer:
By applying filters using user context — via session variables or mapped user ID to data access levels in a separate permissions table joined in the WHERE clause.

10. How do you write a recursive SQL query?
Asked in Zoho’s advanced technical round for BI Developer.
Answer:
Using a CTE:

WITH RECURSIVE emp_hierarchy AS (
  SELECT emp_id, manager_id, name FROM employees WHERE manager_id IS NULL
  UNION ALL
  SELECT e.emp_id, e.manager_id, e.name
  FROM employees e
  JOIN emp_hierarchy h ON e.manager_id = h.emp_id
)
SELECT * FROM emp_hierarchy;

11. What is query execution plan and how do you use it?
Asked in Deloitte Architecture-level interview.
Answer:
It shows how the database engine executes a query — including join types, index usage, and row counts. Helps identify performance bottlenecks. Tools like EXPLAIN (PostgreSQL) or SET SHOWPLAN (SQL Server) are used.

12. What is the impact of SELECT * on performance?
Observed in performance audit by Meesho tech team.
Answer:
Using SELECT * retrieves unnecessary columns, increases memory usage, network load, and can invalidate index-only scans. Always select required columns.

13. How to handle millions of rows in a reporting query efficiently?
Case from BigBasket analytics dashboard optimization.
Answer:
Use summary tables, indexed columns, pagination with LIMIT/OFFSET, filtered CTEs, or data cubes for aggregate-heavy reporting.

14. What is denormalization? When is it appropriate?
Asked in Flipkart warehouse reporting design.
Answer:
Denormalization combines data from multiple tables into one for faster reads at the cost of redundancy. Use in read-heavy, performance-critical systems.

15. What is ACID in the context of SQL databases?
Asked by PhonePe in database transaction round.
Answer:
ACID = Atomicity, Consistency, Isolation, Durability. These ensure data integrity and reliability in multi-step transactions, even during system failures.

16. How would you find gaps in a sequence of IDs?
Asked in Razorpay internal ledger validation round.
Answer:

SELECT id + 1 AS start_missing
FROM transactions t1
WHERE NOT EXISTS (
  SELECT 1 FROM transactions t2 WHERE t2.id = t1.id + 1
);

17. What is data sharding and how is it different from partitioning?
Question from Gojek SQL architecture panel.
Answer:
Sharding splits data across multiple physical databases (horizontal scaling), while partitioning divides within a single database/table. Sharding improves distributed performance and scalability.

18. How do you handle slowly changing dimensions in SQL?
Asked in Salesforce Data Warehouse project interview.
Answer:
By versioning records using effective dates (SCD Type 2), overwriting rows (SCD Type 1), or keeping history in a related table.

19. How do you find the median from a table column using SQL?
Asked in Mu Sigma for Decision Scientist role.
Answer:
Using window functions:

SELECT AVG(col) FROM (
  SELECT col,
         ROW_NUMBER() OVER (ORDER BY col) AS rn,
         COUNT(*) OVER () AS cnt
  FROM table
) t WHERE rn IN (FLOOR((cnt+1)/2), CEIL((cnt+1)/2));

20. What are bitmap indexes? When are they used?
Asked in Data Engineering round at Airtel.
Answer:
Bitmap indexes use bitmaps for index entries, suitable for low-cardinality columns (like gender or status). Improves performance in OLAP systems.

21. How do you deal with large joins between fact and dimension tables?
Asked in Nykaa data warehouse optimization.
Answer:
Pre-aggregate fact tables, ensure indexed join keys, reduce granularity if possible, and use surrogate keys for consistency.

22. What is the difference between OLTP and OLAP systems?
Asked by IBM for ETL Architect interview.
Answer:
OLTP: real-time, transactional, normalized data, write-heavy.
OLAP: analytical, historical, denormalized, read-heavy. Used for reporting.

23. How would you load data incrementally using SQL?
Scenario shared by BigQuery-based ETL pipeline at Google.
Answer:
Track last updated timestamp or max ID processed. Use WHERE clause in load query to fetch new/updated records only.

24. Explain the concept of data skew and its impact.
Real case from Snapdeal’s Spark-SQL based pipeline.
Answer:
Data skew happens when a few values dominate, leading to unbalanced partitions. Causes slower query execution and memory pressure.

25. How do you convert rows into columns in SQL (transpose)?
Asked by Freshworks Business Data Engineering round.
Answer:
Use PIVOT() (SQL Server/Oracle) or aggregate with CASE:

SELECT 
  emp_id,
  MAX(CASE WHEN month = 'Jan' THEN salary END) AS Jan_Salary
FROM salaries
GROUP BY emp_id;

26. What is the difference between logical and physical data models in SQL systems?
Asked in ThoughtWorks database schema review.
Answer:
Logical model defines entities, relationships, and attributes without technical details. Physical model includes storage, indexing, partitioning.

27. How to reduce locking issues in highly concurrent environments?
Asked in Zerodha trading platform DB design.
Answer:
Use lower isolation levels (Read Committed Snapshot), keep transactions short, avoid explicit locks, and use optimistic concurrency control.

28. What is a surrogate key and why is it used?
Asked by Dunzo for Data Infrastructure Engineer.
Answer:
A surrogate key is a system-generated primary key (e.g., auto-increment ID) used instead of natural keys for performance and flexibility.

29. What are some anti-patterns in writing SQL queries?
Asked in PhonePe’s SQL review discussion.
Answer:
Using SELECT *, not indexing filters, redundant subqueries, nested joins without proper filtering, over-reliance on DISTINCT.

30. How do you track historical changes in transactional records?
Real-world usage at CRED for account activity logs.
Answer:
Use versioning with valid-from/valid-to dates, append-only tables, or audit logging with triggers.

SQL for Real-World Scenarios and Case Studies

Practical application-based SQL questions from 2025 interviews

1. How would you detect anomalies in user transaction patterns over a rolling 7-day period?
Use-case from Razorpay’s Fraud Analytics team.
Answer:
Use a rolling average or standard deviation to calculate expected behavior. Then flag outliers:

SELECT user_id, transaction_date, amount,
       AVG(amount) OVER(PARTITION BY user_id ORDER BY transaction_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS rolling_avg
FROM transactions

Then apply threshold logic in a subquery or WHERE clause.

2. Write an SQL query to calculate customer retention for each month.
Asked during Paytm’s Product Analyst interview in May 2025.
Answer:
Retention = users active in Month X who also returned in Month X+1.
Use self-join on login data grouped by customer and month.

WITH logins AS (
  SELECT user_id, DATE_TRUNC('month', login_date) AS month
  FROM user_logins
  GROUP BY user_id, DATE_TRUNC('month', login_date)
)
SELECT l1.month AS cohort_month, COUNT(DISTINCT l1.user_id) AS retained_users
FROM logins l1
JOIN logins l2 
  ON l1.user_id = l2.user_id AND l2.month = l1.month + INTERVAL '1 month'
GROUP BY l1.month

3. How would you identify users who made purchases from two different cities within 24 hours?
Scenario from Amazon delivery location tracking.
Answer:
Self-join on user ID with time filter:

SELECT DISTINCT a.user_id
FROM purchases a
JOIN purchases b
  ON a.user_id = b.user_id 
  AND a.city <> b.city
  AND ABS(EXTRACT(EPOCH FROM a.purchase_time - b.purchase_time)) <= 86400

4. Write a query to find products that had zero sales in the last 30 days.
Asked in Nykaa BI Analyst interview.
Answer:

SELECT p.product_id, p.product_name
FROM products p
LEFT JOIN sales s 
  ON p.product_id = s.product_id AND s.sale_date >= CURRENT_DATE - INTERVAL '30 days'
WHERE s.product_id IS NULL

5. How would you calculate average time between two purchases per user?
Use-case from Swiggy’s customer frequency analysis.
Answer:
Use LAG() function to get previous purchase timestamp:

WITH purchase_gap AS (
  SELECT user_id, purchase_time,
         LAG(purchase_time) OVER(PARTITION BY user_id ORDER BY purchase_time) AS prev_time
  FROM orders
)
SELECT user_id, 
       AVG(EXTRACT(EPOCH FROM (purchase_time - prev_time)) / 3600) AS avg_hours_between_orders
FROM purchase_gap
WHERE prev_time IS NOT NULL
GROUP BY user_id

6. Identify the top 3 most returned products by category.
Real-world problem asked by Flipkart Returns team.
Answer:

SELECT * FROM (
  SELECT category, product_id, COUNT(*) AS return_count,
         RANK() OVER(PARTITION BY category ORDER BY COUNT(*) DESC) AS rnk
  FROM returns
  GROUP BY category, product_id
) t
WHERE rnk <= 3

7. Find users who never skipped a subscription month since joining.
Case study from Netflix India data hiring challenge.
Answer:
Generate all expected months using a date series and compare with actual payments using NOT EXISTS.

8. How would you assign daily rank to drivers based on total rides completed?
Scenario from Ola’s fleet performance system.
Answer:

SELECT driver_id, ride_date, 
       RANK() OVER(PARTITION BY ride_date ORDER BY COUNT(*) DESC) AS daily_rank
FROM rides
GROUP BY driver_id, ride_date

9. Calculate total and average spend per user on their most active weekday.
Asked in Swiggy’s customer segmentation round.
Answer:
Use CTE to find the weekday with most orders per user, then aggregate spend for that weekday.

10. Write an SQL query to flag the first purchase after a promotional email.
Asked in Meesho email marketing funnel analysis.
Answer:

WITH email_events AS (
  SELECT user_id, email_sent_date
  FROM promo_emails
),
first_purchase AS (
  SELECT user_id, MIN(order_date) AS first_order
  FROM orders
  GROUP BY user_id
)
SELECT e.user_id, e.email_sent_date, f.first_order
FROM email_events e
JOIN first_purchase f 
  ON e.user_id = f.user_id 
WHERE f.first_order > e.email_sent_date

Are you ready to revolutionize your coding experience? With our AI-powered sql online compiler, users can instantly write, run, and test code. It’s designed to make your SQL coding journey smoother and more efficient. Give it a try and unleash your programming potential today!

Conclusion

Completing the ‘SQL Interview Questions Blog for 2025’ can give life-changing insights into how programmers solve real-world problems. Imagine the satisfaction in cracking those tough questions! Dive in, and give it a go – conquer the coding world! If you’re keen to explore more about different programming languages like Java, Python, or C++, then head on over to Newtum for a treasure trove of knowledge!

Edited and Compiled by

This article was compiled and edited by @rasikadeshpande, who has over 4 years of experience in writing. She’s passionate about helping beginners understand technical topics in a more interactive way.

About The Author