You are on page 1of 7

DBMS Lab Report

Open Ended Lab

Umar Hanif

BESE 28 C

14th Jan 2024

To make Tables and Enter data:

use OpenEndedLabDBMS;

CREATE TABLE Employee (

EmployeeID INT PRIMARY KEY AUTO_INCREMENT,

Name VARCHAR(100),

Age INT

);

CREATE TABLE OrganizationalPerformance (

PerformanceID INT PRIMARY KEY AUTO_INCREMENT,

CompanyID INT,

EmployeeID INT,

PerformanceScore DECIMAL(5, 2),

FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)

);

INSERT INTO Employee (Name, Age) VALUES

('John Doe', 25),

('Jane Smith', 32),

('Michael Johnson', 45),


('Emily Davis', 28),

('David Brown', 38),

('Sarah Clark', 29),

('Ryan Wilson', 34),

('Olivia Martinez', 42),

('Daniel Thompson', 26),

('Sophia Garcia', 31),

('Ethan Lee', 40),

('Ava Rodriguez', 27),

('Matthew Hernandez', 33),

('Chloe Miller', 39),

('William Moore', 30),

('Isabella Nelson', 35),

('James White', 43),

('Grace King', 36),

('Alexander Hall', 41),

('Mia Perez', 24);

Employee Table
INSERT INTO OrganizationalPerformance (CompanyID, EmployeeID, PerformanceScore) VALUES

(1, 1, 85.5),

(1, 2, 92.3),

(1, 3, 78.9),

(2, 4, 88.7),

(2, 5, 91.2),

(1, 6, 84.2),

(1, 7, 89.6),

(2, 8, 77.3),

(2, 9, 90.1),

(1, 10, 82.5),

(1, 11, 87.4),

(2, 12, 79.8),

(2, 13, 93.0),

(1, 14, 86.1),

(1, 15, 89.2),

(2, 16, 76.5),

(2, 17, 90.5),

(1, 18, 83.7),

(1, 19, 88.9),

(2, 20, 75.8);

Result
Code for Tasks:

--Task 1

select name, age from Employee order by age;

--Task 2

select case
when age < 30 then 'Under 30'

when age >= 30 and age <=40 then 'Between 30 and 40'

when age > 40 then 'Over 40' end as AgeGroup,count(*) as EmployeeCount from Employee

group by AgeGroup order by EmployeeCount asc;

Result

--Task 3

Select

Case

When E.Age Between 20 And 29 Then '20s'

When E.Age Between 30 And 39 Then '30s'

When E.Age Between 40 And 49 Then '40s'

When E.Age Between 50 And 59 Then '50s'

Else 'Teen'

End As AgeGroup,

avg(OP.PerformanceScore) As AveragePerformanceInAgeGroup

From OrganizationalPerformance As OP

INNER JOIN Employee as E on E.EmployeeID = OP.EmployeeID

Where OP.CompanyID = 1

Group By

Case

When E.Age Between 20 And 29 Then '20s'

When E.Age Between 30 And 39 Then '30s'

When E.Age Between 40 And 49 Then '40s'

When E.Age Between 50 And 59 Then '50s'

Else 'Teen'
End;

Result:

--Task 4

select case when e.age < 30 then "Under 30"

when e.age >= 30 and e.age <= 40 then '30-40'

when e.age > 40 then 'Over 40'

end as AgeGroup,

avg(case when OP.companyID = 1 then OP.PerformanceScore end) as 'AvgPerformanceCompany1',

avg(case when OP.companyID = 2 then OP.PerformanceScore end) as 'AvgPerformanceCompany2',

AVG(CASE WHEN CompanyID = 1 THEN PerformanceScore END) - AVG(CASE WHEN CompanyID = 2


THEN PerformanceScore END) AS PerformanceScoreDifference

from Employee as e inner join OrganizationalPerformance as OP on e.EmployeeID = OP.EmployeeID


group by

case

when e.age < 30 then "Under 30"

when e.age >= 30 and e.age <= 40 then '30-40'

when e.age > 40 then 'Over 40' end;

Result:
Notes:

Task 1

⦁ Use of Select and From to get values from Employee table that is aliased as E

⦁ Usage of Order By to sort values by Ascending order of age.

Task 2

⦁ Use of Select and From to get values from Employee table that is aliased as E.

⦁ Case Statement used to create a new column that specifies AgeGroup and then again to group
the output using the Case statement in Group By.

⦁ Count() function used to count number of employee’s within the AgeGroups.

Task 3

⦁ Use of Select and From to get values from OrganizationalPerformance table that is aliased as E.

⦁ Use of Inner Join using EmployeeID to get values from Employee table that is aliased as E.

⦁ Case Statement used to create a new column that specifies AgeGroup and then again to group
the output using the Case statement in Group By.

⦁ Avg() Function to get average Performance Score

⦁ Where used to filter the Company.

Task 4

⦁ Use of Select and From to get values from OrganizationalPerformance table that is aliased as E.

⦁ Use of Inner Join using EmployeeID to get values from Employee table that is aliased as E.

⦁ Case Statement used to create a new column that specifies AgeGroup and then again to group
the output using the Case statement in Group By.

⦁ Multiple Avg() Function to get average Performance Score with a Case Statement to select
different companies average score.

⦁ A new column PerformanceScoreDifference is created to hold values of difference using a


subtraction of the averages.

You might also like