SQL Practice Questions
Basic Queries (5)
1. Select all columns from the Employee table.
2. Select only the name and salary columns from the Employee table.
3. Select employees who are older than 30.
4. Select the names of all departments.
5. Select employees who work in the IT department.
String Matching Queries (5)
6. Select employees whose names start with ‘J’.
7. Select employees whose names end with ‘e’.
8. Select employees whose names contain ‘a’.
9. Select employees whose names are exactly 9 characters long.
10. Select employees whose names have 'o' as the second character.
Date Queries (5)
11. Select employees hired in the year 2020.
12. Select employees hired in January of any year.
13. Select employees hired before 2019.
14. Select employees hired on or after March 1, 2021.
15. Select employees hired in the last 2 years.
Aggregate Queries (5)
16. Select the total salary of all employees.
17. Select the average salary of employees.
18. Select the minimum salary in the Employee table.
19. Select the number of employees in each department.
20. Select the average salary of employees in each department.
Group By Queries (5)
21. Select the total salary for each department.
22. Select the average age of employees in each department.
23. Select the number of employees hired in each year.
24. Select the highest salary in each department.
25. Select the department with the highest average salary.
Having Queries (5)
26. Select departments with more than 2 employees.
27. Select departments with an average salary greater than 55000.
28. Select years with more than 1 employee hired.
29. Select departments with a total salary expense less than 100000.
30. Select departments with the maximum salary above 75000.
Order By Queries (5)
31. Select all employees ordered by their salary in ascending order.
32. Select all employees ordered by their age in descending order.
33. Select all employees ordered by their hire date in ascending order.
34. Select employees ordered by their department and then by their salary.
35. Select departments ordered by the total salary of their employees.
Join Queries (10)
36. Select employee names along with their department names.
37. Select project names along with the department names they belong to.
38. Select employee names and their corresponding project names.
39. Select all employees and their departments, including those without a department.
40. Select all departments and their employees, including departments without
employees.
41. Select employees who are not assigned to any project.
42. Select employees and the number of projects their department is working on.
43. Select the departments that have no employees.
44. Select employee names who share the same department with ‘John Doe’.
45. Select the department name with the highest average salary.
Nested and Correlated Queries (10)
46. Select the employee with the highest salary.
47. Select employees whose salary is above the average salary.
48. Select the second highest salary from the Employee table.
49. Select the department with the most employees.
50. Select employees who earn more than the average salary of their department.
51–55. (More questions continue, but the OCR cut off here — can re-run if needed)
answers
Basic Queries
Q1. Select all columns from the Employee table.
SELECT *
FROM Employee;
Q2. Select only the name and salary columns from the Employee table.
SELECT name, salary
FROM Employee;
Q3. Select employees who are older than 30.
SELECT *
FROM Employee
WHERE age > 30;
Q4. Select the names of all departments.
SELECT name
FROM Department;
Q5. Select employees who work in the IT department.
SELECT e.*
FROM Employee e
JOIN Department d ON e.department_id = d.department_id
WHERE [Link] = 'IT';
Q6. Select employees whose names start with ‘J’.
SELECT *
FROM Employee
WHERE name LIKE 'J%';
Q7. Select employees whose names end with ‘e’.
SELECT *
FROM Employee
WHERE name LIKE '%e';
Q8. Select employees whose names contain ‘a’.
SELECT *
FROM Employee
WHERE name LIKE '%a%';
Q9. Select employees whose names are exactly 9 characters long.
SELECT *
FROM Employee
WHERE LENGTH(name) = 9;
(In SQL Server use LEN(name) instead of LENGTH(name))
Q10. Select employees whose names have 'o' as the second character.
SELECT *
FROM Employee
WHERE name LIKE '_o%';
Date Queries
Q11. Select employees hired in the year 2020.
SELECT *
FROM Employee
WHERE YEAR(hire_date) = 2020;
(In Oracle use EXTRACT(YEAR FROM hire_date) = 2020)
Q12. Select employees hired in January of any year.
SELECT *
FROM Employee
WHERE MONTH(hire_date) = 1;
(In Oracle use EXTRACT(MONTH FROM hire_date) = 1)
Q13. Select employees hired before 2019.
SELECT *
FROM Employee
WHERE hire_date < '2019-01-01';
Q14. Select employees hired on or after March 1, 2021.
SELECT *
FROM Employee
WHERE hire_date >= '2021-03-01';
Q15. Select employees hired in the last 2 years.
SELECT *
FROM Employee
WHERE hire_date >= DATE_SUB(CURDATE(), INTERVAL 2 YEAR);
(In SQL Server use DATEADD(YEAR, -2, GETDATE()))
Aggregate Queries
Q16. Select the total salary of all employees.
SELECT SUM(salary) AS total_salary
FROM Employee;
Q17. Select the average salary of employees.
SELECT AVG(salary) AS average_salary
FROM Employee;
Q18. Select the minimum salary in the Employee table.
SELECT MIN(salary) AS minimum_salary
FROM Employee;
Q19. Select the number of employees in each department.
SELECT department_id, COUNT(*) AS employee_count
FROM Employee
GROUP BY department_id;
Q20. Select the average salary of employees in each department.
SELECT department_id, AVG(salary) AS avg_salary
FROM Employee
GROUP BY department_id;
Group By Queries
Q21. Select the total salary for each department.
SELECT department_id, SUM(salary) AS total_salary
FROM Employee
GROUP BY department_id;
Q22. Select the average age of employees in each department.
SELECT department_id, AVG(age) AS avg_age
FROM Employee
GROUP BY department_id;
Q23. Select the number of employees hired in each year.
SELECT YEAR(hire_date) AS hire_year, COUNT(*) AS employee_count
FROM Employee
GROUP BY YEAR(hire_date);
(In Oracle: EXTRACT(YEAR FROM hire_date) instead of YEAR(hire_date))
Q24. Select the highest salary in each department.
SELECT department_id, MAX(salary) AS highest_salary
FROM Employee
GROUP BY department_id;
Q25. Select the department with the highest average salary.
SELECT department_id
FROM Employee
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 1;
(In SQL Server use TOP 1 instead of LIMIT 1)
Having Queries
Q26. Select departments with more than 2 employees.
SELECT department_id, COUNT(*) AS employee_count
FROM Employee
GROUP BY department_id
HAVING COUNT(*) > 2;
Q27. Select departments with an average salary greater than 55000.
SELECT department_id, AVG(salary) AS avg_salary
FROM Employee
GROUP BY department_id
HAVING AVG(salary) > 55000;
Q28. Select years with more than 1 employee hired.
SELECT YEAR(hire_date) AS hire_year, COUNT(*) AS employee_count
FROM Employee
GROUP BY YEAR(hire_date)
HAVING COUNT(*) > 1;
Q29. Select departments with a total salary expense less than 100000.
SELECT department_id, SUM(salary) AS total_salary
FROM Employee
GROUP BY department_id
HAVING SUM(salary) < 100000;
Q30. Select departments with the maximum salary above 75000.
SELECT department_id, MAX(salary) AS max_salary
FROM Employee
GROUP BY department_id
HAVING MAX(salary) > 75000;
Order By Queries
Q31. Select all employees ordered by their salary in ascending order.
SELECT *
FROM Employee
ORDER BY salary ASC;
Q32. Select all employees ordered by their age in descending order.
SELECT *
FROM Employee
ORDER BY age DESC;
Q33. Select all employees ordered by their hire date in ascending order.
SELECT *
FROM Employee
ORDER BY hire_date ASC;
Q34. Select employees ordered by their department and then by their salary.
SELECT *
FROM Employee
ORDER BY department_id, salary;
Q35. Select departments ordered by the total salary of their employees.
SELECT department_id, SUM(salary) AS total_salary
FROM Employee
GROUP BY department_id
ORDER BY total_salary DESC;
Join Queries
Q36. Select employee names along with their department names.
SELECT [Link] AS employee_name, [Link] AS department_name
FROM Employee e
JOIN Department d ON e.department_id = d.department_id;
Q37. Select project names along with the department names they belong to.
SELECT [Link] AS project_name, [Link] AS department_name
FROM Project p
JOIN Department d ON p.department_id = d.department_id;
(Assuming Project table has a department_id column — if not, we’ll need a mapping table.)
Q38. Select employee names and their corresponding project names.
SELECT [Link] AS employee_name, [Link] AS project_name
FROM Employee e
JOIN ProjectAssignment pa ON e.emp_id = pa.emp_id
JOIN Project p ON pa.project_id = p.project_id;
(Here ProjectAssignment is assumed as a linking table between Employee and Project.)
Q39. Select all employees and their departments, including those without a department.
SELECT [Link] AS employee_name, [Link] AS department_name
FROM Employee e
LEFT JOIN Department d ON e.department_id = d.department_id;
Q40. Select all departments and their employees, including departments without
employees.
SELECT [Link] AS department_name, [Link] AS employee_name
FROM Department d
LEFT JOIN Employee e ON d.department_id = e.department_id;
Q41. Select employees who are not assigned to any project.
SELECT [Link]
FROM Employee e
LEFT JOIN ProjectAssignment pa ON e.emp_id = pa.emp_id
WHERE pa.project_id IS NULL;
Q42. Select employees and the number of projects their department is working on.
SELECT [Link], COUNT(p.project_id) AS project_count
FROM Employee e
JOIN Department d ON e.department_id = d.department_id
JOIN Project p ON d.department_id = p.department_id
GROUP BY [Link];
Q43. Select the departments that have no employees.
SELECT [Link]
FROM Department d
LEFT JOIN Employee e ON d.department_id = e.department_id
WHERE e.emp_id IS NULL;
Q44. Select employee names who share the same department with ‘John Doe’.
SELECT [Link]
FROM Employee e
WHERE e.department_id = (
SELECT department_id
FROM Employee
WHERE name = 'John Doe'
AND [Link] <> 'John Doe';
Q45. Select the department name with the highest average salary.
SELECT [Link]
FROM Department d
JOIN Employee e ON d.department_id = e.department_id
GROUP BY [Link]
ORDER BY AVG([Link]) DESC
LIMIT 1;
(In SQL Server use TOP 1 instead of LIMIT 1)
Nested and Correlated Queries
Q46. Select the employee with the highest salary.
SELECT *
FROM Employee
WHERE salary = (SELECT MAX(salary) FROM Employee);
Q47. Select employees whose salary is above the average salary.
SELECT *
FROM Employee
WHERE salary > (SELECT AVG(salary) FROM Employee);
Q48. Select the second highest salary from the Employee table.
SELECT MAX(salary) AS second_highest_salary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);
(In SQL Server: use SELECT TOP 1 salary FROM Employee WHERE salary < (SELECT
MAX(salary) FROM Employee) ORDER BY salary DESC)
Q49. Select the department with the most employees.
SELECT department_id
FROM Employee
GROUP BY department_id
ORDER BY COUNT(*) DESC
LIMIT 1;
Q50. Select employees who earn more than the average salary of their department.
SELECT e.*
FROM Employee e
WHERE [Link] > (
SELECT AVG(salary)
FROM Employee
WHERE department_id = e.department_id
);
Q51. Select employees hired before the employee with the minimum salary.
SELECT *
FROM Employee
WHERE hire_date < (
SELECT hire_date
FROM Employee
WHERE salary = (SELECT MIN(salary) FROM Employee)
);
Q52. Select employees who work in departments where the average salary is greater than
60000.
SELECT *
FROM Employee e
WHERE e.department_id IN (
SELECT department_id
FROM Employee
GROUP BY department_id
HAVING AVG(salary) > 60000
);
Q53. Select employees who have the same salary as someone else in the company.
SELECT *
FROM Employee e1
WHERE EXISTS (
SELECT 1
FROM Employee e2
WHERE [Link] = [Link] AND e1.emp_id <> e2.emp_id
);
Q54. Select departments where the oldest employee is younger than 40.
SELECT department_id
FROM Employee
GROUP BY department_id
HAVING MAX(age) < 40;
Q55. Select employees who earn the maximum salary in their department.
SELECT e.*
FROM Employee e
WHERE [Link] = (
SELECT MAX(salary)
FROM Employee
WHERE department_id = e.department_id
);