Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

DBMS (SUMMER Lala Rukh Ahsan

2022)
LALA RUKH AHSAN 1
LESSON’S OUTLINE
Advanced SQLizing

Aggregations (examples based on yesterday’s lecture)


Getting around INTERSECT and EXCEPT
Subqueries

LALA RUKH AHSAN 2


LETS REVIEW…

Select <List of Columns and expressions (usually involving


columns)>
From <List of Tables & Join Operators>
Where <List of Row conditions joined together by And, Or, Not>
Group By <list of grouping columns>
Having <list of group conditions connected by And, Or, Not >
Order By <list of sorting specifications>

LALA RUKH AHSAN 3


SQL – DISTINCT
Eliminates all the duplicate entries in the table resulting from the query.
Syntax:
Select [DISTINCT] select_list
From table[, table, …]
[Where expression]
[Order By expression]
Example:
Select DISTINCT studio_id, director_id
From Movies

studio_id director_id
1 1
2 2
2 10
3 1
3 9

LALA RUKH AHSAN 4


SQL – Order By
Used to sort the results based on contents of a column
Multiple levels of sort can be done by specifying
multiple columns
An expression can be used in Order By clause
Syntax:
Select function(column)
From table1 [, table2 …]
[Where condition]
[Order By {Column | alias | position} [ASC | DESC]]

LALA RUKH AHSAN 5


SQL – Order By
Example: Sort Movies by profits in Ascending order
Select MovieTitle, Gross, Budget, (Gross – Budget) as profits
From movies
Order BY profits

Movie_title Gross Budget Profit

67.5 70 -2.5
Great Escape
Upside Down 54 50 4

Green Warrior 96 80 16

Blue Oranges 28 7 21

LALA RUKH AHSAN 6


Aggregate Queries – Group By
Categorizes the query results according to the contents of a
column in the database
Multiple levels of subgroups can be created by specifying
multiple columns
Syntax:
Select column1, [column2, …]
From table [, table …]
[Where condition]
Group By column1, [column2, ….]
Having [Condition]

LALA RUKH AHSAN 7


Aggregate Queries – Group By
Example: Get # of movies by each director for each studio
Select studio_id, director_id, count(*)
From Movies
Group By director_id, studio_id

Example: Get # of movies by each studio ordered by studio_id


Select studio_id, count(*)
From Movies
Group By studio_id
Order By studio_id

LALA RUKH AHSAN 8


Aggregate Queries – Group By
Example:
Select studio_id, Sum(budget)
From movies
Group by studio_id
Having Sum(budget) > 60

Example:
Select studio_id, count(*)
From Movies
Group By studio_id
Order By studio_id

LALA RUKH AHSAN 9


Aggregate Queries
Aggregate queries provides a more holistic view of
the data by further processing the retrieved data.
They can work on
 On all the rows in a table
 A subset of rows in a table selected using a where clause
 Groups of selected data organized using Group By clause.
Syntax:
Select function(column)
From <list of tables>
Where <condition>
Group By <list of columns>
Having <condition>

LALA RUKH AHSAN 10


Aggregate Queries
Functions:
 Sum() Returns a sum of the column
 Count() Returns a total number of rows returned by a query
 Avg() Returns the average of a column
 Min() Returns minimum value of the column returned by
query
 Max() Returns maximum value of the column returned by
query
Notes 1: Count function does not include columns containing null values in total
Notes 2: Count can be used with distinct to count the number of distinct rows

Example:
Query: Select sum(budget)
From movies
Where studio_id = 3

Output: Sum(budget)
---------------
65.1
LALA RUKH AHSAN 11
AGGREGATION
SELECT
SELECT avg(price)
avg(price) SELECT
SELECT count(*)
count(*)
FROM
FROM Product
Product FROM
FROM Product
Product
WHERE
WHERE maker=“Toyota”
maker=“Toyota” WHERE
WHERE year
year>>2020
2020

SQL supports several aggregation operations:

sum, count, min, max, avg

Except count, all aggregations apply to a single attribute

LALA RUKH AHSAN 12


AGGREGATION: COUNT
COUNT applies to duplicates, unless otherwise stated:

SELECT same as Count(*)


SELECT Count(category)
Count(category)
FROM
FROM Product
Product
WHERE
WHERE year
year>>2020
2020

We probably want:

SELECT
SELECT Count(DISTINCT
Count(DISTINCTcategory)
category)
FROM
FROM Product
Product
WHERE
WHERE year
year>>2020
2020

LALA RUKH AHSAN 13


MORE EXAMPLES
Purchase(product, date, price, quantity)

SELECT
SELECT Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase

SELECT
SELECT Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase
WHERE
WHERE product
product==‘mobile’
‘mobile’

LALA RUKH AHSAN 14


SIMPLE AGGREGATIONS
Purchase
Product Date Price Quantity
mobile 10/21 1 20
Laptop 10/3 0.5 10
Laptop 10/10 1 10
mobile 10/25 1.50 20

SELECT
SELECT Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase 50 (= 20+30)
WHERE product==‘‘mobile
WHERE product mobile’’
LALA RUKH AHSAN 15
GROUPING AND
AGGREGATION
Purchase(product, date, price, quantity)

Find total sales after 10/1/2020 per product.

SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2020’
‘10/1/2020’
GROUP
GROUPBY
BY product
product

LALA RUKH AHSAN 16


GROUPING AND
AGGREGATION
1. Compute the FROM and WHERE clauses.

2. Group by the attributes in the GROUPBY

3. Compute the SELECT clause: grouped attributes and aggregates.

LALA RUKH AHSAN 17


1&2. FROM-WHERE-GROUPBY

Product Date Price Quantity


Mobile 10/21 1 20
Mobile 10/25 1.50 20
Laptop 10/3 0.5 10
Laptop 10/10 1 10

LALA RUKH AHSAN 18


3. SELECT
Product Date Price Quantity Product TotalSales
Mobile 10/21 1 20
Mobile 10/25 1.50 20 Mobile 50
Laptop 10/3 0.5 10
Laptop 15
Laptop 10/10 1 10

SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2005’
‘10/1/2005’
GROUP
GROUPBY
BY product
product

LALA RUKH AHSAN 19


ANOTHER EXAMPLE

SELECT
SELECT product,
product,
sum(price
sum(price**quantity)
quantity)AS
ASSumSales
SumSales
max(quantity)
max(quantity)AS
ASMaxQuantity
MaxQuantity
FROM
FROM Purchase
Purchase
GROUP
GROUPBY
BYproduct
product

LALA RUKH AHSAN 20


HAVING CLAUSE
Same query, except that we consider only products that had
at least 100 buyers.

SELECT
SELECT product,
product,Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2020’
‘10/1/2020’
GROUP
GROUPBY
BYproduct
product
HAVING
HAVING Sum(quantity)
Sum(quantity)>>3030

HAVING clause contains conditions on aggregates.

LALA RUKH AHSAN 21


GENERAL FORM OF
GROUPING AND
AGGREGATION
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2

S = may contain attributes a1,…,ak and/or any aggregates


C1 = is any condition on the attributes in R1,…,Rn
C2 = is any condition on aggregate expressions

LALA RUKH AHSAN 22


GENERAL FORM OF
GROUPING AND
AGGREGATION
SELECT
SELECT SS
FROM
FROM RR1,…,R
1,…,Rn
n
WHERE
WHERE C1
C1
GROUP
GROUPBY
BYaa1,…,a
1,…,ak
k
HAVING
HAVING C2C2

Evaluation steps:
1. Evaluate FROM-WHERE, apply condition C1
2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)
4. Compute aggregates in S and return the result
LALA RUKH AHSAN 23
INTERSECT and EXCEPT: not in SQL Server

1. INTERSECT AND
EXCEPT: If R, S have no
duplicates, then can
write without
subqueries
(SELECT
(SELECTR.A,
R.A,R.B
R.B SELECT
SELECTR.A,
R.A,R.B
R.B (HOW ?)
FROM
FROM R) R) FROM
FROM RR
INTERSECT
INTERSECT WHERE
WHERE
(SELECT
(SELECTS.A,
S.A,S.B
S.B EXISTS(SELECT
EXISTS(SELECT**
FROM
FROM S) S) FROM
FROMSS
WHERE
WHERER.A=S.A
R.A=S.Aand
andR.B=S.B)
R.B=S.B)

(SELECT
(SELECTR.A,
R.A,R.B
R.B SELECT
SELECTR.A,
R.A,R.B
R.B
FROM
FROM R)R) FROM
FROM RR
EXCEPT
EXCEPT WHERE
WHERE
(SELECT
(SELECTS.A,
S.A,S.B
S.B NOT
NOT EXISTS(SELECT
EXISTS(SELECT**
FROM
FROM S)S) FROM
FROMSS
WHERE
WHERER.A=S.A
R.A=S.Aand
andR.B=S.B)
R.B=S.B)
LALA RUKH AHSAN 24
Nested Queries
A sub query is a query nested within another query
 The enclosing query also called outer query
 Nested query is called inner query
There can be multiple levels of nesting
Example:
Select movie_title
From movies
Where director_id IN (
Select person_id
From People
Where person_state = ‘TX’)
LALA RUKH AHSAN 25
Nested Queries - Types
Non-Correlated Sub Queries:
 Requires data required by outer query before it can be executed
 Inner query does not contain any reference to outer query
 Behaves like a function

Example:
People(person_fname, person_lname, person_id, person_state, person_city)
Movies(movie_id, movie_title, director_id, studio_id)
Select movie_title, studio_id
From Movies
Where director_id IN (
Select person_id
From People
Where person_state = ‘TX’)

Steps:
1. Subquery is executed
2. Subquery results are plugged into the outer query
3. The outer query is processed
LALA RUKH AHSAN 26
Nested Queries - Types
Correlated Sub Queries:
 Contains reference to the outer query
 Behaves like a loop
Example:
People(person_fname, person_lname, person_id, person_state, person_city)
Cast_Movies(cast_member_id, role, movie_id)
Select person_fname, person_lname
From People p1
Where ‘Pam Green’ in (
Select role
From Cast_Movies
Where p1.person_id = cast_member_id
)
Steps:
 Contents of the table row in outer query are read
 Sub-query is executed using data in the row being processed.
 Results of the inner query are passed to the where in the outer query
 The Outer query is Processed

LALA RUKH AHSAN 27


Slide 8-28 LALA RUKH AHSAN
SO NESTING OF QUERIES
MEANS…
A complete SELECT query, called a nested query , can be specified
within the WHERE-clause of another query, called the outer query
Many of the previous queries can be specified in an alternative form
using nesting
Query : Retrieve the name and address of all employees who work
for the 'Research' department.
Q: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research' )

Slide 8-29 LALA RUKH AHSAN


NON CORRELATED
NESTED QUERIES
The nested query selects the number of the 'Research' department
The outer query select an EMPLOYEE tuple if its DNO value is in the
result of either nested query
The comparison operator IN compares a value v with a set (or multi-set) of
values V, and evaluates to TRUE if v is one of the elements in V
In general, we can have several levels of nested queries
A reference to an unqualified attribute refers to the relation declared in the
innermost nested query
In this example, the nested query is not correlated with the outer query

Slide 8-30 LALA RUKH AHSAN


CORRELATED NESTED
QUERIES
If a condition in the WHERE-clause of a nested query references an
attribute of a relation declared in the outer query , the two queries are
said to be correlated
The result of a correlated nested query is different for each tuple (or
combination of tuples) of the relation(s) the outer query
Query: Retrieve the name of each employee who has a dependent with
the same first name as the employee.
Q: SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT ESSN
FROM DEPENDENT
WHERE ESSN=E.SSN
AND
E.FNAME=DEPENDENT_NAME)
Slide 8-31 LALA RUKH AHSAN
So, you are
prepared for
QUIZ 3 tomorrows quiz
(All about SQL)
Isn't it? 
LALA RUKH AHSAN 32

You might also like