Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

sql queries for practice pdf

Uploaded by

21pa1a6126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

sql queries for practice pdf

Uploaded by

21pa1a6126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Example of Basic SQL queries for practice.

Schema

Employee Table

emp_id name age salary department_id hire_date

1 John Doe 28 50000 1 2020-01-15

Jane
2 34 60000 2 2019-07-23
Smith

3 Bob Brown 45 80000 1 2018-02-12

4 Alice Blue 25 45000 3 2021-03-22

5 Charlie P. 29 50000 2 2019-12-01

Department Table

department_id name

1 IT

2 HR

3 Finance

4 Marketing

Project Table

project_id name department_id

1 Project Alpha 1

2 Project Beta 2

Project
3 1
Gamma

4 Project Delta 3

5 Project Epsilon 4

Basic Queries (10)

1. Select all columns from the Employee table.

SELECT * FROM Employee;

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.

SELECT name, salary FROM Employee;

Explanation: This query retrieves only the name and salary columns from the Employee table.

3. Select employees who are older than 30.

SELECT * FROM Employee WHERE age > 30;

Explanation: This query retrieves all columns for employees whose age is greater than 30.

4. Select employees who work in the IT department.

SELECT * FROM Employee WHERE department_id = 1;


Explanation: This query retrieves all employees who are in the IT department.

5. Select the names of all departments.

SELECT name FROM Department;

Explanation: This query retrieves the names of all departments.

6. Select employees who were hired after January 1, 2019.

SELECT * FROM Employee WHERE hire_date > '2019-01-01';

Explanation: This query retrieves employees hired after January 1, 2019.

7. Select employees with a salary between 45000 and 60000.

SELECT * FROM Employee WHERE salary BETWEEN 45000 AND 60000;

Explanation: This query retrieves employees whose salary is between 45000 and 60000.

8. Select distinct salaries from the Employee table.

SELECT DISTINCT salary FROM Employee;

Explanation: This query retrieves the unique salary values from the Employee table.

9. Select the count of employees in the Employee table.

SELECT COUNT(*) FROM Employee;

Explanation: This query counts the number of employees in the Employee table.

10. Select the maximum salary in the Employee table.

SELECT MAX(salary) FROM Employee;

Explanation: This query retrieves the highest salary from the Employee table.

String Matching Queries (5)

11. Select employees whose names start with 'J'.

SELECT * FROM Employee WHERE name LIKE 'J%';

Explanation: This query retrieves employees whose names start with the letter 'J'.

12. Select employees whose names end with 'e'.

SELECT * FROM Employee WHERE name LIKE '%e';

Explanation: This query retrieves employees whose names end with the letter 'e'.

13. Select employees whose names contain 'a'.

SELECT * FROM Employee WHERE name LIKE '%a%';

Explanation: This query retrieves employees whose names contain the letter 'a'.

14. Select employees whose names are exactly 9 characters long.

SELECT * FROM Employee WHERE name LIKE '_________';

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.

SELECT * FROM Employee WHERE name LIKE '_o%';

Explanation: This query retrieves employees whose names have 'o' as the second character. The underscore represents any single character.

Date Queries (5)

16. Select employees hired in the year 2020.


SELECT * FROM Employee WHERE YEAR(hire_date) = 2020;

Explanation: This query retrieves employees who were hired in the year 2020.

17. Select employees hired in January of any year.

SELECT * FROM Employee WHERE MONTH(hire_date) = 1;

Explanation: This query retrieves employees who were hired in the month of January.

18. Select employees hired before 2019.

SELECT * FROM Employee WHERE hire_date < '2019-01-01';

Explanation: This query retrieves employees who were hired before January 1, 2019.

19. Select employees hired on or after March 1, 2021.

SELECT * FROM Employee WHERE hire_date >= '2021-03-01';

Explanation: This query retrieves employees who were hired on or after March 1, 2021.

20. Select employees hired in the last 2 years.

SELECT * FROM Employee WHERE hire_date >= DATE_SUB(CURDATE(), INTERVAL 2 YEAR);

Explanation: This query retrieves employees who were hired in the last two years.

Aggregate Queries (5)

21. Select the total salary of all employees.

SELECT SUM(salary) FROM Employee;

Explanation: This query calculates the total sum of all employee salaries.

22. Select the average salary of employees.

SELECT AVG(salary) FROM Employee;

Explanation: This query calculates the average salary of all employees.

23. Select the minimum salary in the Employee table.

SELECT MIN(salary) FROM Employee;

Explanation: This query retrieves the lowest salary in the Employee table.

24. Select the number of employees in each department.

SELECT department_id, COUNT(*) FROM Employee GROUP BY department_id;

Explanation: This query counts the number of employees in each department.

25. Select the average salary of employees in each department.

SELECT department_id, AVG(salary) FROM Employee GROUP BY department_id;

Explanation: This query calculates the average salary of employees in each department.

Join Queries (10)

26. Select employee names along with their department names.

SELECT Employee.name AS employee_name, Department.name AS department_name


FROM Employee
JOIN Department ON Employee.department_id = Department.department_id;

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.

28. Select employee names and their corresponding project names.

SELECT Employee.name AS employee_name, Project.name AS project_name


FROM Employee
JOIN Project ON Employee.department_id = Project.department_id;

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.

SELECT Employee.name AS employee_name, Department.name AS department_name


FROM Employee
LEFT JOIN Department ON Employee.department_id = Department.department_id;

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.

SELECT Department.name AS department_name, Employee.name AS employee_name


FROM Department
LEFT JOIN Employee ON Department.department_id = Employee.department_id;

Explanation: This query retrieves all departments and their employees, including departments without any employees (NULL).

31. Select employees who are not assigned to any project.

SELECT Employee.name AS employee_name


FROM Employee
LEFT JOIN Project ON Employee.department_id = Project.department_id
WHERE Project.project_id IS 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.

33. Select the departments that have no employees.

SELECT Department.name AS department_name


FROM Department
LEFT JOIN Employee ON Department.department_id = Employee.department_id
WHERE Employee.emp_id IS NULL;

Explanation: This query retrieves departments that do not have any employees.

34. Select employee names who share the same department with 'John Doe'.

SELECT e2.name AS employee_name


FROM Employee e1
JOIN Employee e2 ON e1.department_id = e2.department_id
WHERE e1.name = 'John Doe' AND e2.name != '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.

SELECT Department.name AS department_name


FROM Department
JOIN Employee ON Department.department_id = Employee.department_id
GROUP BY Department.name
ORDER BY AVG(Employee.salary) DESC
LIMIT 1;

Explanation: This query retrieves the department with the highest average salary.

Nested and Correlated Queries (10)


36. Select the employee with the highest salary.

SELECT * FROM Employee


WHERE salary = (SELECT MAX(salary) FROM Employee);

Explanation: This query retrieves the employee with the highest salary using a nested query.

37. Select employees whose salary is above the average salary.

SELECT * FROM Employee


WHERE salary > (SELECT AVG(salary) FROM Employee);

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.

SELECT MAX(salary) FROM Employee


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

Explanation: This query retrieves the second highest salary from the Employee table using a nested query.

39. Select the department with the most employees.

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.

SELECT * FROM Employee e


WHERE salary > (SELECT AVG(salary) FROM Employee WHERE department_id = e.department_id);

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.

SELECT * FROM Employee


WHERE age > (SELECT MAX(age) FROM Employee WHERE department_id = 2);

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.

44. Select employees who work in a department with at least 2 projects.

SELECT * FROM Employee


WHERE department_id IN (SELECT department_id
FROM Project
GROUP BY department_id
HAVING COUNT(project_id) >= 2);

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.

SELECT * FROM Employee e


WHERE salary > (SELECT AVG(salary) FROM Employee WHERE department_id = e.department_id);

Explanation: This query retrieves employees whose salary is higher than the average salary of their department.

47. Select employees with the same salary as 'Bob Brown'.

SELECT * FROM Employee


WHERE salary = (SELECT salary FROM Employee WHERE name = 'Bob Brown');

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.

SELECT * FROM Employee


WHERE department_id = (SELECT department_id FROM Employee WHERE name = 'Alice Blue')
AND salary > 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.

You might also like