Department of Cyber Security: Faculty of Computing and Artificial Intelligence
Department of Cyber Security: Faculty of Computing and Artificial Intelligence
Department of Cyber
Security
FACULTY OF COMPUTING AND ARTIFICIAL INTELLIGENCE
Class: BSCYS-4
Lab (no): 04
Department of Cyber
Security
Lab 04
Instructions
Submission: Combine all your work (solution folder) in one .zip file. Use proper naming
convention for your submission file. Name the .zip file as SECTION_ROLL-NUM_01.zip
(e.g. A_20123_01.zip). Submit .zip file on Google Classroom within the deadline. Failure
to submit according to the above format would result in deduction of 10% marks.
Submissions on the email will not be accepted.
Plagiarism: Plagiarism cases will be dealt with strictly. If found plagiarized, both the
involved parties will be awarded zero marks in this assignment, all of the remaining
assignments, or even an F grade in the course. Copying from the internet is the easiest
way to get caught!
Tip: For timely completion of the assignment, start as early as possible. Furthermore,
work smartly - as some of the problems can be solved using smarter logic.
Note: Follow the given instructions to the letter, failing to do so will result in a zero.
Page 2
Air University, Islamabad
Department of Cyber
Security
Introduction
SQL DDL (Data Definition Language) commands are used to create and modify the
databases. Data Manipulation Language (DML) commands are used to query the
databases.
Objectives
After performing this lab students should be able to:
Tools/Software Requirement
• MySQL Community Server 5.6
• MySQL Workbench 6.1
Description
Nested Queries/Subqueries
Rule of thumb: avoid writing nested queries when possible; keep in mind that sometimes
it’s impossible
Nested queries
• Can return a single value and this value can be compared with another value in
a WHERE clause
Page 3
Air University, Islamabad
Department of Cyber
Security
• Can return relations that can be used in various ways in WHERE clauses
• Can appear in FROM clauses, followed by a table variable that represents the
tables in the result of the subquery
• Can appear as computed values in a SELECT clause
Aggregate Functions
The functions are used to summarize information from multiple tables into a single-table
summary. Well known built-in aggregate functions are COUNT, SUM, MAX, MIN, and AVG.
HAVING clause
Note: Aggregate functions can be used in the SELECT clause or in a HAVING clause.
Student (snum: integer, sname: char(30), major: char(25), level: char(2), age:
Class (cname: char(40), meets_at: char(20), room: char(10), fid: integer | fid REFS Faculty.fid)
Enrolled (snum: integer, cname: char(40) | snum REFS student.snum, cname REFS class.name)
• Find the name of faculty members who do not teach any course.
Select distinct f.fname
From faculty f
Where f.fid not in
(select c.fid from class c);
Page 4
Air University, Islamabad
Department of Cyber
Security
From class C, enrolled E, faculty F
Where E.cname = C. cname and C.fid = F.fid and f.fname = 'I. Teach')
• Find the names of all students who are enrolled in two classes that meet at the
same time.
select distinct S.sname from student S
Where S.snum in (select E1.snum
from enrolled E1, enrolled E2, class C1, class C2 where E1.snum =
E2.snum and E1.cname <> E2.cname and E1.cname = C1.cname and
E2.cname = C2.cname and C1.meets_at = C2.meets_at)
• Find the class names, and their rooms of all classes that have five or more students
enrolled in it.
Select C.cname, C.room
From class C, enrolled E
Where C.cname = E.cname
Group by E.cname
Having COUNT (*) >= 5;
Lab Task
Write SQL expressions for each of the following queries and execute them:
1. Find the names of all juniors (Level = JR) who are enrolled in a class taught by ‘Ivana
Teach’.
2. Find the names of faculty members that has taught classes only in room R128.
3. Find the names of classes taught by ‘Richard Jackson’ and their times when a class
meet there.
4. Find the names of students majoring in ‘Computer Science’.
Page 5
Air University, Islamabad
Department of Cyber
Security
5. Find the names of classes taught by ‘John Williams’ in dept # 68.
6. For each class taught by ‘John Williams’, retrieve the name and age for students.
7. Find the names of students in ‘Computer Science’ major in descending age-wise.
8. Find distinct student ages in ‘Database Systems’ class in descending order.
9. List the name of ‘Christopher Garcia’s teachers.
10. Retrieve the snum and sname of students who have taken classes from both
‘Ivana Teach’ and ‘Linda Davis’. (Both with simple and nested queries)
11. Find average age of students.
12. Find the average age of student in course: ‘Organic Chemistry’.
13. Find eldest student.
14. Find youngest student in ‘Electrical Engineering’ major.
15. Find eldest student in each level.
16. Find average age of students in each level
17. Find the number of distinct class rooms
18. Find the strength of students in each major.
19. Find strength of students in course: ‘Urban Economics’
20. Find the number of courses for student: ‘Karen Scott’.
Deliverables
1. Complete your lab tasks in SQL workbench and submit a word file in with queries along
with the screenshots of the results to all the questions attempted. Upload it on LMS.
The marking will be based on viva/lab task submitted.
Page 6