SQL Queries5
SQL Queries5
Advanced SQL
DR. RICHA SHARMA
L O C K H AV E N U N I V E R S I T Y
1
Review of Select Query
Most frequently used DML query – meant to retrieve
data from one or more tables (join) based on conditions
we request.
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
2
Review of Select Query
We can use with select query:
Wildcard character * to fetch all columns
Logical operators within Where clause
4
Review of Select Query
Aggregate Operators:
COUNT – returns the number of non-null values of an
attribute.
MAX & MIN
SUM – computes total for a specified attribute.
AVG - computes average for a specified attribute.
select * from EMP where EMP_MGR > (select
avg(EMP_MGR) from EMp);
An example: SELECT COUNT(*), AVG(P_PRICE) FROM
PRODUCT;
5
Review of Select Query
Aggregate Operators and grouping: when we want to
compute results of aggregate operator on sub-groups
within the data, i.e. work with frequency distribution!
Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[GROUP BY columnlist]
[HAVING conditionlist]
[ORDER BY columnlist [ASC | DESC] ] ;
6
SQL JOIN
Operators
7
Types of Join
Inner join returns only rows from the tables that match on a common
value.
Outer join returns the same matched rows as the inner join, plus
unmatched rows from one table or the other (can be left, right or full
outer join).
8
Tables Join (ctd.)
We use alias to identify the source table from which data
is selected – useful with recursive joins!
9
Self or Recursive Join
When a table is joined to itself!
Special Type of Inner Join.
ON E.EMP_MGR = M.EMP_NUM
10
SQL Subqueries
11
Subqueries
Often, it is necessary to process data based on other
processed data!
Subquery is a query inside a query, normally inside
parentheses. Thus, we have:
Outer query
Inner query or nested query
12
WHERE Subqueries
Most common type uses inner SELECT subquery on right
side of WHERE comparison requires a subquery that
returns only one single value (comparison could be =, >, <,
>=, <=)
Example:
SELECT p_code, p_price FROM Product WHERE p_price >=
(SELECT AVG(p_price) FROM Product)
13
IN Subqueries
When we want to compare a single attribute to a list of
values, we use IN operator (NOT IN works as well!)
Example:
select cus_code from customer where cus_code in (select
cus_code from invoice)
14
HAVING Subqueries
Just like WHERE clause, we can use subquery with
HAVING clause.
Example:
select P_CODE, SUM(LINE_UNITS) as total FROM LINE
15
FROM Subqueries
Specifies the tables from which the data will be drawn
Example:
Note: there can be more than one attribute in nested select query. *
is also allowed if we don’t have columns by same name on the
joined tables!
16
EXISTS Subqueries
EXISTS special operator can be used whenever there is a
requirement to execute a command based on the result of
another query!
If a subquery returns any rows, run the main query;
otherwise, do not!
FOR X = 1 TO 2
{
FOR Y = 1 TO 3
{
PRINT "X = "X, "Y = "Y
}
}
18
Correlated Subquery
Exists query is an example of correlated subquery!
Another example: this query lists all product sales in which the
units sold value is greater than the average units sold value
for that product (as opposed to the average for all products).
19
ALL & ANY Subqueries
Useful for inequality comparison of one value to a list of
values!
For instance, we can use the ALL to know which
products cost more than all individual products provided by
vendors from Florida:
20