Introduction to Database System
Fall 2017
Lab Final Paper
Time Allowed: 3 hours
Name: Muhammad Hassan Riaz Yousufi
Registration No
Serial Number (of attendance
sheet)
Section
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Overall
Total Marks 10 10 10 10 10 10 10 10 10 10 100
Ob. Marks
Instructions:
1) Understanding of question is part of paper. Therefore, no queries will be entertained during
examination.
2) abc_school.sql and northwind.sql files are uploaded on LMS. Use those files to attempt paper.
3) You can use any method or approach to solve a query unless you are explicitly instructed to use
specific approach.
4) Use proper indentation/formatting while writing queries.
5) You need to make a MS Word file of your solution + need to submit handwritten hardcopy too.
6) You need to write only queries on handwritten copy (not output table), but in MS Word file, you
need to write query (text form) + its output table (picture) if any.
7) Output table Column heading is given in paper, your output table column heading must match with it.
8) SOME ONE is always with You, so be Relaxed. SOME ONE is always watching You, so be Honest.
Keep Calm & stay Blessed.
abc_School Schema
Table Data
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Q 1: Mistakenly, Student_Contact Table is deleted, now write a query to re-create it. See Schema for
its Structure. Also Insert data in it. (5 + 5 Marks)
create table if not exists Student_Contact(
PhoneNumber varchar(25),
StudentID int,
primary key (PhoneNumber, StudentID),
foreign key(StudentID) references Student (StudentID));
Insert into Student_Contact (PhoneNumber, StudentID) values ("0313-1234567", 1);
Insert into Student_Contact (PhoneNumber, StudentID) values ("0321-1234567", 1);
Insert into Student_Contact (PhoneNumber, StudentID) values ("0333-1234567", 2);
Insert into Student_Contact (PhoneNumber, StudentID) values ("0345-1234567", 7);
Insert into Student_Contact (PhoneNumber, StudentID) values ("0300-1234567", 8);
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Q 2: Write a query that display total count of Male and Female Student. (2+ 8 MARKS)
Total Rows in Output: ___2______
Select s.Gender as Gender, count(Gender) as 'Total Strength'
from Student s
group by gender;
Q 3: Write a query that display all Students name, with their gender, Father Name and Mother Name
(USE JOINS). (2+ 8 MARKS)
Total Rows in Output: __14______
select s.Sname as StudentName, s.Gender as Gender, f.GName as FatherName, m.Gname as
MotherName
from Guardian m
join
Student s on s.MotherID = m.GuardianId
join
Guardian f on s.FatherId = f.GuardianId;
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Q 4: Write a query that display name of only those students whose name are unique in School. (No
other student has that same name). (2+ 8 MARKS)
Total Rows in Output: __10______
Select s.Sname as 'Student Name'
from Student s
Group by s.Sname
having count(s.Sname)=1;
Q 5: Write a query that display Name of students with count of their real/step brother or Sister
enrolled in School. (2+ 8 MARKS)
Total Rows in Output: __14______
Select s.StudentId as 'Student Id', S.Sname as 'Student Name', count(f.Sname) as 'Sibling Count'
from Student S, Student f
where (s.FatherId = f.FatherID || S.MotherId = f.MotherId) and s.StudentId != f.StudentID
group by s.StudentId, S.Sname;
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Q 6: Write a query that display all Father(Husband) Name in Alphabetical order from Guardian Table
with Total number of their Wives. (Use Nested Query to find Count of Wives). (2+ 8 MARKS)
Total Rows in Output: __5______
Select h.GName as 'Husband Name' , (Select Count(w.HusbandId)
from Guardian w
where w.HusbandId = h.GuardianId) as 'Wife Count'
from Guardian h
where h.HusbandId is NULL
order by h.GName;
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Northwind DB Queries
Q 7: Write a query that displays list of all different first name of employees from employees table in
ascending order. (2+ 8 MARKS)
Total Rows in Output: __9______
SELECT DISTINCT e.first_name as 'First Name'
FROM employees e
ORDER BY e.first_name
Q 8: Write a query that displays total count of those order which has no shipping fee. (2+ 8 MARKS)
Total Rows in Output: __1______
SELECT COUNT(*) as 'Total Order with free shipping'
FROM orders o
WHERE o.shipping_fee = 0
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Q 9: Write a query that displays Name of that Products in ascending order which has not been
purchased by any supplier yet. (2+ 8 MARKS)
Total Rows in Output: __16_____
SELECT p.product_name as 'Product Name'
FROM products p
LEFT OUTER JOIN
purchase_order_details pod on p.id = pod.product_id
WHERE pod.id is null
ORDER by p.product_name
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Q 10: Write a query that display Customers First Name and Last Name that has maximum numbers of
invoices. (Note: You will not Hardcode your query.) (2+ 8 MARKS)
Total Rows in Output: __2____
SELECT c.first_name as 'First Name', c.last_name as 'Last name'
FROM customers c
INNER join
orders o
INNER join
invoices I on c.id = o.customer_id and o.id = i.order_id
GROUP by c.first_name, c.last_name
HAVING COUNT(i.id) = (
SELECT COUNT(i.id)
FROM customers c
INNER join
orders o
INNER join
invoices I on c.id = o.customer_id and o.id = i.order_id
GROUP by c.first_name, c.last_name
ORDER by COUNT(i.id) desc
limit 1
ORDER by c.first_name
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 1
Introduction to Database System
Fall 2017
Lab Final Paper (Version 2)
Time Allowed: 3 hours
Name: Muhammad Hassan Riaz Yousufi
Registration No
Serial Number (of attendance
sheet)
Section
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Overall
Total Marks 10 10 10 10 10 10 10 10 10 10 100
Ob. Marks
Instructions:
1) Understanding of question is part of paper. Therefore, no queries will be entertained during
examination.
2) birth_certificate.sql and northwind.sql files are uploaded on LMS. Use those files to attempt paper.
3) You can use any method or approach to solve a query unless you are explicitly instructed to use
specific approach.
4) Use proper indentation/formatting while writing queries.
5) You need to make a MS Word file of your solution + need to submit handwritten hardcopy too.
6) You need to write only queries on handwritten copy (not output table), but in MS Word file, you
need to write query (text form) + its output table (picture) if any.
7) Output table Column heading is given in paper, your output table column heading must match with it.
8) SOME ONE is always with You, so be Relaxed. SOME ONE is always watching You, so be Honest.
Keep Calm & stay Blessed.
Northwind DB Queries
Q 1: Write a query that list of all different category name from products table in ascending order. (2+ 8
MARKS)
Total Rows in Output: ___16_____
SELECT DISTINCT Category
FROM products
ORDER BY Category
Q 2: Write a query that displays Total customer count in each order_date. (2+ 8 MARKS)
Total Rows in Output: _28_____
SELECT o.order_date as 'Order Date', COUNT(c.id) as 'Total Customer'
from customers c, orders o
where c.id = o.customer_id
group by (o.order_date)
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2
Q 3: Write a query that displays Name of that Products in ascending order which has not been ordered
by any customer yet. (2+ 8 MARKS)
Total Rows in Output: __21____
SELECT p.product_name as 'Product Name'
FROM products p
LEFT OUTER JOIN
order_details od on p.id = od.product_id
where od.order_id is null
ORDER by p.product_name
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2
Q 4: Write a query that display Customers First Name and Last Name that has maximum numbers of
invoices. (Note: You will not Hardcode your query.) (2+ 8 MARKS)
Total Rows in Output: __2____
SELECT c.first_name as 'First Name', c.last_name as 'Last name'
FROM customers c
INNER join
orders o
INNER join
invoices I on c.id = o.customer_id and o.id = i.order_id
GROUP by c.first_name, c.last_name
HAVING COUNT(i.id) = (
SELECT COUNT(i.id)
FROM customers c
INNER join
orders o
INNER join
invoices i
on c.id = o.customer_id and o.id = i.order_id
GROUP by c.first_name, c.last_name
ORDER by COUNT(i.id) desc
limit 1
ORDER by c.first_name
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2
Birth_Certificate Schema
Table Data
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2
Q 5: Write a query that display all Father(Husband) Name in Alphabetical order from Parent Table with
Total number of their Wives. (Use Nested Query to find Count of Wives). (2+ 8 MARKS)
Total Rows in Output: _5____
Select h.PName as 'Husband Name' , (Select Count(w.HusbandId)
from Parent w
where w.HusbandId = h.ParentId) as 'Wife Count'
from Parent h
where h.HusbandId is NULL
order by h.PName;
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2
Q 6: Write a query that display Name of children with count of their real/step brother or Sister. (2+ 8
MARKS)
Total Rows in Output: ___14___
Select c.ChildrenID as 'Children Id', c.Cname as 'Children Name', count(f.Cname) as 'Sibling Count'
from Children c, Children f
where (c.FatherId = f.FatherID || c.MotherId = f.MotherId) and c.ChildrenID != f.ChildrenID
group by c.ChildrenID, c.Cname;
Q 7: Write a query that display name of only those children whose name are unique. (No other child
has that same name). (2+ 8 MARKS)
Total Rows in Output: __10___
Select c.Cname as 'Baby Name'
from Children c
Group by c.Cname
having count(c.Cname)=1;
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2
Q 8: Write a query that display all Childrens name, with their gender, Father Name and Mother Name
(USE JOINS). (2+ 8 MARKS)
Total Rows in Output: __14____
select c.Cname as 'Baby Name', c.Gender as 'Gender', f.PName as 'Father Name', m.Pname as 'Mother
Name'
from Parent m
join
Children c on c.MotherID = m.ParentId
join
Parent f on c.FatherId = f.ParentId;
Q 9: Write a query that display total count of Male and Female Children. (2+ 8 MARKS)
Total Rows in Output: __2____
Select c.Gender as Gender, count(Gender) as 'Total Strength'
from Children c
group by gender;
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2
Q 10: Mistakenly, Parent_Contact Table is deleted, now write a query to re-create it. See Schema for
its Structure. Also Insert data in it, present above in table. (5 + 5 Marks)
create table if not exists Parent_Contact(
PhoneNumber varchar(25),
ParentID int,
primary key (PhoneNumber, ParentID),
foreign key(ParentID) references Parent (ParentID));
Insert into Parent_Contact (PhoneNumber, ParentID) values ("0313-1234567", 1);
Insert into Parent_Contact (PhoneNumber, ParentID) values ("0321-1234567", 1);
Insert into Parent_Contact (PhoneNumber, ParentID) values ("0333-1234567", 3);
Insert into Parent_Contact (PhoneNumber, ParentID) values ("0345-1234567", 5);
Insert into Parent_Contact (PhoneNumber, ParentID) values ("0300-1234567", 8);
The STRUGGLE youre in TODAY is developing the STRENGTH you need for TOMORROW
Version 2