RDBMS_Day 4
Short Description
Download RDBMS_Day 4...
Description
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,
1
Grouped results
2
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
SELECT PNO, SUM(QTY) FROM SP GROUP BY PNO
Copyright © 2004, Infosys Technologies Ltd
3
ER/CORP/CRS/DB07/003 Version No: 2.0
In the output table all the rows with an identical value in the grouping column will be grouped together.
3
Retrieval using GROUP BY
Get SNO, PNO, total qty for each part supplied SELECT SNO, PNO, SUM(QTY) FROM SP GROUP BY PNO SELECT SNO, PNO, SUM(QTY) FROM SP GROUP BY SNO, PNO Copyright © 2004, Infosys Technologies Ltd
4
ER/CORP/CRS/DB07/003 Version No: 2.0
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.
4
Retrieval using HAVING •
Used to specify condition on group
Get PNO for parts which have more than two shipments
SELECT PNO,COUNT(*) FROM GROUP BY PNO HAVING COUNT(*)>2
SP
Get supplier numbers who have at least two shipments.
SELECT SNO , COUNT(*) FROM GROUP BY SNO HAVING COUNT(*)>=2
Copyright © 2004, Infosys Technologies Ltd
SP
5
ER/CORP/CRS/DB07/003 Version No: 2.0
5
Can you identify any error…? SELECT SNO , COUNT(*) FROM SP GROUP BY SNO HAVING PNO=‘p2’; Ans: The Having condition has to be based on some column that appears in the select list
Copyright © 2004, Infosys Technologies Ltd
6
ER/CORP/CRS/DB07/003 Version No: 2.0
6
Independent subqueries
7
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.
Copyright © 2004, Infosys Technologies Ltd
8
ER/CORP/CRS/DB07/003 Version No: 2.0
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;
8
Retrieval using SUB QUERIES Get supplier names for all suppliers who supply part P2
SELECT SNAME FROM S WHERE SNO IN (SELECT SNO FROM SP WHERE PNO =‘P2’)
Copyright © 2004, Infosys Technologies Ltd
9
ER/CORP/CRS/DB07/003 Version No: 2.0
9
Retrieval using SUB QUERIES Get SNO for suppliers who are located in the same city as S1
SELECT SNO FROM S WHERE CITY = (SELECT CITY FROM S WHERE SNO=‘S1’)
Copyright © 2004, 10 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
10
Retrieval using SUB QUERIES Get SNO for suppliers who supply at least one part supplied by S2
SELECT SNO FROM SP WHERE PNO IN (SELECT PNO FROM SP WHERE SNO=‘S2’)
Copyright © 2004, 11 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
11
Retrieval using SUB QUERIES Get SNO who have status less than the status of ‘S1’
SELECT SNO FROM S WHERE STATUS < (SELECT STATUS FROM S WHERE SNO=‘S1’)
Copyright © 2004, 12 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
12
Retrieval using SUB QUERIES Get supplier numbers for suppliers with status less than the current maximum in the supplier table
SELECT SNO FROM S WHERE STATUS < (SELECT MAX(STATUS) MAX FROM S)
Copyright © 2004, 13 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
13
Retrieval using SUB QUERIES Get SNO for suppliers who do not supply any part supplied by S2
SELECT SNO FROM SP WHERE PNO NOT IN (SELECT PNO FROM SP WHERE SNO=‘S2’))
Copyright © 2004, 14 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
14
Write a query for the following ... For each part supplied, get the part number, maximum quantity, and minimum quantity supplied for that part
SELECT SP.P# , MAX (SP.QTY) AS MXQ, MIN (SP.QTY) AS MNQ FROM SP GROUP BY SP.P# ;
Copyright © 2004, 15 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
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);
15
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.
Copyright © 2004, 16 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
16
Correlated Sub Queries Get PNO for all parts supplied by more than one supplier
SELECT PNO FROM SP X WHERE PNO IN (SELECT PNO FROM SP Y WHERE Y.SNOX.SNO)
Copyright © 2004, 17 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
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 qualifie to 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 query To 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 Qty S1 P1 J1 75 S2 P3 J1 40 S1 P1 J2 30 S4 P3 J1 55 Here, 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 Y SNO PNO JNO Qty SNO PNO JNO Qty S1 P1 J1 75 S1 P1 J1 75 S2 P3 J1 40 S2 P3 J1 40 S1 P1 J2 30 S1 P1 J2 30 S4 P3 J1 55 S4 P3 J1 55 The 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 selected Iteration 2 : Is p3 in (p1, p1, p3) -> qualified -> selected Iteration 3: Is p1 in (p3,p3) -> not qualified -> not selected Iteration 4: Is p3 in (p1,p1,p3)-> qualified -> selected So the result will contain two rows displaying Pno P3 P3 To avoid this we may use in the outer query select distinct rather than select
17
Correlated Sub Queries ... Get SNO for suppliers supplying some project with P1 in a quantity greater than the average qty of P1 supplied to that project
SELECT DISTINCT SNO FROM SPJ X WHERE PNO=‘P1’ AND QTY> (SELECT AVG(QTY) AVG FROM SPJ Y WHERE PNO=‘P1’ AND X.JNO=Y.JNO)
Copyright © 2004, 18 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
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.
18
Exercise •
List names of sales reps who have a higher target than their managers
Select s.name from salesreps s where s.quota > (select m.quota from selesreps m where m.emplnum=s.manager);
Copyright © 2004, 19 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
19
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)
Copyright © 2004, 20 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
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.
20
Relational algebra operations
21
SET operations
22
Retrieval using UNION Get a list of all the parts cities and supplier cities
SELECT CITY FROM P UNION SELECT CITY FROM S
Copyright © 2004, 23 Infosys Technologies Ltd
Supplier
Parts
ER/CORP/CRS/DB07/003 Version No: 2.0
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 tuple occurs in relation 1 or relation 2, it is selected. Set theoretic notation indicates union as indicated in the slide
23
Retrieval using INTERSECT Get a list of all common parts and supplier cities
SELECT CITY FROM P INTERSECT SELECT CITY FROM S
Copyright © 2004, 24 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
An intersection is an AND operation. It retrieves those tuples which are present in both relation A and B
24
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 Copyright © 2004, 25 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
This is the difference operation. It retrieves tuples which are present in relation 1 but not in relation 2.
25
Other RA operations • • •
Restriction Projection Join
Copyright © 2004, 26 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
26
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;
Copyright © 2004, 27 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
This will retrieve only those rows of the table which satisfy the condition in the where clause
27
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
Copyright © 2004, 28 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
28
Join
29
JOIN • Inner join • Equi join • Outer join – Left-outer join – Right-outer join • Self join
Copyright © 2004, 30 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
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.
30
Inner Joins • •
Common type of join Combines records from two tables with matching values on a column.
Copyright © 2004, 31 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
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 foreign key relationship between these two columns which would be used in a join operation
31
Retrieval from Multiple tables-Equi join Get all combinations of supplier and part information such that the supplier and part are co-located.
SELECT S.*, P.* FROM S, P WHERE S.CITY=P.CITY
Copyright © 2004, 32 Infosys Technologies Ltd
ER/CORP/CRS/DB07/003 Version No: 2.0
Here the where clause is based on the equality condition “=“. Hence it is called equi join
32
Retrieval from Multiple tables- Non Equi join Get SNO,PNO combinations where the part’s city follows the supplier’s alphabetically
SELECT SNO, PNO FROM S, P WHERE S.CITY
View more...
Comments