0% found this document useful (0 votes)
9 views6 pages

Quiz 3 DBMS - Solution

This document is a quiz for the CS-232 Introduction to Databases course at Ghulam Ishaq Khan Institute, consisting of various SQL-related questions. It covers topics such as query construction, handling relational databases, and using JOINs to retrieve data. The quiz includes specific tasks like identifying query issues, constructing SQL queries for bank and employee databases, and writing correlated subqueries.

Uploaded by

muazndm129
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Quiz 3 DBMS - Solution

This document is a quiz for the CS-232 Introduction to Databases course at Ghulam Ishaq Khan Institute, consisting of various SQL-related questions. It covers topics such as query construction, handling relational databases, and using JOINs to retrieve data. The quiz includes specific tasks like identifying query issues, constructing SQL queries for bank and employee databases, and writing correlated subqueries.

Uploaded by

muazndm129
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ghulam Ishaq Khan Institute of Engineering Sciences and

Technology
Faculty of Computer Science and Engineering
CS-232 Introduction to Databases
QUIZ # 3
25 Marks
*All questions in this quiz are mapped onto CLO-3
Name Dated: 16th Dec 2022
Registration # Time: 40 minutes

Question 1. Given that you have a relation called “instructor”, consider the query
as given below and write output. In case you think there is an issue with the query,
you need to identify the problem.
(2 Marks)

select dept_name, ID, avg


(salary)
from instructor
group by dept_name;

The main problem which will


definitely cause an issue is that
when we group by dept_name there
would still be distinct and multiple
“IDs” against each department and
they cannot be shown in against a
single row of department unless we
use “group_concat”.

Question 2. Consider the bank database of as shown in relational schema below,


where the primary keys are underlined. Construct the following SQL queries for
this relational database
(4 x 2 = 8 Marks)

Page 1 of 6
Name Dated: 19th May 2022
Registration #

A. Find the ID of each customer of the bank who has an account but not a loan.

(Select ID From depositor) (Select ID From depositor)


EXCEPT MINUS
(Select ID From borrower) (Select ID From borrower)

Explanation:
We can see that account number is a foreign key in the depositor relation, and by domain
knowledge we know that if a person opens an account, he/she must deposit an initial amount.
Clearly, we can find IDs of all those who have account using the depositor relation. Similarly, a
customer’s id will only exist in borrower relation if and only if there is some loan against him

B. Find the ID of each customer who lives on the same street and in the same city as customer '12345' .

Select F.ID
From customer as F, customer as S
Where F.customer_street = S.customer_street
AND F.customer_city = S.customer_city
AND S.customer_id = ‘12345’

C. Find the name of each branch that has at least one customer who has an account in the bank and who lives
in “Harrison”.
Select DISTINCT branch_name
From account, depositor, customer
Where customer.id = depositor.id
AND depositor.account_number = account.account_number
AND customer_city = ‘Harrison’

Page 2 of 6
Name Dated: 19th May 2022
Registration #

D. Find the names of all branches that have assets greater than those of at least one branch located in
“Brooklyn”.

Select branch_name
From branch
Where assets > some
(
Select assets
From branch
Where branch.city = ‘Brooklyn’
)

Question 3. Consider the employee database as shown in the figure below, where
the primary keys are underlined. Construct the following SQL queries for this
relational database
(2 x 3 = 6 Marks)

A. Give all employees of “First Bank Corporation” a 10 percent raise.

B. Give all managers of “First Bank Corporation” a 10 percent raise.

Page 3 of 6
Name Dated: 19th May 2022
Registration #

C. Delete all tuples in the works relation for employees of “Small Bank Corporation”.

Question 4. Considering the employee relation as shown in the figure, write a


correlated subquery to find employees who earn a salary greater than the
average salary for their respective department.
Also, the output is needed in following format:
EMPNO ENAME SAL DEPTNO

(3 Marks)

Select E.Ssn AS EMPNO, CONCAT(E.Fname, “ ”, E.Minit, “ “, E.Lname) AS


EName, E.Salary AS SAL, E.Dno AS DEPTNO
From EMPLOYEE E
Where SAL >
(
Select AVG(Salary)
From EMPLOYEE
Where DEPTNO = E.DEPTNO
)

Question 5. Considering the following data (tables and schema), answer the
questions by writing queries using JOINS.
(3 x 2 = 6 Marks)

Page 4 of 6
Name Dated: 19th May 2022
Registration #

A. Write query to obtain date, item_name, is_veg, price, and units_sold using both tables, however, it is not
necessary that data between both tables must match i.e. there is a possibility that some information is missing
from either of the two tables

SELECT
d.date,
i.name,
i.is_veg,
d.price,
d.units_sold
FROM daily_menu d
FULL OUTER JOIN menu_items i
ON d.item_id = i.item_id

Page 5 of 6
Name Dated: 19th May 2022
Registration #

B. Write query to obtain date, item_name, is_veg, price, and units_sold using both tables, it is essential that all
information from daily_menu table must be present however, there maybe cases that some information is
missing from menu_items table

SELECT
d.date,
i.name,
i.is_veg,
d.price,
d.units_sold
FROM daily_menu d
LEFT JOIN menu_items i
ON d.item_id = i.item_id

Page 6 of 6

You might also like