Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views7 pages

SQL Interview Questions

The document lists 50 important SQL query questions commonly asked in interviews, categorized into basic SQL queries, SQL functions and aggregation, joins, subqueries and nested queries, and advanced querying techniques. Each question is accompanied by an example SQL statement to illustrate its use. The content serves as a comprehensive guide for preparing for SQL-related job interviews.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

SQL Interview Questions

The document lists 50 important SQL query questions commonly asked in interviews, categorized into basic SQL queries, SQL functions and aggregation, joins, subqueries and nested queries, and advanced querying techniques. Each question is accompanied by an example SQL statement to illustrate its use. The content serves as a comprehensive guide for preparing for SQL-related job interviews.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

50 Important SQL Query Questions for Interview (with Examples)

Basic SQL Query Questions

1. Fetch all records from a table:

SELECT * FROM employees;

2. Select specific columns:

SELECT name, salary FROM employees;

3. Filter records using WHERE clause:

SELECT * FROM employees WHERE salary > 50000;

4. Select unique values:

SELECT DISTINCT department FROM employees;

5. Sort data using ORDER BY:

SELECT * FROM employees ORDER BY salary DESC;

6. Use multiple conditions:

SELECT * FROM employees WHERE department = 'IT' AND salary > 40000;

7. LIKE vs =:

SELECT * FROM employees WHERE name LIKE 'Ravi%';

1
8. Pattern matching with wildcards:

SELECT * FROM employees WHERE email LIKE '%@gmail.com';

9. Count number of rows:

SELECT COUNT(*) FROM employees;

10. Aggregate functions:

SELECT AVG(salary), MIN(salary), MAX(salary) FROM employees;

SQL Functions & Aggregation

11. GROUP BY usage:

SELECT department, COUNT(*) FROM employees GROUP BY department;

12. Aggregate with GROUP BY:

SELECT department, SUM(salary) FROM employees GROUP BY department;

13. GROUP BY vs HAVING:

SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*)


> 5;

14. HAVING clause filter:

SELECT department, AVG(salary) FROM employees GROUP BY department HAVING


AVG(salary) > 50000;

15. NULL handling:

2
SELECT * FROM employees WHERE manager_id IS NULL;

16. Use COALESCE():

SELECT name, COALESCE(manager_id, 'Not Assigned') FROM employees;

17. IS NULL vs = NULL: - IS NULL is valid. = NULL is invalid in SQL.

18. Count per department:

SELECT department, COUNT(*) FROM employees GROUP BY department;

19. Highest salary:

SELECT MAX(salary) FROM employees;

20. Second highest salary:

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

Joins

21. INNER JOIN example:

SELECT e.name, d.name FROM employees e INNER JOIN departments d ON e.dept_id =


d.id;

22. INNER vs LEFT JOIN: - LEFT JOIN includes unmatched rows from the left table.

23. LEFT JOIN unmatched:

SELECT e.name FROM employees e LEFT JOIN departments d ON e.dept_id = d.id


WHERE d.id IS NULL;

3
24. RIGHT JOIN:

SELECT e.name, d.name FROM employees e RIGHT JOIN departments d ON e.dept_id =


d.id;

25. FULL JOIN:

SELECT * FROM employees e FULL OUTER JOIN departments d ON e.dept_id = d.id;

26. SELF JOIN:

SELECT e.name, m.name as manager FROM employees e JOIN employees m ON


e.manager_id = m.id;

27. Join 3 tables:

SELECT * FROM a JOIN b ON a.id = b.a_id JOIN c ON b.id = c.b_id;

28. Manager names: (Refer to Self Join example)

29. ON vs USING: - ON gives more flexibility. USING simplifies when column names are same.

30. Multiple joins: As shown above.

Subqueries & Nested Queries

31. Subquery in WHERE:

SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

32. Subquery in FROM:

SELECT dept, avg_sal FROM (SELECT department as dept, AVG(salary) as avg_sal


FROM employees GROUP BY department);

33. Correlated Subquery:

4
SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM
employees WHERE department = e.department);

34. Difference: - Correlated uses values from outer query.

35. Above avg salary: As shown above.

36. Max salary per department:

SELECT * FROM employees e WHERE salary = (SELECT MAX(salary) FROM employees


WHERE department = e.department);

37. EXISTS example:

SELECT name FROM employees e WHERE EXISTS (SELECT 1 FROM departments d WHERE
e.dept_id = d.id);

38. IN / NOT IN:

SELECT * FROM employees WHERE department IN ('IT', 'HR');

Advanced Querying

39. Delete duplicates:

DELETE FROM employees WHERE id NOT IN (SELECT MIN(id) FROM employees GROUP BY
name, salary);

40. Nth highest salary:

SELECT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) rnk
FROM employees) temp WHERE rnk = 3;

41. Conditional update:

5
UPDATE employees SET salary = salary + 1000 WHERE department = 'HR';

42. Window functions:

SELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM employees;

43. Running total:

SELECT name, salary, SUM(salary) OVER (ORDER BY name) as running_total FROM


employees;

44. PARTITION BY example:

SELECT name, department, RANK() OVER (PARTITION BY department ORDER BY salary


DESC) FROM employees;

45. UNION vs UNION ALL:

SELECT name FROM emp1 UNION SELECT name FROM emp2;

46. Pivot (with CASE):

SELECT id,
MAX(CASE WHEN month = 'Jan' THEN revenue END) AS Jan,
MAX(CASE WHEN month = 'Feb' THEN revenue END) AS Feb
FROM sales GROUP BY id;

47. Transpose rows: (Use CASE + GROUP BY technique)

48. LIMIT / TOP:

SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

49. Employees without department:

6
SELECT * FROM employees WHERE dept_id IS NULL;

50. Subquery insert/update/delete:

UPDATE employees SET dept_id = (SELECT id FROM departments WHERE name = 'IT')
WHERE name = 'Ravi';

Let me know if you want a practice sheet or quiz version of this.

You might also like