Lab Mid Spring 2024 Solution
Lab Mid Spring 2024 Solution
Q1. Write SQL commands for following queries regarding Joins [2*3=06 marks]
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’
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’ )
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;
```
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;
```
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
-- Bella
-- Kitty
-- Lola
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".