Database Systems Spring 2024

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DATABASE SYSTEMS SPRING 2024

Instructor: Ms. Mehwish Ghafoor Assignment-01

Abdullah Mansoor
Teaching Assistant Muhammad Taha
Muzamil Ali
Zain Razzaq

Note:
Submit the assignment on Canvas using the following format BCS220XX.PDF
Deadline: March 22, 2024
Keep it in mind Deadline will not be Extended
LATE SUBMISSION IS NOT ACCEPTABLE.
You MUST NOT PLAGIARIZE as it will lead to zero in this Assignment

( Single row functions, Multiple row functions, Case, Joins, Group by clause,
Having clause and Subqueries )
1. Write a query that rounds the `salary` column to the nearest hundred for each record in
the `employees` table.
2. Write a query that calculates the total salary for all employees who were hired after
January 1, 2022 and have an even employee_id.
3. Write a query that calculates the average salary for all employees hired after December 1,
2022.
4. Write a query that categorizes employees into 'Junior', 'Intermediate', and 'Senior' based
on their years of experience, with 'Junior' having 0-2 years of experience, 'Intermediate'
having 3-5 years of experience, and 'Senior' having 6 or more years of experience.
5. Write a query that returns 'High' if the `budget` of a department is greater than $50,000,
'Medium' if it is between $25,000 and $50,000, 'Low' if it is less than $25,000, and
'Unknown' otherwise.
6. Write a query that returns 'Senior' if the employee has a `salary` greater than $75,000,
'Intermediate' if it is between $50,000 and $75,000, 'Junior' if it is less than $50,000, and
'Unknown' otherwise.
7. Write a query that categorizes employees into 'Above Average', 'Average', and 'Below
Average' based on their salary, with 'Above Average' being above the average salary,
'Average' being equal to the average salary, and 'Below Average' being below the average
salary, but only for employees hired after January 1, 2022..
8. Write a query that categorizes employees into 'Senior', 'Intermediate', and 'Junior' based
on their salary, with 'Senior' being above $100,000, 'Intermediate' being between $75,000
and $100,000, and 'Junior' being below $75,000. However, employees who were hired
before January 1, 2022, should be categorized based on the previous rules: 'Senior' if their
salary is above $75,000, 'Intermediate' if it is between $50,000 and $75,000, and 'Junior'
if it is below $50,000.
9. Write a query to retrieve the employees' details (first_name, last_name, salary) who are
working in departments located in cities where the total budget allocated to all
departments exceeds 500,000.
10. Write a query to list the employees (first_name, last_name) and their salary who work in
departments with budgets greater than the average department budget and also earn more
than the average salary of all employees.
11. Write a query to display the average salary of all employees working in departments
located in cities where more than three employees are earning more than 100,000.
12. Write a query to list the employees (first_name, last_name) and their salary who work in
departments located in cities where the highest budget department is located.
13. Write a query to retrieve the employees' details (first_name, last_name, salary) who are
working in the same department as 'John Doe' and earn more than 'John Doe'.
14. Write a query to display the total budget and average salary of all employees working in
departments located in cities with departments having more than 10 employees.
15. Write a query that retrieves the maximum salary for each department, but only for
departments where the maximum salary is greater than $75,000 and the department has
more than 5 employees.
16. Write a query that retrieves the minimum salary for each department, but only for
departments with more than 10 employees and where the minimum salary is greater than
the average salary for the department.
17. Write a query that retrieves the average salary for each department, but only for
departments where the average salary is greater than the average salary for the entire
company.
18. Write a query that retrieves the maximum salary for each department, but only for
departments where the maximum salary is greater than $75,000 and the department is
located in a city with more than 3 departments.
19. Write a query that retrieves the minimum budget for each location, but only for locations
where the average budget is less than $50,000 and the location has more than 2
departments.
20. Write a query that retrieves the maximum budget for each location, but only for locations
where the maximum budget is greater than the minimum budget for the entire company
and the location has more than one department.
21. Write a query to retrieve the first name, last name, and salary of the employee with the
highest salary in the department that has the highest total salary.
22. Write a query to retrieve the department name, budget, and average salary of departments
where the average salary is greater than the average salary of employees hired after
January 1, 2022.
23. Write a query to retrieve the first name, last name, and salary of the employee who has
the third-lowest salary in the department with the highest total budget.
24. Write a query that retrieves the `department_name` and `budget` of the department with
the highest budget.
25. Write a query to retrieve the first name, last name, and salary of the employee with the
fourth-highest salary in the department where the average salary is the lowest.
26. Write a query to retrieve the department name, budget, and total salary of departments
where the total salary is greater than the average salary multiplied by the budget.
27. Write a query that returns the `department_id` and the average salary of employees in
each department, but only for departments where the average salary is between $50,000
and $75,000.
28. Write a query that retrieves the department_id and the minimum salary of employees in
each department, but only for departments where the minimum salary is greater than or
equal to $40,000.
29. Write a query that returns the `department_id` and the sum of salaries of employees in
each department, but only for departments where the sum of salaries is exactly $200,000.
30. Write a query that returns the `department_id` and the number of employees in each
department, but only for departments with more than 2 employees and less than 7
employees.

RELATIONAL ALGEBRA
Consider the following relational database schema consisting of the four relation schemas:
passenger ( pid, pname, pgender, pcity)
agency ( aid, aname, acity)
flight (fid, fdate, time, src, dest)
booking (pid, aid, fid, fdate)

Relational algebra operators:

σ – selection with conditions (It selects all tuples that satisfy the conditions. Shows entire
table with respect to the structure)

Π – projection operator (It selects the attributes which are listed here)

⨝ - natural join operator (Binary operator that join two relations on common attributes’
values)
-, ∪, and ∩ - set operators (difference, union and intersection)

Answer the following questions using relational algebra queries:


1. Get the complete details of all flights to Islamabad.
2. Get the details about all flights from lahore to islamabad.
3. Find only the flight numbers for passenger with pid 123 for flights to Lahore before
06/11/2020.
4. Find the passenger names for passengers who have bookings on at least one flight.
5. Find the passenger names for those who do not have any bookings in any flights.
6. Find the agency names for agencies that located in the same city as passenger with
passenger id 123.
7. Get the details of flights that are scheduled on both dates 01/12/2020 and 02/12/2020 at
16:00 hours.
8. Get the details of flights that are scheduled on either of the dates 01/12/2020 or
02/12/2020 or both at 16:00 hours.
9. Find the agency names for agencies who do not have any bookings for passenger with id
123.
10. Find the details of all male passengers who are associated with Jet agency.

Tables

DEPARTMENTS

● Department_id
● Department_name
● Location
● Budget

Employees

● First_name
● Last_name
● Hire_date
● Salary
● Department_id
Passenger

● pid
● pname
● pgender
● pcity

Agency

● aid
● aname
● acity

Flight

● fid
● fdate
● time
● src
● dest

Booking

● pid
● aid
● fid
● fdate

Dataset
CREATE TABLE if not exists departments (
department_id INT AUTO_INCREMENT PRIMARY KEY,
department_name VARCHAR(50) NOT NULL,
location VARCHAR(50) NOT NULL,
budget DECIMAL(10, 2) NOT NULL
);

CREATE TABLE if not exists employees (


employee_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
hire_date DATE NOT NULL,
salary DECIMAL(10, 2) NOT NULL,
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

INSERT INTO departments (department_id, department_name,


location,budget) VALUES
(1, 'Human Resources', 'New York',70000),
(2, 'Marketing', 'London',50000),
(3, 'Finance', 'Paris',70000),
(4, 'Operations', 'Berlin',90000),
(5, 'Research', 'Tokyo',100000),
(6, 'Sales', 'Sydney',20000),
(7, 'Development', 'Moscow',10000),
(8, 'Quality Assurance', 'Seoul',50000),
(9, 'Support', 'Dubai',60000),
(10, 'Delivery', 'Beijing',70000);

INSERT INTO employees (first_name, last_name, hire_date, salary,


department_id) VALUES
('John', 'Doe', '2022-01-01', 50000, 1),
('Jane', 'Doe', '2022-02-01', 55000, 2),
('Jim', 'Smith', '2022-03-01', 60000, 3),
('Amy', 'Johnson', '2022-04-01', 65000, 4),
('Bob', 'Williams', '2022-05-01', 70000, 5),
('Carol', 'Jones', '2022-06-01', 75000, 6),
('David', 'Brown', '2022-07-01', 80000, 7),
('Emily', 'Davis', '2022-08-01', 85000, 8),
('Frank', 'Miller', '2022-09-01', 90000, 9),
('Gina', 'Wilson', '2022-10-01', 95000, 10),
('Harry', 'Moore', '2022-11-01', 100000, 1),
('Iris', 'Taylor', '2022-12-01', 105000, 2),
('Jack', 'Anderson', '2022-01-01', 110000, 3),
('Karen', 'Thomas', '2022-02-01', 115000, 4),
('Larry', 'Jackson', '2022-03-01', 120000, 5),
('Maggie', 'White', '2022-04-01', 125000, 6),
('Nathan', 'Harris', '2022-05-01', 130000, 7),
('Olivia', 'Martin', '2022-06-01', 135000, 8),
('Paul', 'Thompson', '2022-07-01', 140000, 9),
('Quinn', 'Young', '2022-08-01', 145000, 2);

You might also like