SQL Part1 (Basics)
SQL Part1 (Basics)
2. Explain the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN,
and FULL OUTER JOIN.
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);
NULL is unknown, so comparisons like =, <> with NULL return FALSE. Use IS NULL or IS
NOT NULL.
SELECT * FROM students WHERE marks > ALL (SELECT marks FROM students
WHERE class = ‘B');
Use it when you want to aggregate data grouped by one or more columns.
11. What is the use of the IN, ANY, ALL, and EXISTS operators?
SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM
employees);
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 NOT IN (SELECT dept_id FROM
departments WHERE name IN ('Sales', ‘Marketing'));
22. Find all employees whose salary is equal to the maximum salary in the
company.
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);
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);
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;
31. Find departments where no employee has salary more than 30,000.
35. Find average salary of employees per department having at least one
employee.
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;
40. Find the total number of NULL values in the bonus column.
Let me know if you want the same as a downloadable file (PDF, DOCX), or if you'd like practice
exercises with mock tables.