SQL BASICS
1. What is the difference between WHERE and HAVING clause in SQL?
• WHERE filters rows before grouping.
• HAVING filters groups after aggregation.
2. Explain the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN,
and FULL OUTER JOIN.
• INNER JOIN: returns matching rows.
• LEFT JOIN: all from left + matched from right.
• RIGHT JOIN: all from right + matched from left.
• FULL OUTER JOIN: all rows from both sides.
3. What is a nested query in SQL? Give an example use case.
A nested query (subquery) is a query inside another query.
Example:
SELECT name FROM employees WHERE dept_id = (SELECT dept_id FROM
departments WHERE name = ‘HR');
4. What is a correlated subquery? How does it differ from a nested query?
A correlated subquery refers to a column from the outer query and is executed for each row.
Example:
SELECT name FROM employees e WHERE salary > (SELECT AVG(salary) FROM
employees WHERE dept_id = e.dept_id);
5. What is the purpose of aggregate functions in SQL? Name a few.
They perform calculations on multiple values:
• SUM(), COUNT(), AVG(), MIN(), MAX().
6. Explain how SQL handles NULL values in comparison operations.
NULL is unknown, so comparisons like =, <> with NULL return FALSE. Use IS NULL or IS
NOT NULL.
7. What are the set comparison operators in SQL? Give examples.
• IN, NOT IN, ANY, ALL, EXISTS.
Example:
SELECT * FROM students WHERE marks > ALL (SELECT marks FROM students
WHERE class = ‘B');
8. What is the difference between COUNT(*), COUNT(column_name), and
COUNT(DISTINCT column_name)?
• COUNT(*): counts all rows.
• COUNT(column): counts non-NULL values.
• COUNT(DISTINCT column): counts unique non-NULL values.
9. When should you use a GROUP BY clause in SQL?
Use it when you want to aggregate data grouped by one or more columns.
10. Can NULL = NULL return true in SQL? Explain.
No, it returns UNKNOWN. Use IS NULL instead.
11. What is the use of the IN, ANY, ALL, and EXISTS operators?
• IN: matches any value in a list.
• ANY: true if any subquery value matches.
• ALL: true if all subquery values match.
• EXISTS: true if subquery returns any rows.
12. What is the difference between scalar and multi-row subqueries?
• Scalar subquery: returns one value.
• Multi-row subquery: returns multiple rows.
13. Get names of employees in department 'HR'.
SELECT name FROM employees WHERE dept_id = (SELECT dept_id FROM
departments WHERE name = ‘HR');
14. Find employees with salary greater than average salary.
SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM
employees);
15. List departments with more than 5 employees.
SELECT dept_id FROM employees GROUP BY dept_id HAVING COUNT(*) > 5;
16. Find employees whose salary is greater than all employees in department 2.
SELECT name FROM employees WHERE salary > ALL (SELECT salary FROM
employees WHERE dept_id = 2);
17. Get names of employees who work in departments with budget > 1M.
SELECT name FROM employees WHERE dept_id IN (SELECT dept_id FROM
departments WHERE budget > 1000000);
18. Find employees who do not work in 'Sales' or 'Marketing'.
SELECT name FROM employees WHERE dept_id NOT IN (SELECT dept_id FROM
departments WHERE name IN ('Sales', ‘Marketing'));
19. Find the department with the highest average salary.
SELECT dept_id FROM employees GROUP BY dept_id ORDER BY AVG(salary) DESC
LIMIT 1;
20. Find employees with duplicate names.
SELECT name FROM employees GROUP BY name HAVING COUNT(*) > 1;
21. Find departments that have no employees.
SELECT name FROM departments WHERE dept_id NOT IN (SELECT DISTINCT
dept_id FROM employees);
22. Find all employees whose salary is equal to the maximum salary in the
company.
SELECT name FROM employees WHERE salary = (SELECT MAX(salary) FROM
employees);
23. List employees who earn more than their department’s average salary
(correlated).
SELECT name FROM employees e WHERE salary > (SELECT AVG(salary) FROM
employees WHERE dept_id = e.dept_id);
24. Find employees having NULL manager ID.
SELECT name FROM employees WHERE manager_id IS NULL;
25. Get total salary paid in each department.
SELECT dept_id, SUM(salary) AS total_salary FROM employees GROUP BY dept_id;
26. List employee names and their department names.
SELECT e.name, d.name FROM employees e JOIN departments d ON e.dept_id =
d.dept_id;
27. Find employees whose salary is greater than any employee in department 1.
SELECT name FROM employees WHERE salary > ANY (SELECT salary FROM
employees WHERE dept_id = 1);
28. Find employees who do not have a manager.
SELECT name FROM employees WHERE manager_id IS NULL;
29. Find all departments where the average salary is more than 60,000.
SELECT dept_id FROM employees GROUP BY dept_id HAVING AVG(salary) > 60000;
30. Get number of employees in each department.
SELECT dept_id, COUNT(*) AS emp_count FROM employees GROUP BY dept_id;
31. Find departments where no employee has salary more than 30,000.
SELECT dept_id FROM departments WHERE dept_id NOT IN (
SELECT DISTINCT dept_id FROM employees WHERE salary > 30000
);
32. Get employee(s) with the 2nd highest salary.
SELECT name FROM employees WHERE salary = (
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary)
FROM employees)
);
33. Get departments with exactly 3 employees.
SELECT dept_id FROM employees GROUP BY dept_id HAVING COUNT(*) = 3;
34. Get employees who joined in the current year.
SELECT name FROM employees WHERE YEAR(join_date) = YEAR(CURDATE());
35. Find average salary of employees per department having at least one
employee.
SELECT dept_id, AVG(salary) FROM employees GROUP BY dept_id;
36. Find employees working in more than one project.
SELECT emp_id FROM project_assignment GROUP BY emp_id HAVING
COUNT(DISTINCT project_id) > 1;
37. Get employees whose salary is NOT NULL and greater than 50000.
SELECT name FROM employees WHERE salary IS NOT NULL AND salary > 50000;
38. Find departments with all employees having salary > 20,000.
SELECT dept_id FROM employees GROUP BY dept_id HAVING MIN(salary) > 20000;
39. Find employees with no assigned project (use NOT EXISTS).
SELECT name FROM employees e WHERE NOT EXISTS (
SELECT 1 FROM project_assignment p WHERE p.emp_id = e.emp_id
);
40. Find the total number of NULL values in the bonus column.
SELECT COUNT(*) - COUNT(bonus) AS null_count FROM employees;
Let me know if you want the same as a downloadable file (PDF, DOCX), or if you'd like practice
exercises with mock tables.