COMP3311 Week 3 Thursday Lecture

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [0/18]
❖ Week 03 Thursday

In today's lecture ...

Things to do ...

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [1/18]
❖ Video Material

Slides and Videos

Lecture Material
COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [2/18]
❖ Example Database: Beers/Bars/Drinkers

Consider the following ER model:

[Diagram:Pics/exercises/beer1.png]

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [3/18]
❖ Exercise: Delete/Update Examples


Carry out the following actions on the beer database:

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [4/18]
❖ Problem Solving in SQL

Developing SQL queries ...

Learn some query patterns and know when to apply them
COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [5/18]
❖ Views

Views are like "virtual tables"

create [or replace] view ViewName ( AttributeNames )
as
select ResultValues (one per attribute) from ... ;

Things to note:

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [6/18]
❖ Views (cont)

Views are very useful in simplifying complex queries

Views look like tables

If using  create or replace view  you cannot Need to  drop view , then  create view
COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [7/18]
❖ 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  ...;

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [8/18]
❖ 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

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [9/18]
❖ 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 sharing the same value of 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

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [10/18]
❖ 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
}

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [11/18]
❖ 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
      }
   }
}

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [12/18]
❖ Exercise: Queries on Simple Beer DB

Answer these queries on the Beer database:

  1. What beers are made by Toohey's?
  2. Show beers with headings "Beer", "Brewer".
  3. How many different beers are there?
  4. How many different brewers are there?
  5. Find pairs of beers by the same manufacturer.
  6. (a) Which beers does John like?
    (b) Find the brewers whose beers John likes.
  7. (a) How many beers does each brewer make?
    (b) Which brewer makes the most beers?
    (c) Which beers are the only one made by their brewer?
COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [13/18]
❖ Finding the biggest/smallest/etc

Example: what is the most expensive beer?

As SQL:

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);

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [14/18]
❖ 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:

E.g.   X = name,   Y = beer,   Z = price
COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [15/18]
❖ 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 unless doing e.g. pagination.

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [16/18]
❖ Exercise: More queries on Simple Beer DB


  1. List of beers by each brewer
  2. Find the beers sold at bars where John drinks.
  3. Bars where either Gernot or John drink.
  4. Bars where both Gernot and John drink.
  5. Bars where John drinks but Gernot doesn't.
  6. Find bars that serve New at the same price
    as the Coogee Bay Hotel charges for VB.
  7. Find the average price of common beers
    (i.e. served in more than two hotels).
  8. Which bar sells 'New' cheapest?
COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [17/18]
❖ 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);

COMP3311 22T3 ♢ Week 3 Thursday Lecture ♢ [18/18]


Produced: 6 Mar 2023