Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Name- Shoeb Ahmed Dastagir Faras

ID- 202202238

Create Query Codes:SQL Code for Creating tables:CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Email
VARCHAR(100), DepartmentID INT, SupervisorID INT, HireDate DATE);CREATE TABLE
Departments ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(50));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, FOREIGN KEY (EmployeeID) REFERENCES
Employees(EmployeeID));Insert Queries:SQL code for inserting data into employees
tableINSERT INTO Employees (EmployeeID, FirstName, LastName, Email, DepartmentID,
SupervisorID, HireDate)VALUES (1, 'John', 'Doe', 'john.doe@example.com', 1, NULL,
'2020-01-15'), (2, 'Jane', 'Smith', 'jane.smith@example.com', 2, 1, '2019-05-20'),
(3, 'David', 'Johnson', 'david.johnson@example.com', 1, 1, '2018-09-10'), (4,
'Emily', 'Brown', 'emily.brown@example.com', 2, 3, '2021-03-02'), (5, 'Michael',
'Davis', 'michael.davis@example.com', 3, 1, '2022-07-11'), (6, 'Sarah', 'Wilson',
'sarah.wilson@example.com', 1, 1, '2022-02-14'), (7, 'James', 'Anderson',
'james.anderson@example.com', 2, 1, '2023-01-05'), (8, 'Laura', 'Thompson',
'laura.thompson@example.com', 1, 3, '2023-02-18'), (9, 'Robert', 'Walker',
'robert.walker@example.com', 3, 1, '2022-09-30'), (10, 'Olivia', 'Harris',
'olivia.harris@example.com', 2, 1, '2021-06-12'), (11, 'Thomas', 'Clark',
'thomas.clark@example.com', 1, 3, '2023-04-22'), (12, 'Jessica', 'Martinez',
'jessica.martinez@example.com', 2, 1, '2023-03-10');SQL code for inserting data
into departments tableINSERT INTO Departments (DepartmentID, DepartmentName)VALUES
(1, 'Sales'), (2, 'Marketing'), (3, 'Finance'), (4, 'IT'), (5, 'HR'), (6,
'Operations');SQL Code for inserting data into salaries table: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'); SQL Code for
inserting data into TimeLogs Table:INSERT INTO TimeLogs (LogID, EmployeeID,
LogDate, StartTime)VALUES (1, 1, '2023-01-10', '09:00:00'), (2, 1, '2023-01-11',
'08:30:00'), (3, 2, '2023-01-10', '09:15:00'), (4, 3, '2023-01-11', '08:45:00'),
(5, 4, '2023-01-10', '09:30:00'), (6, 5, '2023-01-11', '08:00:00'),(7, 6, '2023-
01-10', '09:45:00'), (8, 7, '2023-01-11', '08:15:00'), (9, 8, '2023-01-10',
'09:20:00'), (10, 9, '2023-01-11', '08:40:00'), (11, 10, '2023-01-10',
'09:10:00'), (12, 11, '2023-01-11', '08:50:00');SQL Codes for following tasks:a)
Retrieve all employee IDs and their corresponding first names:SELECT EmployeeID,
FirstNameFROM Employees;b) Retrieve the department IDs and department names from
the Departments table:SELECT DepartmentID, DepartmentNameFROM Departments;c)
Retrieve the employee IDs and hire dates of employees hired in or after 2022:SELECT
EmployeeID, HireDateFROM EmployeesWHERE HireDate >= '2022-01-01';d) Retrieve the
employee IDs, salaries, and start dates from the Salaries table for employees with
a salary greater than 55000:SELECT EmployeeID, Salary, StartDateFROM SalariesWHERE
Salary > 55000;e) Retrieve the employee IDs, log dates, and start times from the
Timelogs table for logs recorded on January 11, 2023:SELECT EmployeeID, LogDate,
StartTimeFROM TimelogsWHERE LogDate = '2023-01-11';f) Retrieve the employee IDs and
email addresses of employees with a supervisor ID of 1:SELECT EmployeeID, EmailFROM
EmployeesWHERE SupervisorID = 1;g) Retrieve the department IDs and department names
from the Departments table in descending order of department names:SELECT
DepartmentID, DepartmentNameFROM DepartmentsORDER BY DepartmentName DESC;h)
Retrieve the employee IDs and full names (concatenation of first name and last
name) from the Employees table:SELECT EmployeeID, FirstName, LastNameFROM
Employees;i) Retrieve the distinct hire dates from the Employees table:SELECT
DISTINCT HireDateFROM Employees;j) Retrieve the average salary from the Salaries
table:SELECT AVG(Salary) AS AverageSalaryFROM Salaries;k) Retrieve the employee IDs
and email addresses of employees whose last names start with �S�:SELECT EmployeeID,
EmailFROM EmployeesWHERE LastName LIKE 'S%';L) Retrieve the department IDs and the
count of employees in each department:SELECT DepartmentID, COUNT(*) AS
EmployeeCountFROM EmployeesGROUP BY DepartmentID;M) Retrieve the employee IDs and
full names of employees in department ID 2:SELECT EmployeeID, FirstName, LastName
FROM EmployeesWHERE DepartmentID = 2;N) Retrieve the employee IDs and average
salaries for employees in department ID 1:SELECT EmployeeID, AVG(Salary) AS
AverageSalaryFROM SalariesWHERE EmployeeID IN ( SELECT EmployeeID FROM Employees
WHERE DepartmentID = 1)GROUP BY EmployeeID;O) Retrieve the employee IDs and hire
dates for employees hired in the year 2021 or later:SELECT EmployeeID, HireDateFROM
EmployeesWHERE hiredate >= 2021-01-01;P) Retrieve the department IDs and the
maximum salary in each department: SELECT DepartmentID, MAX(Salary) AS MaxSalary
FROM DepartmentsGROUP BY Salaries;Q) Retrieve the employee IDs and full names of
employees who have an email address containing �example.com�:SELECT EmployeeID,
FirstName, LastNameFROM EmployeesWHERE Email LIKE '%example.com%';R) Retrieve the
department IDs and department names for departments with more than 3 employees:
SELECT DepartmentID, DepartmentNameFROM DepartmentsWHERE DepartmentID IN ( SELECT
DepartmentID FROM Employees GROUP BY DepartmentID HAVING COUNT(*) > 3);S)
Retrieve the employee IDs and the difference in days between their hire dates and
the current date:SELECT EmployeeID, DATEDIFF(CURDATE(), HireDate) AS DaysSinceHire
FROM Employees;T) Retrieve the employee IDs and the number of log entries for each
employee in the Timelogs table:SELECT EmployeeID, COUNT(*) AS LogEntryCountFROM
TimelogsGROUP BY EmployeeID;0SQL0Shoeb Ahmed D Faras202202238

You might also like