❖ Week 03 Thursday |
❖ Video Material |
Slides and Videos
❖ Exercise: Delete/Update Examples |
Carry out the following actions on the beer database:
❖ Problem Solving in SQL |
Developing SQL queries ...
FROM
JOIN
WHERE
GROUP BY
SELECT
❖ Views |
Views are like "virtual tables"
create [or replace] view ViewName ( AttributeNames ) as select ResultValues (one per attribute) from ... ;
Things to note:
AttributeNames
select
❖ Views (cont) |
Views are very useful in simplifying complex queries
Views look like tables
create or replace view
drop view
create view
❖ Views (cont) |
Two ways of defining a view
create or replace view V
as
select x as a1, y as a2, z as a3
from R
where ... ;
-- alternatively
create or replace view V(a1, a2, a3)
as
select x, y, z
from R
where ...;
❖ SQL Queries |
Functionality provided by SQL ...
Filtering: extract attributes from tuples, extract tuples from tables
SELECT b,c FROM R(a,b,c,d) WHERE a > 5
Combining: merging related tuples from different tables
... FROM R(x,y,z) JOIN S(a,b,c) ON R.y = S.a
Summarising: aggregating values in a single column
SELECT avg(mark) FROM ...
Set operations: union, intersection, difference
❖ SQL Queries (cont) |
More functionality provided by SQL ...
Grouping: forming subsets of tuples sharing some property
... GROUP BY R.a
(forms groups of tuples from R
a
Group Filtering: selecting only groups satisfying a condition
... GROUP BY R.a HAVING max(R.a) < 75
Renaming: assign a name to a component of a query
SELECT a as name FROM Employee(a,b,c) e WHERE e.b > 50000
❖ Semantics of SELECT..WHERE |
The query
SELECT * FROM R WHERE Condition
can be treated as
Results = {} foreach tuple T in R { if T satisfies Condition add T to Results }
❖ Semantics of JOIN |
The query
SELECT * FROM R JOIN S ON R.x = S.y WHERE Condition
can be treated as
Results = {}
foreach tuple TR in R {
foreach tuple TS in S {
if TR.x == TS.y {
Tjoined = TR+TS # combined
if Tjoined satisfies Condition
add Tjoined to Results
}
}
}
❖ Exercise: Queries on Simple Beer DB |
Answer these queries on the Beer database:
❖ Finding the biggest/smallest/etc |
Example: what is the most expensive beer?
create view maxPrice as select max(price) from Sells; create view highestPriceBeer as select s.beer from Sells s where s.price = (select * from maxPrice);
❖ Finding the biggest/smallest/etc (cont) |
Don't need the view; can calculate "inline"
create view highestPriceBeer as select s.beer from Sells s where s.price = (select max(price) from Sells);
But you always need a query to extract the single value
An example of a common pattern:
❖ Finding the biggest/smallest/etc (cont) |
Note: there may be more than one equally biggest/smallest
The following is not a general solution
create view highestPriceBeer as select s.beer from Sells s order by s.price desc limit 1
Avoid limit
❖ Exercise: More queries on Simple Beer DB |
❖ More Problem Solving in SQL |
Example: what is cheapest beer at each bar?
for each Bar { find cheapest price at the bar fine name of beer with this price }
Needs correlated subquery
select s.bar, s.beer, s.price:numeric(4,2) from Sells s where s.price = (select min(s1.price) from Sells s1 where s1.bar = s.bar);
Produced: 6 Mar 2023