rdbms- day 4 - wordpress.com · 1 rdbms- day 4 •grouped results •sub queries •relational...
TRANSCRIPT
1
RDBMS- Day 4
•Grouped results•Sub queries•Relational algebra•Use of EXISTS and NOT EXISTS
In today’s session we will discuss about the concept of subqueries,
2
Grouped results
3
ER/CORP/CRS/DB07/003
Version No: 2.03Copyright © 2004,
Infosys Technologies Ltd
SQL - Using GROUP BY
• Related rows can be grouped together by GROUP BY clause by specifying a column as a grouping column.
• GROUP BY is associated with an aggregate function• Example: For each part supplied get the part number and the total
shipment quantity
SELECTSELECT PNO, SUM(QTY)
FROMFROM SP
GROUP BY GROUP BY PNO
In the output table all the rows with an identical value in the grouping column will be grouped together.
4
ER/CORP/CRS/DB07/003
Version No: 2.04Copyright © 2004,
Infosys Technologies Ltd
SELECTSELECT SNO, PNO, SUM(QTY)
FROMFROM SP
GROUP BY GROUP BY PNO
SELECTSELECT SNO, PNO, SUM(QTY)
FROMFROM SP
GROUP BY GROUP BY SNO, PNO
Retrieval using GROUP BY
Get SNO, PNO, total qty for each part supplied
Why the first form is considered wrong is that, for each part , multiple suppliers would have supplied the part in different combinations. For e.g say for a part p1, s1 might have supplied twice, s2 once , s3 thrice etc. so we want the results to be grouped based on part number and within that by supplier number and take the count.
5
ER/CORP/CRS/DB07/003
Version No: 2.05Copyright © 2004,
Infosys Technologies Ltd
Get PNO for parts which have more than two shipments
SELECTSELECT PNO,COUNT(*) FROMFROM SP
GROUP BY GROUP BY PNO
HAVINGHAVING COUNT(*)>2
Retrieval using HAVING
• Used to specify condition on group
Get supplier numbers who have at least two shipments.
SELECTSELECT SNO , COUNT(*) FROMFROM SP
GROUP BY GROUP BY SNO
HAVINGHAVING COUNT(*)>=2
6
ER/CORP/CRS/DB07/003
Version No: 2.06Copyright © 2004,
Infosys Technologies Ltd
Can you identify any error…?
SELECTSELECT SNO , COUNT(*)
FROMFROM SP
GROUP BY GROUP BY SNO
HAVINGHAVING PNO=‘p2’;
Ans: The Having condition has to be based on some column that appears in the select list
7
Independent subqueries
8
ER/CORP/CRS/DB07/003
Version No: 2.08Copyright © 2004,
Infosys Technologies Ltd
Independent sub-queries
• Inner query is independent of outer query.
• Inner query is executed first and the results are stored.
• Outer query then runs on the stored results.
These are queries where there are two parts to the query. We need to collect one type of information based on which other set of information has to be retrieved from the table.
For e.g :
Select all sales reps who have a higher quota than sales rep 101.
We need to analyze this query and understand how to break it into sub problems
1. First we nee dto find out what is the quota of seles rep 101
2. Based on this info, we need to select sales reps who have a higher quota than this value
3. So, the inner query will find the quota of sales rep 101 and the outer query will extract sakes reps exceeding this quota value. The solution would look like:
SELECT Rep
FROM SalesReps
WHERE Quota >
SELECT Quota
FROM SalesReps
WHERE Empl_Num = 101;
9
ER/CORP/CRS/DB07/003
Version No: 2.09Copyright © 2004,
Infosys Technologies Ltd
Get supplier names for all suppliers who supply pa rt P2
SELECTSELECT SNAME
FROMFROM S
WHEREWHERE SNO IN
(SELECTSELECT SNO
FROMFROM SP
WHERE WHERE PNO =‘P2’)
Retrieval using SUB QUERIES
10
ER/CORP/CRS/DB07/003
Version No: 2.010Copyright © 2004,
Infosys Technologies Ltd
Get SNO for suppliers who are located in the same city as S1
SELECTSELECT SNO
FROM FROM SWHEREWHERE CITY =
(SELECTSELECT CITY
FROMFROM S
WHERE WHERE SNO=‘S1’)
Retrieval using SUB QUERIES
11
ER/CORP/CRS/DB07/003
Version No: 2.011Copyright © 2004,
Infosys Technologies Ltd
Get SNO for suppliers who supply at least one par t supplied by S2
SELECT SELECT SNO
FROMFROM SP
WHEREWHERE PNO IN
(SELECTSELECT PNO
FROMFROM SP
WHEREWHERE SNO=‘S2’)
Retrieval using SUB QUERIES
12
ER/CORP/CRS/DB07/003
Version No: 2.012Copyright © 2004,
Infosys Technologies Ltd
SELECTSELECT SNO
FROMFROM S
WHEREWHERE STATUS <
(SELECTSELECT STATUS
FROMFROM S
WHEREWHERE SNO=‘S1’)
Get SNO who have status less than the status of ‘S1 ’
Retrieval using SUB QUERIES
13
ER/CORP/CRS/DB07/003
Version No: 2.013Copyright © 2004,
Infosys Technologies Ltd
Get supplier numbers for suppliers with status les s than the current maximum in the supplier table
SELECTSELECT SNO
FROMFROM S
WHEREWHERE STATUS <
(SELECT MAXSELECT MAX(STATUS)
FROMFROM S)
Retrieval using SUB QUERIES
14
ER/CORP/CRS/DB07/003
Version No: 2.014Copyright © 2004,
Infosys Technologies Ltd
Get SNO for suppliers who do not supply any part supplied by S2
SELECTSELECT SNO FROMFROM SP
WHEREWHERE PNO NOT INNOT IN
(SELECT PNO FROM SP
WHERE SNO=‘S2’))
Retrieval using SUB QUERIES
15
ER/CORP/CRS/DB07/003
Version No: 2.015Copyright © 2004,
Infosys Technologies Ltd
For each part supplied, get the part number, maximu m quantity, and minimum quantity supplied for that part
SELECTSELECT SP.P#,
MAX (SP.QTY) ASAS MXQ,
MIN (SP.QTY) ASAS MNQ
FROMFROM SP
GROUP BY SP.P#;GROUP BY SP.P#;
Write a query for the following ...
Let us take few more examples and see how to solve:
1. Who are the customers whose rep is 102 and have at least one order more than $1000?
Select cust_name from customers
where cust_rep=102 and cust_num in
(select cust from orders
where amount > 1000)
2. Which product has the maximum total order amount?
Select product from orders group by product having sum(amount) =(select max(sum(amount)) from orders group by product);
16
ER/CORP/CRS/DB07/003
Version No: 2.016Copyright © 2004,
Infosys Technologies Ltd
Correlated Sub Queries
• You can refer to the table in the FROM clause of the outer query in the inner query using Correlated sub-queries.
• The inner query is executed separately for each row of the outer query.
17
ER/CORP/CRS/DB07/003
Version No: 2.017Copyright © 2004,
Infosys Technologies Ltd
Get PNO for all parts supplied by more than on e supplier
SELECTSELECT PNO
FROMFROM SP XX
WHEREWHERE PNO IN
(SELECTSELECT PNO
FROMFROM SP YY
WHEREWHERE YY.SNO<>XX.SNO)
Correlated Sub Queries
In this query, clearly there is a case of comparison of each row with some set of rows of the table to determine whether the given rows qualifieto go into the result. So, the typical way to solve is to select each row and check it with some other rows selected based on a criteria iteratively. Typically, the same table will be involved in both the outer query and the inner queryTo solve these type of correlated queries, we typically consider the same table as two copies each with a different name . This enables comparing the table with itself. To illustrate this, consider the above query. Let’s take a sample snapshot:SNO PNO JNO QtyS1 P1 J1 75S2 P3 J1 40S1 P1 J2 30S4 P3 J1 55Here, both p1 and p3 have been supplied twice. But p1 has been supplied by the same supplier and p3 by two different suppliers.So , when we consider the same table as two copies X and Y and compare, the processing would go something like this…X YSNO PNO JNO Qty SNO
PNO JNO QtyS1 P1 J1 75 S1 P1
J1 75S2 P3 J1 40 S2 P3
J1 40S1 P1 J2 30 S1 P1
J2 30S4 P3 J1 55 S4 P3
J1 55The inner query will be executed completely once for every row of the outer query. So, the result returned by the inner query would be Iteration 1 :Is p1 in (p3, p3) - not qualified -> not selectedIteration 2 :Is p3 in (p1, p1, p3) -> qualified -> selectedIteration 3:Is p1 in (p3,p3) -> not qualified -> not selectedIteration 4:Is p3 in (p1,p1,p3)-> qualified -> selectedSo the result will contain two rows displaying PnoP3P3To avoid this we may use in the outer query select distinct rather than select
18
ER/CORP/CRS/DB07/003
Version No: 2.018Copyright © 2004,
Infosys Technologies Ltd
SELECTSELECT DISTINCT SNO
FROMFROM SPJ X
WHEREWHERE PNO=‘P1’
ANDAND QTY> (SELECT AVGSELECT AVG(QTY)
FROMFROM SPJ YWHEREWHERE PNO=‘P1’
ANDAND X.JNO=Y.JNO)
Get SNO for suppliers supplying some project wit h P1 in a quantity greater than the average qty of P1 supplied to that project
Correlated Sub Queries ...
Take a sample snapshot of the shipment tables as follows:
SNO PNO JNO Qty
S1 P1 J1 75
S2 P1 J1 40
S3 P2 J2 30
S4 P1 J1 55
Here, s1, s2 , s4 are supplying project j1 with part p1. The average quantity of p1 supplied to j1 is 56. 7. out of this, s1 supplies individually a quantity 75 of part p1 to the project j1.
So according to the query, s1 is the supplier supplying some project (here it is j1) with p1 in a quantity(75) which is greater than the average quantity of p1 supplied to that project(56.7). So the result should be s1.
19
ER/CORP/CRS/DB07/003
Version No: 2.019Copyright © 2004,
Infosys Technologies Ltd
Exercise
• List names of sales reps who have a higher target than their managers
Select s.name from salesreps s wheres.quota > (select m.quota from selesreps m
where m.emplnum=s.manager);
20
ER/CORP/CRS/DB07/003
Version No: 2.020Copyright © 2004,
Infosys Technologies Ltd
Exercise
• Who are the customers whose rep is 102 and have at least one order more than $1000?
Select cust_name from customers
Where cust_rep=102 and cust_num in
(select cust from orders where amount > 1000)
To solve this problem, we split the query as follows:
We have to find the list of customers having an order amount above 1000
Then we have to select customers whose rep is 102 and whose custno is in the list generated by the inner query.
Here there are two clear cut components which are independent so, this will be solved as an independent subquery.
21
Relational algebra operations
22
SET operations
23
ER/CORP/CRS/DB07/003
Version No: 2.023Copyright © 2004,
Infosys Technologies Ltd
Get a list of all the parts cities and supplier ci ties
SELECTSELECT CITY
FROMFROM P
UNIONUNION
SELECTSELECT CITY
FROMFROM S
Retrieval using UNION
Supplier Parts
The results of two independent SELECT statements can be worked with using the SET operation –UNION. By default, UNION returns only distinct values. Union is like an “OR” operation. If the tupleoccurs in relation 1 or relation 2, it is selected. Set theoretic notation indicates union as indicated in the slide
24
ER/CORP/CRS/DB07/003
Version No: 2.024Copyright © 2004,
Infosys Technologies Ltd
Get a list of all common parts and supplier cities
SELECTSELECT CITY
FROMFROM P
INTERSECTINTERSECT
SELECTSELECT CITY
FROMFROM S
Retrieval using INTERSECT
An intersection is an AND operation. It retrieves those tuples which are present in both relation A and B
25
ER/CORP/CRS/DB07/003
Version No: 2.025Copyright © 2004,
Infosys Technologies Ltd
Minus
• Get the list of cities specific to suppliers which are not the cities of any parts
SELECT city FROM supplier MINUS
SELECT city FROM parts
This is the difference operation. It retrieves tuples which are present in relation 1 but not in relation 2.
26
ER/CORP/CRS/DB07/003
Version No: 2.026Copyright © 2004,
Infosys Technologies Ltd
Other RA operations
• Restriction• Projection• Join
27
ER/CORP/CRS/DB07/003
Version No: 2.027Copyright © 2004,
Infosys Technologies Ltd
Restriction
• Restricts the rows that can be chosen from a relation using a where clause• Takes a horizontal subset of values from the original relation• E.g select * from employee where salary>10000;
This will retrieve only those rows of the table which satisfy the condition in the where clause
28
ER/CORP/CRS/DB07/003
Version No: 2.028Copyright © 2004,
Infosys Technologies Ltd
Projection
• Projection is projecting a set of attributes of a relation so that rows of values corresponding to those colums will figure in the output
• For e.g • select empid, name, salary from employee;
• This takes a vertical subset of the relation
29
Join
30
ER/CORP/CRS/DB07/003
Version No: 2.030Copyright © 2004,
Infosys Technologies Ltd
JOIN
• Inner join• Equi join• Outer join
– Left-outer join– Right-outer join
• Self join
In relational databses, data is spread over multiple tables. Sometimes we may want dta from two or more tables. A join is an operation which combines results from two or more tables.
31
ER/CORP/CRS/DB07/003
Version No: 2.031Copyright © 2004,
Infosys Technologies Ltd
Inner Joins
• Common type of join• Combines records from two tables with matching values on a column.
In this type of join, rows from two different tables are selected based on a matching column. Which rows are selected is restricted by a where clause. For example,
Customers Orders
Custid Orderid
custname custid
cust_city amount
Here, custid of customer table and custid of order table correspond.
Note :
pl note that there need not be any primary key fore ign key relationship between these two columns which would be used in a join operation
32
ER/CORP/CRS/DB07/003
Version No: 2.032Copyright © 2004,
Infosys Technologies Ltd
Get all combinations of supplier and part informati on such that the supplier and part are co-located.
SELECTSELECT S.*, P.*
FROMFROM S, P
WHEREWHERE S.CITY=P.CITY
Retrieval from Multiple tables-Equi join
Here the where clause is based on the equality condition “=“. Hence it is called equi join
33
ER/CORP/CRS/DB07/003
Version No: 2.033Copyright © 2004,
Infosys Technologies Ltd
Get SNO,PNO combinations where the part’s city f ollows the supplier’s alphabetically
SELECTSELECT SNO, PNO
FROMFROM S, P
WHEREWHERE S.CITY<P.CITY
Retrieval from Multiple tables- Non Equi join
Here the where clause is based on a non quality condition (<). ?Hence, it is called non-equi join
34
ER/CORP/CRS/DB07/003
Version No: 2.034Copyright © 2004,
Infosys Technologies Ltd
Get SNO and PNO for co-located suppliers and par ts omitting suppliers with status<20
SELECTSELECT P.PNO, S.SNO
FROMFROM P, S
WHEREWHERE S.CITY = P.CITY
AND S.STATUS > = 20
Retrieval from Multiple tablesRetrieval from Multiple tables
There may be multiple conditions in the where clause apart from the join condition
35
ER/CORP/CRS/DB07/003
Version No: 2.035Copyright © 2004,
Infosys Technologies Ltd
Outer join
• Retrieve all rows that match the WHERE clause and those that have a NULL
The inner join takes into account only those non NULL rows from the tables involved. If you want the result to include even those rows having a NULL for a particular row in the selected column, then go for an outer join. The syntax for representing this is slightly different in each rdbms product. What follows in the next slide is the oracle style.
36
ER/CORP/CRS/DB07/003
Version No: 2.036Copyright © 2004,
Infosys Technologies Ltd
Left/Right-Outer join
• Left outer joins include all records from the first (left) of two tables, A=B +
Right outer joins include all records from the second (right) of two tables,
A += B
37
ER/CORP/CRS/DB07/003
Version No: 2.037Copyright © 2004,
Infosys Technologies Ltd
Example of left-join
SELECT S.SNO, SP.QTYFROM S, SPWHERE S.SNO = SP.SNO (+);
All unmatched rows of S are also selected
List all SNO with QTY supplied or SNO which have no t yet supplied any QTY
38
ER/CORP/CRS/DB07/003
Version No: 2.038Copyright © 2004,
Infosys Technologies Ltd
Example of right outer join
• Select the orders and coresponding customernames. Select all customer names, even if there are no orders placed by any.
select o.order_num, c.cust_name from orders o, customers c where o.cust (+) = c.cust_num;
The (+) symbol is next to the column which needs to be expanded to include null values also. In the example above, there may be some customers who have not made any orders, so if we select their names from the customers table(the second table based on int position in the query), the corresponding order detail would be null. Eevne then such values have to be selected . That’s what is indicated. A typical output would look like:
ORDER_NUM CUST_NAME
--------- ----------
5 radha
first corp
jcp inc.
39
ER/CORP/CRS/DB07/003
Version No: 2.039Copyright © 2004,
Infosys Technologies Ltd
Get all pairs of SNO who are co-located
SELECTSELECT FIRST.SNO, SECOND.SNO
FROMFROM S FIRST,
S SECOND
WHEREWHERE FIRST.CITY=SECOND.CITY
Self join-Joining a table with itself
When you wish to join a table with itself based on some criteria, use the concept of synonyms. Treat the table as two different tables by giving synonyms
40
ER/CORP/CRS/DB07/003
Version No: 2.040Copyright © 2004,
Infosys Technologies Ltd
Cartesian Product (a) Cross Join
• A join without any conditions is called a Cartesian product
• It will be a collection of all possible combinations of rows from the tables involved
• Practically of little use
• Just included for conceptual completeness
• E.g select s.sno, sp.sno from supplier s , shipment sp;
Let us assume a snapshot of the supplier table and shipment table are as follows:Supplier Shipment
SNO Name status SNO PNO qtys1 Ram 20 s3 p1 56s2 John 45 s4 p2 100s3 Rafi 32 s3 p3 45s4 Inder 34 s1 p2 34The cartesian product would be like:SNO SNOs1 s3s2 s3s3 s3s4 s3s1 s4s2 s4s3 s4s4 s4s1 s3s2 s3s3 s3s4 s3s1 s1s2 s1s3 s1s4 s1which is hardly of any use
41
EXISTS Vs NOT EXISTS
42
ER/CORP/CRS/DB07/003
Version No: 2.042Copyright © 2004,
Infosys Technologies Ltd
Get PNO’s supplied in the same QTY by at least one different supplier
SELECTSELECT PNO
FROMFROM SP A
WHERE EXISTSWHERE EXISTS(SELECT SELECT *
FROMFROM SP BWHEREWHERE A.PNO=B.PNO
ANDAND A.QTY=B.QTY ANDANDA.SNO<>B.SNO)
Retrieval using EXISTS
Exists check for the existence of a situation/condition.
43
ER/CORP/CRS/DB07/003
Version No: 2.043Copyright © 2004,
Infosys Technologies Ltd
Get all part names from parts table which have been shipped
SELECTSELECT PNAME
FROMFROM P
WHERE EXISTSWHERE EXISTS
(SELECTSELECT *
FROMFROM SP
WHEREWHERE SP.PNO=P.PNO)
Retrieval using EXISTS
Let us take another example:Find the deptno of those departments having employees who can do some work done by employee in depertment d1.
The solution would look like:Select deptno
from department where exists(
select * from employee x
where x.dept= ‘d1’ and exists(select *
from employee y
y.job=x.job and y.dept=department.deptno)
)
and deptno <> ‘d1’;
44
ER/CORP/CRS/DB07/003
Version No: 2.044Copyright © 2004,
Infosys Technologies Ltd
Get the list of all prospective suppliers, i. e.,su ppliers for whom no shipments exist yet.
SELECTSELECT SNAME
FROMFROM S
WHERE NOT EXISTSWHERE NOT EXISTS
(SELECTSELECT *
FROMFROM SP
WHEREWHERE SP.SNO=S.SNO)
Retrieval using NOT EXISTS
A not exists checks for the opposite condition than an Exists. It checks for the non-existence of a condition.
Not exists can be used to arrive at a result which can’t otherwise be arrived at using exists.
The logic is as follows:For all x there exists an y such that f(x,y) is true is the same asThere does not exist and x for which there does not exists a y so that f(x,y)is true
Taking a slight variant of the previous example:Find the deptno of those departments having employees who can do all work done by employees in depertment d1.
Select deptnofrom department
where not exists(select *
from employee x
where x.dept= ‘d1’ and not exists(
select * from employee y
y.job=x.job and y.dept=department.deptno)
)and deptno <> ‘d1’
45
ER/CORP/CRS/DB07/003
Version No: 2.045Copyright © 2004,
Infosys Technologies Ltd
Exercise
• List cities that have received shipments from every customer.
SELECT A.CITY_NAME
FROM CITY AS A
WHERE NOT EXISTS(
SELECT *
FROM CUSTOMER B
WHERE NOT EXISTS(
SELECT *
FROM SHIPMENT C
WHERE A.CITY_NAME=C.DESTINATION
AND B.CUST_ID=C.CUST_ID));
46
ER/CORP/CRS/DB07/003
Version No: 2.046Copyright © 2004,
Infosys Technologies Ltd
Summary• The result of a query can be grouped based on a grouping column
• While checking for conditions after grouping by a column , Having is used instead of where
• Grouped queries help look at data category wise
• When the query consists of more than one component, it is implemented in the form of a nested query depending on the nature of the query.
• Sub queries help split a problem involving different levels of data
• Relational algebra operations like union, intersect, difference, restriction, projection and join help us get different combinations of data from more than one table
47
ER/CORP/CRS/DB07/003
Version No: 2.047Copyright © 2004,
Infosys Technologies Ltd
Thank You!