Lecture Grpby & Comples Joins.
Lecture Grpby & Comples Joins.
complex joins
1
The SHR table
shr
shrcode shrfirm shrprice shrqty shrdiv shrpe
2
Calculating
SELECT shrfirm, shrprice, shrqty,
shrdiv/shrprice*100 as YIELD
FROM shr;
shrfirm shrprice shrqty yield
Freedonia Copper 27.50 10,529 6.69
Patagonian Tea 55.25 12,635 4.52
Abyssinian Ruby 31.82 22,010 4.15
Sri Lankan Gold 50.37 32,868 5.32
Indian Lead & Zinc 37.75 6,390 7.95
Burmese Elephant 0.07 154,713 14.29
Bolivian Sheep 12.75 231,678 13.96
Nigerian Geese 35.00 12,323 4.80
Canadian Sugar 52.78 4,716 4.74
3
Royal Ostrich Farms 33.75 1,234,923 8.89
“Aggregate” functions
• COUNT, AVG, SUM, MIN, and MAX
Find the average dividend.
SELECT AVG(shrdiv) AS avgdiv FROM shr;
avgdiv
2.03
5
6
Select count(Country) from Menu;
This …
count( <some column name>)
… only counts the non null values in that column.
7
Min and Max functions:
Give the cheapest Italian Pizza;
8
9
Group by and having.
10
nation
NATION and STOCK
natcode natname exchrate
UK United Kingdom 1.00
USA United States 0.67
AUS Australia 0.46
IND India 0.0228
stock
natname stkvalue
Australia 946430.65
India 97506.71
United Kingdom 48908364.25
United States 30066065.54 12
HAVING – like WHERE, but
after the grouping
Report the total value of stocks for nations with two or more listed stocks. (The
“two or more listed stocks” refers to a property of the groupings, so that part is
performed after the grouping.)
13
Discussion about optional lab
exercises.
14
Subqueries
• A query within a query
Report all firms with a PE ratio greater than the average for the
portfolio.
16
Using subqueries to find the maximum
(or minimum)
Give pizza and prices for pizzas that are more expensive than
all Italian pizzas.
17
Alternate way to find the maximum (or
minimum): “ALL”
Give pizza and prices for pizzas that are
more expensive than all Italian pizzas.
WHERE price =
(SELECT min(price) from menu); 19
ANY operator
List pizzas with at least one ‘meat’ ingredient.
22
Type II Nested Queries
(Correlated Subqueries)
• Not in this subject
• At least not for P and C;
• Maybe D and HD
• Reference to outer query
• Executes one time for each row of outer query
23
Revision: the Natural Join
• Join creates a new table from two existing tables by
matching on a column common to both tables
• Eg.SELECT *
FROM stock NATURAL JOIN nation;
• Cross product form …
• The new table contains two identical columns
SELECT * FROM stock, nation
WHERE stock.natcode = nation.natcode;
24
Left Outer join
• Not in this subject
• At least not for P and C; maybe D and HD
• A natural join plus those rows from t1 not included in
the natural join
• SELECT *
FROM t1 LEFT JOIN t2 USING (id);
t1 t2
27
Self-Join
• Join a table to itself
• Usually involve a self-referencing relationship supervises
• Useful to find relationships
among rows of the same emp
table
28
Joining a table with itself
emp
empno empfname empsalary deptname bossno
1 Alice 75000 Management
2 Ned 45000 Marketing 1
3 Andrew 25000 Marketing 2
4 Clare 22000 Marketing 2
5 Todd 38000 Accounting 1
6 Nancy 22000 Accounting 5
7 Brier 43000 Purchasing 1
8 Sarah 56000 Purchasing 7 supervises
9 Sophie 35000 Personnel & PR 1
emp
emp
empno empfname empsalary deptname bossno
1 Alice 75000 Management
2 Ned 45000 Marketing 1
3 Andrew 25000 Marketing 2
4 Clare 22000 Marketing 2
5 Todd 38000 Accounting 1
6 Nancy 22000 Accounting 5
7 Brier 43000 Purchasing 1
8 Sarah 56000 Purchasing 7 29
9 Sophie 35000 Personnel & PR 1
Querying a recursive
relationship
Find the names of employees who earn more than their boss.
supervises
SELECT wrk.empfname
FROM emp wrk, emp boss
WHERE wrk.bossno = boss.empno emp
AND wrk.empsalary > boss.empsalary;
wrk boss
empno empfname empsalary deptname bossno empno empfname empsalary deptname bossno
2 Ned 45,000 Marketing 1 1 Alice 75,000 Management
3 Andrew 25,000 Marketing 2 2 Ned 45,000 Marketing 1
4 Clare 22,000 Marketing 2 2 Ned 45,000 Marketing 1
5 Todd 38,000 Accounting 1 1 Alice 75,000 Management
6 Nancy 22,000 Accounting 5 5 Todd 38,000 Accounting 1
7 Brier 43,000 Purchasing 1 1 Alice 75,000 Management
8 Sarah 56,000 Purchasing 7 7 Brier 43,000 Purchasing 1
9 Sophie 35,000 Personnel & PR 1 1 Alice 75,000 Management
30
empfname
Sarah
web links on aggregate functions