Home / Blog / 40 SQL Interview Questions for QA Engineers
40 SQL Interview Questions for QA Engineers
SQL questions appear in almost every QA and SDET interview. Interviewers are not testing your ability to design databases — they are testing whether you can independently validate test data and write queries to investigate defects.
These 40 questions reflect what actually gets asked. Each answer is written at the right depth for a QA interview.
Basic SELECT and Filtering
1. Write a query to fetch all records from a users table.
SELECT * FROM users;In practice, always specify columns rather than * in production code — it is faster and clearer. For ad-hoc test queries, * is fine.
2. Write a query to fetch only the name and email columns from users.
SELECT name, email FROM users;3. How do you filter records with a WHERE clause?
-- Get all active users
SELECT id, name, email
FROM users
WHERE is_active = 1;
-- Get all orders with status 'pending'
SELECT id, user_id, total_amount
FROM orders
WHERE status = 'pending';4. How do you use AND and OR in SQL?
-- Active users registered this year
SELECT name, email
FROM users
WHERE is_active = 1 AND YEAR(created_at) = 2026;
-- Orders that are cancelled or refunded
SELECT id, status
FROM orders
WHERE status = 'cancelled' OR status = 'refunded';5. How do you sort query results?
-- Most recent orders first
SELECT id, status, created_at
FROM orders
ORDER BY created_at DESC;
-- Alphabetical by name
SELECT name, email FROM users ORDER BY name ASC;6. How do you limit the number of results returned?
-- Get the 5 most recent orders
SELECT id, status, created_at
FROM orders
ORDER BY created_at DESC
LIMIT 5;
-- Page 2 of results (offset 10, fetch next 10)
SELECT id, name FROM products
ORDER BY id
LIMIT 10 OFFSET 10;QA use case: Verifying pagination API endpoints return the correct data slice.
7. What is the DISTINCT keyword?
Returns unique values, removing duplicates.
-- All unique order statuses currently in the system
SELECT DISTINCT status FROM orders;
-- All unique categories of products
SELECT DISTINCT category FROM products;QA use case: Checking that a feature has not introduced unexpected status values.
8. How do you use the LIKE operator?
-- Users with Gmail addresses
SELECT name, email FROM users WHERE email LIKE '%@gmail.com';
-- Products with 'phone' anywhere in the name
SELECT name, price FROM products WHERE name LIKE '%phone%';
-- Products whose name starts with 'Apple'
SELECT name FROM products WHERE name LIKE 'Apple%';9. How do you use the IN operator?
-- Orders with specific statuses
SELECT id, status FROM orders
WHERE status IN ('pending', 'processing', 'on_hold');
-- Specific users by ID
SELECT name FROM users WHERE id IN (1, 5, 10, 42);10. How do you use BETWEEN?
-- Orders placed in March 2026
SELECT id, total_amount FROM orders
WHERE created_at BETWEEN '2026-03-01' AND '2026-03-31 23:59:59';
-- Products priced between ₹500 and ₹2000
SELECT name, price FROM products
WHERE price BETWEEN 500 AND 2000;NULL Handling
11. How do you check for NULL values?
-- Users who have not provided a phone number
SELECT id, name FROM users WHERE phone IS NULL;
-- Users who have provided a phone number
SELECT id, name, phone FROM users WHERE phone IS NOT NULL;WHERE phone = NULL does not work. NULL is not equal to anything, including itself. Always use IS NULL or IS NOT NULL.
12. What is the COALESCE function?
Returns the first non-NULL value in a list. Useful for handling NULL in query output:
-- Show "Not provided" instead of NULL for missing phone
SELECT name, COALESCE(phone, 'Not provided') AS phone
FROM users;13. A QA scenario: How do you find all test records where a required field was not saved?
SELECT id, name, created_at
FROM orders
WHERE shipping_address IS NULL;If shipping address should be mandatory but some records have NULL, there is a data bug.
Aggregate Functions
14. What are aggregate functions? Give examples.
Functions that compute a single result from multiple rows: COUNT, SUM, AVG, MIN, MAX.
SELECT COUNT(*) AS total_users FROM users;
SELECT SUM(total_amount) AS revenue FROM orders WHERE status = 'completed';
SELECT AVG(total_amount) AS avg_order_value FROM orders;
SELECT MIN(price) AS cheapest FROM products;
SELECT MAX(price) AS most_expensive FROM products;15. How do you use GROUP BY?
Groups rows sharing a common value so aggregate functions apply per group:
-- Count orders by status
SELECT status, COUNT(*) AS count FROM orders GROUP BY status;
-- Total spent per user
SELECT user_id, SUM(total_amount) AS total_spent
FROM orders
WHERE status = 'completed'
GROUP BY user_id
ORDER BY total_spent DESC;16. What is the difference between WHERE and HAVING?
WHERE filters rows before aggregation. HAVING filters groups after aggregation.
-- WRONG: Cannot use aggregate in WHERE
SELECT user_id, COUNT(*) FROM orders
WHERE COUNT(*) > 5 -- Error!
GROUP BY user_id;
-- CORRECT: Use HAVING for post-aggregation filter
SELECT user_id, COUNT(*) AS order_count
FROM orders
GROUP BY user_id
HAVING order_count > 5;17. Write a query to find users who have placed more than 3 orders.
SELECT user_id, COUNT(*) AS order_count
FROM orders
GROUP BY user_id
HAVING order_count > 3;JOINs
18. What is an INNER JOIN?
Returns only rows where there is a match in both tables.
-- Get orders with customer name
SELECT o.id AS order_id, u.name, u.email, o.status
FROM orders o
INNER JOIN users u ON o.user_id = u.id;19. What is a LEFT JOIN?
Returns all rows from the left table, and matching rows from the right table. Non-matching rows from the right get NULL.
-- All users with their order count (including users with no orders)
SELECT u.id, u.name, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.name;QA use case: Verify a newly created user has zero orders.
20. What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN excludes rows with no match. LEFT JOIN includes all rows from the left table, with NULLs where no match exists.
If a user has no orders: INNER JOIN omits that user. LEFT JOIN includes the user with order_count = 0.
21. Write a 3-table JOIN to get order details with user name and product names.
SELECT
u.name AS customer,
o.id AS order_id,
o.status,
p.name AS product,
oi.quantity,
oi.unit_price
FROM orders o
INNER JOIN users u ON o.user_id = u.id
INNER JOIN order_items oi ON o.id = oi.order_id
INNER JOIN products p ON oi.product_id = p.id
WHERE o.id = 1001;22. What is a SELF JOIN?
Joining a table to itself. Used for hierarchical data like employee-manager relationships:
SELECT
e.name AS employee,
m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;Subqueries
23. What is a subquery?
A SELECT statement nested inside another SELECT, WHERE, or FROM clause.
-- Users who have placed at least one order
SELECT name, email FROM users
WHERE id IN (SELECT DISTINCT user_id FROM orders);
-- Products more expensive than the average price
SELECT name, price FROM products
WHERE price > (SELECT AVG(price) FROM products);24. What is the difference between a correlated and non-correlated subquery?
A non-correlated subquery runs once and its result is used by the outer query.
A correlated subquery references the outer query and re-runs for each row:
-- Correlated: find users whose latest order was over ₹1000
SELECT u.name
FROM users u
WHERE (
SELECT MAX(o.total_amount)
FROM orders o
WHERE o.user_id = u.id -- References outer query
) > 1000;Data Validation Queries for QA
25. How do you find duplicate records?
-- Find duplicate emails
SELECT email, COUNT(*) AS count
FROM users
GROUP BY email
HAVING count > 1;QA use case: After a bulk import test, verify there are no duplicate entries.
26. How do you verify a record was saved correctly after an API call?
-- Check the most recent order for user 42
SELECT id, status, total_amount, created_at
FROM orders
WHERE user_id = 42
ORDER BY created_at DESC
LIMIT 1;27. How do you check that a foreign key constraint is maintained?
-- Find order_items that reference non-existent orders (orphaned records)
SELECT oi.id, oi.order_id
FROM order_items oi
LEFT JOIN orders o ON oi.order_id = o.id
WHERE o.id IS NULL;If this returns rows, there are orphaned order_items — a data integrity bug.
28. How do you verify data was deleted correctly?
-- Verify the user account no longer exists
SELECT COUNT(*) FROM users WHERE id = 42;
-- Expected: 029. How do you verify stock was decremented after a purchase?
Run the query before and after the purchase:
SELECT stock_quantity FROM products WHERE id = 10;Before: 50. After purchasing 3 units, expected: 47.
30. How do you verify the total order amount matches line items?
SELECT
o.id,
o.total_amount AS stored_total,
SUM(oi.quantity * oi.unit_price) AS calculated_total,
o.total_amount - SUM(oi.quantity * oi.unit_price) AS difference
FROM orders o
INNER JOIN order_items oi ON o.id = oi.order_id
WHERE o.id = 1001
GROUP BY o.id, o.total_amount;If difference is non-zero, the order total calculation is buggy.
UPDATE and DELETE
31. How do you update a record? What precaution must you take?
UPDATE products SET stock_quantity = 100 WHERE id = 50;Always include a WHERE clause. UPDATE products SET stock_quantity = 100 without WHERE updates every row in the table.
Best practice: run a SELECT first to confirm what you are about to update:
SELECT id, stock_quantity FROM products WHERE id = 50;
-- Verify one row. Then run the UPDATE.32. How do you safely delete test data?
-- Check first
SELECT id, user_id, created_at FROM orders
WHERE user_id = 999 AND created_at > '2026-04-01';
-- Then delete
DELETE FROM orders
WHERE user_id = 999 AND created_at > '2026-04-01';Never run DELETE FROM orders without a WHERE clause.
Scenario Questions
33. After a registration test creates 100 users, how do you verify all were saved?
SELECT COUNT(*)
FROM users
WHERE created_at >= '2026-04-01 10:00:00';
-- Expected: 10034. How do you find the second-highest salary in an employee table?
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);35. How do you find all orders placed in the last 7 days?
SELECT id, user_id, total_amount, created_at
FROM orders
WHERE created_at >= NOW() - INTERVAL 7 DAY
ORDER BY created_at DESC;36. How do you find products that have never been ordered?
SELECT p.name, p.price
FROM products p
LEFT JOIN order_items oi ON p.id = oi.product_id
WHERE oi.product_id IS NULL;37. How do you verify an email address format in SQL?
SELECT id, email
FROM users
WHERE email NOT LIKE '%@%.%';Returns rows where the email does not match a basic format — missing @ or no domain dot.
38. How do you get the count of orders placed per day for the last week?
SELECT DATE(created_at) AS order_date, COUNT(*) AS order_count
FROM orders
WHERE created_at >= NOW() - INTERVAL 7 DAY
GROUP BY DATE(created_at)
ORDER BY order_date DESC;39. How do you find users who have not placed any orders?
SELECT u.id, u.name, u.email
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE o.id IS NULL;40. How do you write a query to check for data inconsistency — orders with a total that doesn't match the sum of items?
SELECT
o.id,
o.total_amount,
SUM(oi.quantity * oi.unit_price) AS calculated_total
FROM orders o
INNER JOIN order_items oi ON o.id = oi.order_id
GROUP BY o.id, o.total_amount
HAVING o.total_amount != SUM(oi.quantity * oi.unit_price);Any rows returned are orders where the stored total does not match the line item calculation — a financial calculation bug.
Interview Tips for SQL Questions
1. Think out loud. Interviewers want to see your reasoning process. Say "I need to join orders to users to get the customer name, so I'll use an INNER JOIN on user_id..."
2. Ask about the schema. If the question is vague ("how would you check for data issues?"), ask what tables and columns are relevant before writing the query.
3. Offer to verify with a SELECT first. For UPDATE/DELETE questions, always mention you would run a SELECT with the same WHERE clause first to confirm the affected rows.
4. Know the difference between WHERE and HAVING cold. This question appears in 80% of SQL interviews for QA roles.
5. Practice on a real database. Everything in this guide is testable on sqliteonline.com in the browser. Create the sample tables and run the queries. Reading SQL is not the same as writing it.
Recommended Resource
QA Interview Kit
Interview prep kit with real-world QA and API scenarios.
Related Posts
Behavioral Interview Questions for QA Engineers (With Sample Answers)
Behavioral interview questions specifically for QA and SDET roles — with the STAR method explained and sample answers that actually sound human.
Read article →QA Interview Preparation Checklist: Everything You Need to Know
A complete QA interview preparation checklist — what to study, what to practice, and how to approach each round of a QA or SDET interview.
Read article →30 Performance Testing Interview Questions and Answers
Performance testing interview questions for QA roles — covering load testing, stress testing, JMeter, key metrics, and how to analyze results.
Read article →