Lesson 04: Functions in SQL
Overview
In this exercise, you will engage in database management and SQL query writing to
handle employee information. The primary focus is on creating tables, inserting
data, and performing data retrieval operations. The scenario involves managing
employee data in a company, where you will create tables, populate them with
data, and execute queries to analyze and update information.
Instructions
• Work through the tasks individually or within your group
• Divide your time evenly among the tasks to ensure a comprehensive
understanding
Task
You are the database administrator for a company, responsible for managing
employee information and department details. The data includes employee ID,
name, department ID, and salary. Your goal is to organize and update this data
efficiently using SQL queries.
• Input Data:
o Database Name: 'employee_management_database.'
o Tables:
▪ Employees
▪ Department
o Employees Table Creation
Column Name Data Type Constraints
employee_id INT PRIMARY KEY
employee_name VARCHAR(255)
department_id INT
salary DECIMAL(10,2)
▪ Insert the provided employee data into the 'employees' table.
employee_id employee_name department_id salary
1 John Doe 1 50000.00
2 Jane Smith 2 60000.00
3 Bob Johnson 1 55000.00
4 Alice Brown 3 70000.00
5 Charlie White 2 62000.00
o Department Table Creation:
Column Name Data Type Constraints
department_id INT PRIMARY KEY
department_name VARCHAR(255)
▪ Insert the provided department data into the 'departments'
table.
department_id department_name
1 HR
2 IT
3 Finance
Based on the above situation, perform the following tasks:
1. Employee-Department Query:
Write a SELECT query to retrieve employee names, corresponding
department names, and salaries. Order the result by department name in
ascending order.
2. Salary Adjustment:
Update the salaries of employees in the 'IT' department by increasing them
by 10%. Use the ROUND function to maintain decimal precision.
3. Display Updated IT Department Salaries:
Retrieve and display the updated salaries of employees in the 'IT'
department.
Discussion Questions (Optional)
If time permits, discuss the following questions:
• Discuss the importance of maintaining a relational database structure for
managing employee information.
• Evaluate the efficiency of using SQL queries for updating employee salaries
compared to manual methods.
• Explore potential scenarios where the employee information database can
be useful in HR and finance departments.
Answer Key
Note: Create a database and tables using the following code:
1. Create a new database
SQL Code:
CREATE DATABASE IF NOT EXISTS employee_management_database;
2. Switch to the new database
SQL Code:
USE employee_management_database;
3. Create the ‘employees’ table
SQL Code:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(255),
department_id INT,
salary DECIMAL(10,2)
);
DESCRIBE employees;
4. Insert data into the ‘employees’ table
SQL Code:
INSERT INTO employees VALUES
(1, 'John Doe', 1, 50000.00),
(2, 'Jane Smith', 2, 60000.00),
(3, 'Bob Johnson', 1, 55000.00),
(4, 'Alice Brown', 3, 70000.00),
(5, 'Charlie White', 2, 62000.00);
SELECT * FROM employees;
5. Create the 'departments' table
SQL Code:
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(255)
);
DESCRIBE departments;
6. Insert data into the 'departments' table
SQL Code:
INSERT INTO departments VALUES
(1, 'HR'),
(2, 'IT'),
(3, 'Finance');
SELECT * FROM departments;
1. Employee-Department Query:
Retrieve and display the employee names, department names, and
salaries from the "employee_management_database.employees"
SQL Code:
SELECT
employee_management_database.employees.employee_name,
employee_management_database.departments.department_name,
employee_management_database.employees.salary
FROM employee_management_database.employees,
employee_management_database.departments
WHERE employee_management_database.employees.department_id =
employee_management_database.departments.department_id
ORDER BY
employee_management_database.departments.department_name ASC;
2. Salary Adjustment:
Update the salary of employees in the 'IT' department by increasing it
by 10%, and then retrieve the updated information for employees in the
'IT' department.
SQL Code:
UPDATE employee_management_database.employees
SET salary = ROUND(salary * 1.1, 2)
WHERE department_id = (SELECT department_id FROM
employee_management_database.departments WHERE department_name =
'IT');
3. Display Updated IT Department Salaries:
SQL Code:
SELECT * FROM employee_management_database.employees WHERE
department_id = (SELECT department_id FROM
employee_management_database.departments WHERE department_name =
'IT');