Project Scenario: Employee Management System
Objective
The goal of this project is to create a database for an Employee Management System that
manages employee information, departments, and salaries. We will demonstrate how to use
subqueries to retrieve specific data and use functions to manipulate data.
Step 1: Database Design
We will create the following tables:
1. Employees: To store employee information.
2. Departments: To store department details.
Table Structure
1. Employees Table
○ EmployeeID (INT, Primary Key, Auto-increment)
○ FirstName (NVARCHAR(50), NOT NULL)
○ LastName (NVARCHAR(50), NOT NULL)
○ Email (NVARCHAR(100), NOT NULL)
○ DepartmentID (INT, Foreign Key referencing Departments)
○ Salary (DECIMAL(10, 2), NOT NULL)
2. Departments Table
○ DepartmentID (INT, Primary Key, Auto-increment)
○ DepartmentName (NVARCHAR(100), NOT NULL)
Step 2: SQL Commands
Create the Database
CREATE DATABASE EmployeeDB;
GO
USE EmployeeDB;
GO
Create the Tables
Create Departments Table
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY IDENTITY(1,1),
DepartmentName NVARCHAR(100) NOT NULL
);
GO
Create Employees Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY IDENTITY(1,1),
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
Email NVARCHAR(100) NOT NULL UNIQUE,
DepartmentID INT,
Salary DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);
GO
Step 3: Insert Sample Data
Insert Departments
INSERT INTO Departments (DepartmentName)
VALUES
('Human Resources'),
('IT'),
('Sales'),
('Marketing');
GO
Insert Employees
INSERT INTO Employees (FirstName, LastName, Email, DepartmentID,
Salary)
VALUES
('Alice', 'Johnson', '
[email protected]', 1, 70000.00),
('Bob', 'Smith', '
[email protected]', 2, 80000.00),
('Charlie', 'Brown', '
[email protected]', 3, 60000.00),
('David', 'Wilson', '
[email protected]', 2, 75000.00),
('Eva', 'Clark', '
[email protected]', 4, 55000.00);
GO
Step 4: Using Subqueries and Functions
1. Subquery to Find Employees with Above Average Salary
SELECT FirstName, LastName, Salary
FROM Employees
WHERE Salary > (SELECT AVG(Salary) FROM Employees);
GO
This query retrieves the names and salaries of employees whose salaries are above the
average salary of all employees.
2. Subquery to List Employees in a Specific Department
SELECT FirstName, LastName
FROM Employees
WHERE DepartmentID = (SELECT DepartmentID FROM Departments WHERE
DepartmentName = 'IT');
GO
This query retrieves the names of employees who work in the IT department.
3. Using Functions to Format Employee Names
You can create a user-defined function to concatenate the first and last names of employees.
CREATE FUNCTION dbo.GetFullName(@EmployeeID INT)
RETURNS NVARCHAR(100)
AS
BEGIN
DECLARE @FullName NVARCHAR(100);
SELECT @FullName = FirstName + ' ' + LastName
FROM Employees
WHERE EmployeeID = @EmployeeID;
RETURN @FullName;
END;
GO
4. Using the Function in a Query
Now, you can use the GetFullName function to retrieve full names of employees.
SELECT dbo.GetFullName(EmployeeID) AS FullName, Salary
FROM Employees;
GO
Summary of SQL Concepts Used
1. Subqueries:
○ Used to find employees with above-average salaries and to filter employees by
department.
2. Functions:
○ Created a user-defined function to concatenate first and last names, enhancing
query capabilities.
Conclusion
In this project, we successfully created an EmployeeDB database, defined tables for employees
and departments, and inserted sample data.