sql queries for practice pdf
sql queries for practice pdf
Schema
Employee Table
Jane
2 34 60000 2 2019-07-23
Smith
Department Table
department_id name
1 IT
2 HR
3 Finance
4 Marketing
Project Table
1 Project Alpha 1
2 Project Beta 2
Project
3 1
Gamma
4 Project Delta 3
5 Project Epsilon 4
Explanation: This query retrieves all the columns and rows from the Employee table.
2. Select only the name and salary columns from the Employee table.
Explanation: This query retrieves only the name and salary columns from the Employee table.
Explanation: This query retrieves all columns for employees whose age is greater than 30.
Explanation: This query retrieves employees whose salary is between 45000 and 60000.
Explanation: This query retrieves the unique salary values from the Employee table.
Explanation: This query counts the number of employees in the Employee table.
Explanation: This query retrieves the highest salary from the Employee table.
Explanation: This query retrieves employees whose names start with the letter 'J'.
Explanation: This query retrieves employees whose names end with the letter 'e'.
Explanation: This query retrieves employees whose names contain the letter 'a'.
Explanation: This query retrieves employees whose names are exactly 9 characters long. Each underscore represents one character.
15. Select employees whose names have 'o' as the second character.
Explanation: This query retrieves employees whose names have 'o' as the second character. The underscore represents any single character.
Explanation: This query retrieves employees who were hired in the year 2020.
Explanation: This query retrieves employees who were hired in the month of January.
Explanation: This query retrieves employees who were hired before January 1, 2019.
Explanation: This query retrieves employees who were hired on or after March 1, 2021.
Explanation: This query retrieves employees who were hired in the last two years.
Explanation: This query calculates the total sum of all employee salaries.
Explanation: This query retrieves the lowest salary in the Employee table.
Explanation: This query calculates the average salary of employees in each department.
Explanation: This query retrieves employee names along with their corresponding department names.
27. Select project names along with the department names they belong to.
SELECT Project.name AS project_name, Department.name AS department_name
FROM Project
JOIN Department ON Project.department_id = Department.department_id;
Explanation: This query retrieves project names along with the department names they belong to.
Explanation: This query retrieves employee names and the names of the projects they are working on.
29. Select all employees and their departments, including those without a department.
Explanation: This query retrieves all employees and their departments, including employees who do not belong to any department (NULL).
30. Select all departments and their employees, including departments without employees.
Explanation: This query retrieves all departments and their employees, including departments without any employees (NULL).
Explanation: This query retrieves employees who are not assigned to any project.
32. **
Select employees and the number of projects their department is working on.** sql SELECT Employee.name AS employee_name, COUNT(Project.project_id)
AS project_count FROM Employee LEFT JOIN Project ON Employee.department_id = Project.department_id GROUP BY Employee.name; Explanation:
This query retrieves the number of projects each employee's department is working on.
Explanation: This query retrieves departments that do not have any employees.
34. Select employee names who share the same department with 'John Doe'.
Explanation: This query retrieves employees who are in the same department as 'John Doe', excluding 'John Doe' himself.
35. Select the department name with the highest average salary.
Explanation: This query retrieves the department with the highest average salary.
Explanation: This query retrieves the employee with the highest salary using a nested query.
Explanation: This query retrieves employees whose salary is above the average salary of all employees.
38. Select the second highest salary from the Employee table.
Explanation: This query retrieves the second highest salary from the Employee table using a nested query.
SELECT department_id
FROM Employee
GROUP BY department_id
ORDER BY COUNT(*) DESC
LIMIT 1;
Explanation: This query retrieves the department with the most employees.
40. Select employees who earn more than the average salary of their department.
Explanation: This query retrieves employees who earn more than the average salary of their respective departments.
41. Select the nth highest salary (for example, 3rd highest).
SELECT salary
FROM Employee e1
WHERE 2 = (SELECT COUNT(DISTINCT salary) FROM Employee e2 WHERE e2.salary > e1.salary);
Explanation: This query retrieves the nth highest salary (in this example, the 3rd highest). The inner query counts distinct salaries greater than the current
row's salary.
42. Select employees who are older than all employees in the HR department.
Explanation: This query retrieves employees who are older than the oldest employee in the HR department.
43. Select departments where the average salary is greater than 55000.
SELECT department_id
FROM Employee
GROUP BY department_id
HAVING AVG(salary) > 55000;
Explanation: This query retrieves departments where the average salary of employees is greater than 55000.
Explanation: This query retrieves employees who work in departments that have at least 2 projects.
45. Select employees who were hired on the same date as 'Jane Smith'.
SELECT * FROM Employee
WHERE hire_date = (SELECT hire_date FROM Employee WHERE name = 'Jane Smith');
Explanation: This query retrieves employees who were hired on the same date as 'Jane Smith'.
46. Select employees whose salary is higher than their department's average salary.
Explanation: This query retrieves employees whose salary is higher than the average salary of their department.
Explanation: This query retrieves employees who have the same salary as 'Bob Brown'.
48. Select employees in the same department as 'Alice Blue' and with a salary above 40000.
Explanation: This query retrieves employees who work in the same department as 'Alice Blue' and have a salary above 40000.
49. Select the department name with the smallest total salary expense.
SELECT Department.name
FROM Department
JOIN Employee ON Department.department_id = Employee.department_id
GROUP BY Department.name
ORDER BY SUM(Employee.salary) ASC
LIMIT 1;
Explanation: This query retrieves the department with the smallest total salary expense.
50. Select the names of departments where no employees are older than 40.
SELECT name
FROM Department
WHERE department_id NOT IN (SELECT department_id
FROM Employee
WHERE age > 40);
Explanation: This query retrieves the names of departments where no employees are older than 40.
These queries provide a comprehensive set of exercises for beginners to practice and understand SQL. Each query is explained, and where applicable, alternative
ways to achieve the same result are provided.