Of course.
Here are some exercises for practicing Data Manipulation Language
(DML) in SQL. DML commands are used to manage the data within database
tables. The primary commands are SELECT, INSERT, UPDATE, and DELETE.
These exercises assume you have a database schema with the following tables.
If you don't have them, you can use the DDL exercises from previous
conversation to create them.
Database Schema for Exercises
Table: Employees
EmployeeID (INT, Primary Key)
FirstName (VARCHAR(50))
LastName (VARCHAR(50))
DepartmentID (INT)
Salary (DECIMAL(10, 2))
HireDate (DATE)
Email (VARCHAR(100), UNIQUE)
Table: Departments
DepartmentID (INT, Primary Key)
DepartmentName (VARCHAR(50), UNIQUE)
Location (VARCHAR(100))
Table: Projects
ProjectID (INT, Primary Key)
ProjectName (VARCHAR(100))
StartDate (DATE)
EndDate (DATE)
Budget (DECIMAL(12, 2))
Table: EmployeeProjects (Associative/Junction Table)
EmployeeID (INT, Foreign Key to Employees)
ProjectID (INT, Foreign Key to Projects)
HoursWorked (DECIMAL(5, 2))
Part 1: INSERT Statements
Exercise 1: Basic INSERT
1. Insert two new departments into the Departments table:
o DepartmentID: 1, DepartmentName: 'IT', Location: 'Building A'
o DepartmentID: 2, DepartmentName: 'Marketing', Location: 'Building
B'
2. Insert three new employees into the Employees table. Make sure to specify
all columns explicitly.
o EmployeeID: 101, FirstName: 'Alice', LastName: 'Johnson',
DepartmentID: 1, Salary: 75000.00, HireDate: '2020-01-15', Email:
'alice.j@[Link]'
o EmployeeID: 102, FirstName: 'Bob', LastName: 'Williams',
DepartmentID: 1, Salary: 82000.00, HireDate: '2019-05-20', Email:
'bob.w@[Link]'
o EmployeeID: 103, FirstName: 'Charlie', LastName: 'Brown',
DepartmentID: 2, Salary: 60000.00, HireDate: '2021-03-10', Email:
'charlie.b@[Link]'
3. Insert two new projects into the Projects table.
o ProjectID: 501, ProjectName: 'Website Redesign', StartDate: '2023-
01-01', EndDate: '2023-06-30', Budget: 50000.00
o ProjectID: 502, ProjectName: 'Marketing Campaign', StartDate:
'2023-02-15', EndDate: '2023-08-31', Budget: 75000.00
4. Insert three entries into the EmployeeProjects table to assign employees
to projects.
o Alice (101) works 120.5 hours on the 'Website Redesign' project
(501).
o Bob (102) works 150 hours on the 'Website Redesign' project (501).
o Charlie (103) works 80 hours on the 'Marketing Campaign' project
(502).
Part 2: SELECT Statements
Exercise 2: Basic SELECT
1. Retrieve the FirstName and LastName of all employees.
2. Retrieve all columns for all departments.
Exercise 3: SELECT with WHERE Clause
1. Find all employees who work in DepartmentID 1.
2. Find all projects with a Budget greater than 60000.00.
3. Find the names of all employees who were hired before '2021-01-01'.
Exercise 4: SELECT with JOIN
1. List all employees and their corresponding department names.
2. Find the names of all employees who have worked on the project named
'Website Redesign'.
3. Retrieve the FirstName, LastName, and DepartmentName of employees
who earn a Salary of over 70000.00.
Exercise 5: SELECT with GROUP BY, HAVING, and Aggregate Functions
1. Count the number of employees in each department.
2. Find the average salary for each department.
3. Find the total number of hours worked on each project.
4. List the DepartmentName and the number of employees for departments
that have more than 1 employee.
5. Find the ProjectName and total hours worked for projects where the total
hours worked exceed 100.
Exercise 6: SELECT with ORDER BY and LIMIT / TOP
1. Retrieve the top 3 highest-paid employees' names and salaries.
2. List all projects, ordered by their Budget in descending order.
3. Find the names of employees, ordered by their LastName and then
FirstName in ascending order.
Part 3: UPDATE Statements
Exercise 7: Basic UPDATE
1. Increase the Salary of the employee with EmployeeID 101 by 5000.00.
2. Update the EndDate of the 'Marketing Campaign' project (ProjectID 502) to
'2023-09-30'.
3. Change the DepartmentID of employee 103 to 1.
Exercise 8: UPDATE with WHERE and Calculations
1. Give a 10% raise to all employees in the 'IT' department.
2. Decrease the Budget of all projects that started in '2023' by 5%.
Part 4: DELETE Statements
Exercise 9: Basic DELETE
1. Delete the EmployeeProjects entry for employee 102 and project 501.
2. Delete the project named 'Website Redesign' from the Projects table.
Exercise 10: DELETE with WHERE Clause
1. Delete all employees who were hired after '2022-01-01'.
2. Delete all departments located in 'Building B'.
Part 5: Challenge Exercise (Combining DML)
Exercise 11: Complex Scenario
1. Insert a new department: DepartmentID: 3, DepartmentName: 'HR',
Location: 'Building C'.
2. Insert a new employee: FirstName: 'Diana', LastName: 'Smith',
DepartmentID: 3, Salary: 65000.00, HireDate: '2023-05-01', Email:
'diana.s@[Link]'.
3. Give all employees in the 'IT' department a raise. Their new salary should
be their current salary plus 5% of the average salary of all employees in
the IT department.
4. Find the names of all employees who have worked on a project with a
budget greater than the average budget of all projects.