10 Most Asked SQL Interview Questions 2025
These are asked at TCS, Infosys, Wipro, Amazon, Flipkart, and Razorpay. Study all of them.
Q1: Difference between WHERE and HAVING?
WHERE filters individual rows before aggregation — cannot use aggregate functions. HAVING filters groups after aggregation — can use COUNT, SUM, AVG.
-- CORRECT
SELECT dept, COUNT(*) AS cnt FROM employees GROUP BY dept HAVING cnt > 5;
Q2: How to check for NULL?
NULL is not equal to NULL. Always use IS NULL or IS NOT NULL. COALESCE(col, default) replaces NULL with a value.
Q3: UNION vs UNION ALL?
UNION removes duplicates (sorts result — slower). UNION ALL keeps all rows including duplicates (faster). Use UNION ALL unless you need deduplication.
Q4: Find second highest salary.
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
-- Or:
SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;
Q5: Find duplicate emails.
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;
Q6: Delete duplicates keeping one.
DELETE FROM users WHERE id NOT IN (SELECT MIN(id) FROM users GROUP BY email);
Q7: Employees earning above department average.
SELECT e.name, e.salary FROM employees e
JOIN (SELECT dept_id, AVG(salary) AS avg FROM employees GROUP BY dept_id) d
ON d.dept_id = e.dept_id WHERE e.salary > d.avg;
Q8: What is a self join?
A table joining itself. Classic use: employee-manager hierarchy where both are in the same table.
Q9: What is a stored procedure?
Precompiled SQL code stored in the database, called with CALL. Can have IN/OUT parameters, control flow (IF/LOOP), and transactions. Reduces network round-trips.
Q10: What is ACID?
Atomicity (all or nothing), Consistency (valid state), Isolation (concurrent safety), Durability (survives crashes). These are the properties guaranteed by database transactions.
Comments (0)
No comments yet. Be the first!
Leave a Comment