15 Essential SQL Interview Questions for Testers

Master the most critical SQL queries, JOIN operations, subqueries, and database validation scenarios asked in QA Automation and Database Testing interviews.

SQL Query Execution Order (Order of Operations)

Database engines execute SQL clauses in a specific logical order — not in the order they are written. Keep this sequence in mind during technical interviews:

1 FROM / JOINSpecify source tables & links 2 WHEREFilter individual raw rows 3 GROUP BYPartition rows into groups 4 HAVINGFilter aggregated groups 5 SELECTGenerate result columns 6 DISTINCTRemove duplicate output rows 7 ORDER BYSort final display rows 8 LIMIT / OFFSETTruncate returned rows count

SQL JOIN Types Explained with Examples

Understanding JOINs is critical for API and backend testing. INNER JOIN returns matching records from both tables. LEFT JOIN returns all records from the left table and matched records from the right. Testers frequently use these to validate complex data mappings between user accounts and transaction histories.

Advanced SQL Interview Questions with Answers

Senior QA interviews focus heavily on subqueries, correlated subqueries, and window functions (like ROW_NUMBER and RANK). Interviewers often ask you to find the "nth highest salary" or identify duplicate records—tasks that prove you can handle complex database testing scenarios.

Top 15 SQL Interview Questions with Tested Solutions

Click any question below to inspect query syntax, execution rationale, and QA verification checkpoints.

Subquery & Window Function

1. How to find the 2nd Highest Salary in an Employee table?

GROUP BY & HAVING

2. How to find and count duplicate records in a table?

CTE & Data Cleanup

3. How to delete duplicate rows while retaining one original copy?

Self Join

4. Write a query to find employees who earn more than their direct managers.

LEFT JOIN & NULL

5. How to find departments that have NO employees assigned?

Partitioning & Ranking

6. Find the top 2 highest-paid employees in EACH department.

Set Operations & INTERSECT

7. Find customers who placed orders in BOTH 2025 and 2026.

Window Function & Cumulative

8. How to calculate a running total (cumulative sum) of revenue per day?

Data Reconciliation

9. How to identify records that exist in Source Table A but NOT in Target Table B?

Clauses & Filtering

10. What is the key difference between WHERE and HAVING clause?

UNION vs UNION ALL

11. What is the difference between UNION and UNION ALL? What is the performance impact?

Sequence & Ranking

12. How to fetch the 3rd earliest order date for each customer?

Pivot & Conditional Aggregation

13. How to convert rows into columns (Pivot Table) using standard SQL?

Three-Valued Logic & NULL

14. How to properly test for NULL values, and why does 'WHERE status = NULL' fail?

QA Automation & Data Integrity

15. How do QA engineers validate data integrity between Staging and Production databases?

SQL Practice Tips for QA Engineers

The best way to master database testing is through hands-on practice. Use our SQL Practice Sandbox to write queries against a live sample database. Practice finding orphans, checking referential integrity, and validating data migrations.

How to Write Clean SQL in QA Interviews: Expert Tips

After more than 12 years conducting and sitting on both sides of QA technical interviews, I can tell you that writing correct SQL is only half the battle. Interviewers at companies like Accenture, Deloitte, and Persistent Systems are equally evaluating how you write your SQL — your logical approach, your readability habits, and your understanding of performance implications. The eight tips below are the exact habits I look for when assessing a candidate's database testing maturity.

Common SQL Mistakes in QA Interviews: Pitfalls and Fixes

Even experienced testers make predictable SQL mistakes under interview pressure. Recognizing these patterns in advance — and knowing the corrected approach — gives you a significant edge. Here are the five most common pitfalls I see candidates make, along with precise fixes.

❌ Pitfall 1: Using WHERE instead of HAVING after GROUP BY

Wrong: SELECT department_id, COUNT(*) FROM employees GROUP BY department_id WHERE COUNT(*) > 5; — This throws a syntax error because WHERE cannot reference aggregate functions. Fix: Replace WHERE with HAVING: GROUP BY department_id HAVING COUNT(*) > 5;

❌ Pitfall 2: Confusing INNER JOIN with LEFT JOIN when nulls matter

When asked to find departments with no employees, candidates often write an INNER JOIN which silently excludes null matches. The correct approach is a LEFT JOIN followed by a WHERE e.id IS NULL condition to capture unmatched parent records.

❌ Pitfall 3: Forgetting to handle ties in ranking queries

Using a simple LIMIT 1 OFFSET 1 to find the second highest salary breaks immediately when two employees share the top salary. Always use DENSE_RANK() which correctly handles ties, or use the MAX/WHERE subquery pattern shown in Question 1 above.

❌ Pitfall 4: Not qualifying column names in multi-table queries

When joining two tables that share a column name (like id or name), always prefix columns with the table alias: e.name, m.name. Unqualified column references in multi-table queries produce ambiguous column errors that are easy to avoid and make you look careless.

❌ Pitfall 5: Writing inefficient correlated subqueries in the SELECT clause

Placing a subquery directly in the SELECT clause (e.g., to fetch a count per row) executes that subquery once per row in the outer query — a classic N+1 problem. The fix is to pre-aggregate in a CTE or derived table and then JOIN the result, which processes the aggregation only once regardless of dataset size.

Avoiding these five pitfalls will immediately elevate the quality of your SQL in any interview. Practice writing queries under time pressure in our SQL Practice Lab to build the muscle memory to produce clean, correct SQL on demand.

RD

Written by Rammehar Dhiman

Senior QA Automation Engineer • 12+ Years Experience • Pune, India

Rammehar has led QA automation across banking, e-commerce, and enterprise SaaS. He specialises in Selenium, Playwright, API testing, and CI/CD quality frameworks. He created Ram Technical Help to provide free, expert-level QA education to aspiring testers worldwide.

View full profile →