Lab Mid Spring 2024 Solution

You might also like

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

INTRODUCTION TO DATABASE SYSTEMS (Lab) Spring 2024

Mid Examination Solution


Course Title: Introduction to Database Systems Course Code: Cs2421W Credit 1
Course Instructor/s: Mr.
(Lab)Mazhar Javed Awan Programme Hours:
Semester: Section: W2
Name: Date:
Time Allowed: 90 Minutes Maximum Marks: 50
Student’s Name: Reg. No.
Important Instructions / Guidelines:
Note: READ THESE INSTRUCTIONS
 Attempt all questions
Answers should be written within space and precise

Q1. Write SQL commands for following queries regarding Joins [2*3=06 marks]

a) What will be output of following command?


SELECT *
FROM employee FULL OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

b) Write SQL command for those record which matches in both tables ?

SELECT *
FROM employee inner JOIN department
ON employee.DepartmentID = department.DepartmentID;
c) What will be output of following command?
SELECT *
FROM employee LEFT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

Q2. Consider the following relations containing airline flight information: [2 * 5 =10 marks]
Flights(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time)
Aircraft(aid: integer, aname: string, cruisingrange: integer) Certified(eid: integer, aid: integer)
Employees(eid: integer, ename: string, salary: integer)
Note that the Employees relation describes pilots and other kinds of employees as well; every pilot is certified for some
aircraft (otherwise, he or she would not qualify as a pilot), and only pilots are certified to fly.
(a) Write SQL queries for the following Statements using above relations
1. Find the eid’s of pilots certified for some Boeing aircraft.

SQL
SELECT C.eid
FROM Aircraft A, Certified C
WHERE A.aid = C.aid AND A.aname = ‘Boeing’

2. Find the names of pilots certified for some Boeing aircraft.

SELECT E.ename
FROM Aircraft A, Certified C, Employees E
WHERE A.aid = C.aid AND A.aname = ‘Boeing’ AND E.eid = C.eid

3. Find the aids of all aircraft that can be used on non-stop flights from Lahore to Islamabad.
SELECT A.aid
FROM Aircraft A, Flights F
WHERE F.from = ‘Lahore’ AND F.to = ‘Islamabad’ AND
A.cruisingrange > F.distance

4. Identify the flights that can be piloted by every pilot whose salary is more than $100,000.

SELECT E.ename
FROM Aircraft A, Certified C, Employees E, Flights F
WHERE A.aid = C.aid AND E.eid = C.eid AND
distance < cruisingrange AND salary > 100,
5. Find the names of pilots who can operate planes with a range greater than 3,000 miles but are not certified on any
Boeing aircraft.

SELECT E.ename
FROM Certified C, Employees E, Aircraft A
WHERE A.aid = C.aid AND E.eid = C.eid AND A.cruisingrange > 3000
AND E.eid NOT IN ( SELECT C2.eid
FROM Certified C2, Aircraft A2
WHERE C2.aid = A2.aid AND A2.aname = ‘Boeing’ )

Q3. Queries/TASKS to Perform to see the following Tables: [ 2* 9=18 marks]

Company Database
Here are the SQL queries that correspond to the tasks described in the document for the company database:

1. **Add a column for Manager_Assistant in the DEPARTMENT relation with appropriate constraints.**
```sql
ALTER TABLE DEPARTMENT
ADD Manager_Assistant VARCHAR(50) NOT NULL;
```

2. **Increase the salary of all employees of department number 30 by 5%.**


```sql
UPDATE EMPLOYEE
SET Salary = Salary * 1.05
WHERE Dno = 30;
```

3. **Show Fname of each employee working in department number 4 with his supervisor's name.**
```sql
SELECT E.Fname AS EmployeeName, S.Fname AS SupervisorName
FROM EMPLOYEE E
JOIN EMPLOYEE S ON E.Super_ssn = S.Ssn
WHERE E.Dno = 4;
```

4. **Retrieve project names alphabetically on which Ahmad is working.**


```sql
SELECT DISTINCT P.Pname
FROM PROJECT P
JOIN WORKS_ON W ON P.Pnumber = W.Pno
JOIN EMPLOYEE E ON W.Essn = E.Ssn
WHERE E.Fname = 'Ahmad'
ORDER BY P.Pname ASC;
```

5. **Find number of projects on which Ahmad is working.**


```sql
SELECT COUNT(DISTINCT W.Pno) AS NumberOfProjects
FROM WORKS_ON W
JOIN EMPLOYEE E ON W.Essn = E.Ssn
WHERE E.Fname = 'Ahmad';
```

6. **Show average salary of employees in each department.**


```sql
SELECT Dno, AVG(Salary) AS AvgSalary
FROM EMPLOYEE
GROUP BY Dno;
```

7. **Retrieve Sum of Salary of employees with department number which has more than 5 employees.**
```sql
SELECT Dno, SUM(Salary) AS TotalSalary
FROM EMPLOYEE
GROUP BY Dno
HAVING COUNT(Ssn) > 5;
```

8. **Retrieve only those Pno and Pname on which less than 3 employees are working.**
```sql
SELECT P.Pnumber, P.Pname
FROM PROJECT P
JOIN WORKS_ON W ON P.Pnumber = W.Pno
GROUP BY P.Pnumber, P.Pname
HAVING COUNT(W.Essn) < 3;
```

9. **Find Name and salary of employees who earn a salary more than that of employee named Ali.**
```sql
SELECT Fname, Salary
FROM EMPLOYEE
WHERE Salary > (SELECT Salary FROM EMPLOYEE WHERE Fname = 'Ali');
Q4.. The following questions are related to some scenario of various SQL commands: [2 * 8= 16 marks]

1. Given the following data definition, write a query that returns the number of students whose first name is mazhar.
--
-- TABLE students
-- id INTEGER PRIMARY KEY,
-- firstName VARCHAR(30) NOT NULL,
-- lastName VARCHAR(30) NOT NULL

select count(*) from students where firstName = 'mazhar';

2. Information about pets is kept in two separate tables:


--
-- TABLE dogs
-- id INTEGER NOT NULL PRIMARY KEY,
-- name VARCHAR(50) NOT NULL
--
-- TABLE cats
-- id INTEGER NOT NULL PRIMARY KEY,
-- name VARCHAR(50) NOT NULL
--
-- Write a query that select all distinct pet names

-- Expected output (in any order):


-- name

-- Bella
-- Kitty
-- Lola

select name from dogs


union
select name from cats;

3. Set the value of the City columns to 'Oslo', but only the ones where the Country column has the value "Norway".
4. Use the BETWEEN operator to select all the records where the value of the ProductName column is alphabetically
between 'Geitost' and 'Pavlova'.

5. all records where the first letter of the City is NOT an "a" or a "c" or an "f".

6. Choose the correct JOIN clause to select all the records from the Customers table plus all the matches in
the Orders table.

7. List the number of customers in each country, ordered by the country with the most customers first.

8. Set the value of the City columns to 'Oslo', but only the ones where the Country column has the value "Norway".

You might also like