0% found this document useful (0 votes)
42 views

SQL Assignment

The document contains SQL statements to create tables and insert data into a sample EmployeeManagement database with tables for employees, departments, salaries, and time logs. The tables are created with the relevant columns and constraints. Sample data is then inserted into each table.

Uploaded by

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

SQL Assignment

The document contains SQL statements to create tables and insert data into a sample EmployeeManagement database with tables for employees, departments, salaries, and time logs. The tables are created with the relevant columns and constraints. Sample data is then inserted into each table.

Uploaded by

Shoeb Faras
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

NAME Shoeb Ahmed Dastagir Faras

STUDENT ID 202202238
SUBJECT S23 IN3193-ISBA G2

CREATE DATABASE EmployeeManagementDB;

-- Use the database


USE EmployeeManagementDB;

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
DepartmentID INT,
SupervisorID INT,
HireDate DATE
);

CREATE TABLE Departments (


DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50) NOT NULL
);

CREATE TABLE Salaries (


SalaryID INT PRIMARY KEY,
EmployeeID INT,
Salary DECIMAL(10, 2),
StartDate DATE,
EndDate DATE,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);

CREATE TABLE TimeLogs (


LogID INT PRIMARY KEY,
EmployeeID INT,
LogDate DATE,
StartTime TIME,
EndTime TIME,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);

INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, DepartmentID,


SupervisorID, HireDate)
VALUES
(1, 'John', 'Doe', '[email protected]', 1, NULL, '2020-01-15'),
(2, 'Jane', 'Smith', '[email protected]', 2, 1, '2019-05-20'),
(3, 'David', 'Johnson', '[email protected]', 1, 1, '2018-09-10'),
(4, 'Emily', 'Brown', '[email protected]', 2, 3, '2021-03-02'),
(5, 'Michael', 'Davis', '[email protected]', 3, 1, '2022-07-11'),
(6, 'Sarah', 'Wilson', '[email protected]', 1, 1, '2022-02-14'),
(7, 'James', 'Anderson', '[email protected]', 2, 1, '2023-01-05'),
(8, 'Laura', 'Thompson', '[email protected]', 1, 3, '2023-02-18'),
(9, 'Robert', 'Walker', '[email protected]', 3, 1, '2022-09-30'),
(10, 'Olivia', 'Harris', '[email protected]', 2, 1, '2021-06-12'),
(11, 'Thomas', 'Clark', '[email protected]', 1, 3, '2023-04-22'),
(12, 'Jessica', 'Martinez', '[email protected]', 2, 1, '2023-03-10');
INSERT INTO Departments (DepartmentID, DepartmentName)
VALUES
(1, 'Sales'),
(2, 'Marketing'),
(3, 'Finance'),
(4, 'IT'),
(5, 'HR'),
(6, 'Operations');

INSERT INTO Salaries (SalaryID, EmployeeID, Salary, StartDate, EndDate)


VALUES
(1, 1, 50000.00, '2020-01-01', '2020-12-31'),
(2, 1, 55000.00, '2021-01-01', '2021-12-31'),
(3, 2, 60000.00, '2019-01-01', '2019-12-31'),
(4, 3, 52000.00, '2018-01-01', '2018-12-31'),
(5, 4, 65000.00, '2021-01-01', '2021-12-31'),
(6, 5, 48000.00, '2022-01-01', '2022-12-31'),
(7, 6, 56000.00, '2023-01-01', '2023-12-31'),
(8, 7, 62000.00, '2023-01-01', '2023-12-31'),
(9, 8, 53000.00, '2023-01-01', '2023-12-31'),
(10, 9, 59000.00, '2022-01-01', '2022-12-31'),
(11, 10, 57000.00, '2021-01-01', '2021-12-31'),
(12, 11, 60000.00, '2023-01-01', '2023-12-31');

INSERT INTO TimeLogs (LogID, EmployeeID, LogDate, StartTime, EndTime)


VALUES
(1, 1, '2023-01-10', '09:00:00', '17:00:00'),
(2, 1, '2023-01-11', '08:30:00', '16:30:00'),
(3, 2, '2023-01-10', '09:15:00', '17:15:00'),
(4, 3, '2023-01-11', '08:45:00', '16:45:00'),
(5, 4, '2023-01-10', '09:30:00', '17:30:00'),
(6, 5, '2023-01-11', '08:00:00', '16:00:00'),
(7, 6, '2023-01-10', '09:45:00', '17:45:00'),
(8, 7, '2023-01-11', '08:15:00', '16:15:00'),
(9, 8, '2023-01-10', '09:20:00', '17:20:00'),
(10, 9, '2023-01-11', '08:40:00', '16:40:00'),
(11, 10, '2023-01-10', '09:10:00', '17:10:00'),
(12, 11, '2023-01-11', '08:50:00', '16:50:00');

SELECT EmployeeID, FirstName FROM Employees;

SELECT DepartmentID, DepartmentName FROM Departments;

SELECT EmployeeID, HireDate FROM Employees WHERE HireDate >= '2022-01-01';

SELECT EmployeeID, Salary, StartDate FROM Salaries WHERE Salary > 55000;

SELECT EmployeeID, LogDate, StartTime FROM TimeLogs WHERE LogDate = '2023-01-11';

SELECT EmployeeID, Email FROM Employees WHERE SupervisorID = 1;

SELECT DepartmentID, DepartmentName FROM Departments ORDER BY DepartmentName DESC;


SELECT EmployeeID, CONCAT(FirstName, ' ', LastName) AS FullName FROM Employees;

SELECT DISTINCT HireDate FROM Employees;

SELECT AVG(Salary) AS AverageSalary FROM Salaries;

SELECT EmployeeID, Email FROM Employees WHERE LastName LIKE 'S%';

SELECT DepartmentID, COUNT(*) AS EmployeeCount FROM EmployeesGROUP BY DepartmentID;

SELECT EmployeeID, CONCAT(FirstName, ' ', LastName) AS FullName FROM Employees


WHERE DepartmentID = 2;

SELECT EmployeeID, AVG(Salary) AS AverageSalary FROM Salaries


WHERE EmployeeID IN (
SELECT EmployeeID
FROM Employees
WHERE DepartmentID = 1
)
GROUP BY EmployeeID;

SELECT EmployeeID, HireDate FROM Employees


WHERE YEAR(HireDate) >= 2021;

SELECT DepartmentID, MAX(Salary) AS MaximumSalary FROM Salaries


GROUP BY DepartmentID;

SELECT EmployeeID, CONCAT(FirstName, ' ', LastName) AS FullName FROM Employees


WHERE Email LIKE '%example.com%';

SELECT d.DepartmentID, d.DepartmentName FROM Departments d


JOIN Employees e ON d.DepartmentID = e.DepartmentID
GROUP BY d.DepartmentID, d.DepartmentName
HAVING COUNT(*) > 3;

You might also like