SQL Interview Questions and Answers
Q: How do you identify duplicate records in a table?
A: SELECT column1, column2, COUNT(*) FROM table_name GROUP BY column1, column2
HAVING COUNT(*) > 1;
Q: How can you delete duplicates while keeping just one entry?
A: DELETE FROM table_name WHERE id NOT IN (SELECT MIN(id) FROM table_name GROUP
BY column1, column2);
Q: What's the difference between UNION and UNION ALL?
A: UNION removes duplicate rows, while UNION ALL includes all rows including duplicates.
Q: When should you use RANK, ROW_NUMBER, and DENSE_RANK?
A: ROW_NUMBER() assigns unique row numbers. RANK() gives the same rank to duplicates but
skips the next. DENSE_RANK() assigns the same rank to duplicates without skipping numbers.
Q: How do you find records in one table that dont exist in another?
A: SELECT * FROM table1 WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table1.id =
table2.id);
Q: How do you retrieve the second-highest salary for each department?
A: SELECT department, salary FROM (SELECT department, salary, DENSE_RANK() OVER
(PARTITION BY department ORDER BY salary DESC) AS rnk FROM employees) AS ranked
WHERE rnk = 2;
Q: How do you find employees earning more than their manager?
A: SELECT e.name, e.salary FROM employees e JOIN employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;
Q: What's the key difference between INNER JOIN and LEFT JOIN?
A: INNER JOIN returns only matching records, while LEFT JOIN returns all records from the left
table and matching records from the right table.
Q: How would you swap gender values (M F) in a table using SQL?
A: UPDATE employees SET gender = CASE WHEN gender = 'M' THEN 'F' WHEN gender = 'F'
THEN 'M' END;
Q: How do different types of joins impact the number of records in the output?
A: INNER JOIN: Only common records.
LEFT JOIN: All left records, NULLs for missing right.
RIGHT JOIN: All right records, NULLs for missing left.
FULL OUTER JOIN: All records from both tables.
Q: How do you retrieve the nth highest salary from a table?
A: SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET n-1;
Q: Whats the difference between HAVING and WHERE in SQL?
A: WHERE filters records before aggregation, while HAVING filters records after aggregation.
Q: How do you pivot rows into columns in SQL?
A: SELECT department, SUM(CASE WHEN role = 'Manager' THEN salary END) AS
Manager_Salary, SUM(CASE WHEN role = 'Engineer' THEN salary END) AS Engineer_Salary
FROM employees GROUP BY department;
Q: How do you calculate a running total in SQL?
A: SELECT name, salary, SUM(salary) OVER (ORDER BY id) AS running_total FROM employees;
Q: How would you identify and handle orphan records in a database?
A: SELECT * FROM child_table WHERE parent_id NOT IN (SELECT id FROM parent_table);
Q: Whats the difference between DELETE, TRUNCATE, and DROP?
A: DELETE removes specific rows with WHERE. TRUNCATE removes all rows without logging
transactions. DROP removes the entire table.
Q: How do you find the longest consecutive sequence of values in a column?
A: SELECT value, COUNT(*) FROM (SELECT value, value - ROW_NUMBER() OVER (ORDER BY
value) AS grp FROM table_name) AS subquery GROUP BY grp ORDER BY COUNT(*) DESC
LIMIT 1;
Q: What is a self-join, and when would you use it?
A: A self-join is when a table joins itself.
Example: SELECT e1.name, e2.name AS colleague FROM employees e1 JOIN employees e2 ON
e1.manager_id = e2.id;
Q: How do you find the earliest and latest records for each group in a table?
A: SELECT department, MIN(join_date) AS earliest, MAX(join_date) AS latest FROM employees
GROUP BY department;
Q: How do you optimize SQL queries for better performance?
A: - Use indexes
- Avoid SELECT *
- Use EXPLAIN ANALYZE
- Normalize/denormalize appropriately
- Use partitioning and indexing.