Database Lab Manual 5 FAST NUCES
Database Lab Manual 5 FAST NUCES
Database Lab Manual 5 FAST NUCES
Lab Manual 4
“Nested Queries”
1.Contents
2. Objective................................................................................................................................................................2
3. Pre-requisites..........................................................................................................................................................2
4. Nested Queries.......................................................................................................................................................3
A subquery (inner query) is a SQL select query nested inside a another select query (outer query)...............................3
A subquery can be nested inside:......................................................................................................................................3
There are two types of subqueries....................................................................................................................................3
Scalar Vs Non-scalar.........................................................................................................................................................3
Non-Correlated Query................................................................................................................................................4
Non-Correlated Subqueries in SELECT clause................................................................................................................4
Non-Correlated Subqueries in From Clause.....................................................................................................................4
Non-Correlated Subqueries in Where Clause...................................................................................................................5
Correlated queries.......................................................................................................................................................5
Correlated Subquery in Select Clause...............................................................................................................................5
Correlated Subquery in Where Clause..............................................................................................................................6
Correlated Subquery in Having Clause.............................................................................................................................6
Aggregate Functions
Page 1
FAST NU Lahore Database Systems Lab - CL 218
2. Objective
The purpose of this manual is to get started with nested queries. This lab will cover all the topics we have
covered before. Starting from simple Select-From-Where, Joins, Order by, Aggregate functions & Group
by, all of these will be used in combination with the nested queries.
3. Pre-requisites
Lab manual 2 & 3 which includes:
o Select-From-Where clause
o Joins and all its types
Task Distribution
Total Time 170 Minutes
Nested Queries 30 Minutes
Exercise 120 Minutes
Evaluation Last 20 Minutes
Page 2
FAST NU Lahore Database Systems Lab - CL 218
4. Nested Queries
For this in-lab manual, use the InLab5TryThisSchema.sql script to create database and practice the queries given
below.
A subquery (inner query) is a SQL select query nested inside a another select query (outer query)
A subquery may occur in:
SELECT clause of outer query
FROM clause of outer query
WHERE clause of outer query (most commonly used)
Scalar Vs Non-scalar
A select query can return a scalar value or a table. Scalar value means one column and one row
Example: result of the following query is scalar
A select query can also return non-scalar value, with more than one column and/or more than one row
Example:
Select StudentID from Students
Will give non-scalar result.
If you are writing a sub query in Select Clause, the inner query should be Scalar
If you are writiing a subquey in From Clause, inner query can be scalar or non-Scalar
If you are writing a subquery in Where Clause, inner query can be scalar or non-Scalar depending on conditon.
Page 3
FAST NU Lahore Database Systems Lab - CL 218
Non-Correlated Query:
Non-Correlated Subqueries in SELECT clause
SELECT <List of columns of T>
(select ColumnName from <TableName>)
FROM <tablename> AS T
WHERE <condition>
**inner query should be scalar
TRY THIS
Page 4
FAST NU Lahore Database Systems Lab - CL 218
TRY THIS
Correlated queries
When inner query is correlated with outer query, then the inner query is executed for each row of outer query.
Correlated Subquery in Select Clause
TRY THIS
Page 5
FAST NU Lahore Database Systems Lab - CL 218
TRY THIS
Modify the query given above to, Show name, IDs, Calcuated CGPA and CGPA given in Student table of all the
students with CGPA given in student table lesser to calcuated CGPA
Page 6
FAST NU Lahore Database Systems Lab - CL 218
5. Aggregation-Grouping
Aggregation allows you to apply calculation on values of column, and it will return a scalar value. Adding the GROUP
BY Clause allows you to aggregate on groups of data, a scalar value will be returned for each group of data.
Some examples of Aggregate functions are given below.
Aggregation Function Key work How it works No of Column Function can work on
AVG() Returns the average of the values in a Single column
group. Null values are ignored.
COUNT() Returns the number of items in a Single Column or List of Columns or *
group. This function always returns
an int data type value
MAX() Returns the maximum value in the Single column
expression.
MIN() Returns the minimum value in the Single column
expression.
SUM() Returns the sum of all the values in the Single column
expression. SUM can be used on
numeric columns only and it ignores all
the NULL values.
Figure 1 Aggregation Functions
Use the script (Lab4TryManual.sql Figure 1) to create database to try the following queries.
Page 7
FAST NU Lahore Database Systems Lab - CL 218
Page 8
FAST NU Lahore Database Systems Lab - CL 218
Grouping:
Syntax:
NOTE: ONLY THE COLUMNS THAT ARE USED IN GROUPING CAN BE USED IN SELECT CLAUSE
Page 9
FAST NU Lahore Database Systems Lab - CL 218
Having Clause
Having Clause allows us to filter the data based on the result of aggregation function, it’s the same as where clause
except that we cannot use aggregate functions in where clause and we cannot use simple columns having clause.
1. SELECT (COMPULSORY)
2. FROM (COMPULSORY)
3. WHERE
4. GROUP
5. HAVING
Page 10