SQL: Queries on One Table

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [0/12]
❖ Queries

A query is a declarative program that retrieves data from a database.

declarative = say what we want, not method to get it

Queries are used in two ways in RDBMSs:


SQL is based on the relational algebra, which we discuss elsewhere

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [1/12]
❖ SQL Query Language

An SQL query consists of a sequence of clauses:

SELECT   projectionList
FROM     relations/joins
WHERE    condition
GROUP BY groupingAttributes
HAVING   groupCondition

FROM,   WHERE,   GROUP BY,   HAVING   clauses are optional.

Result of query: a relation, typically displayed as a table.

Result could be just one tuple with one attribute (i.e. one value) or even empty

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [2/12]
❖ SQL Query Language (cont)

Functionality provided by SQL ...

Filtering: extract attributes from tuples, extract tuples frm 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 20T3 ♢ SQL: Queries on One Table ♢ [3/12]
❖ SQL Query Language (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 20T3 ♢ SQL: Queries on One Table ♢ [4/12]
❖ SQL Query Language (cont)

Schema:

Example SQL query on this schema:

SELECT   s.id, s.name, avg(e.mark) as avgMark
FROM     Students s
         JOIN Enrolments e on (s.id = e.student)
GROUP BY s.id, s.name
-- or --
SELECT   s.id, s.name, avg(e.mark) as avgMark
FROM     Students s, Enrolments e
WHERE    s.id = e.student
GROUP BY s.id, s.name

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [5/12]
❖ SQL Query Language (cont)


How the example query is computed:

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [6/12]
❖ Problem-solving in SQL

Starts with an information request:

Ends with:

Pre-req:   know your schema

Look for keywords in request to identify required data :

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [7/12]
❖ Problem-solving in SQL (cont)


Developing SQL queries ...

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [8/12]
❖ Problem-solving in SQL (cont)


Example: just the beers that John likes

... giving ...

select beer from Likes where drinker='John';

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [9/12]
❖ Views

A view associates a name with a query:

Each time the view is invoked (in a FROM clause): A view can be treated as a "virtual table".

Views are useful for "packaging" a complex query to use in other queries.

cf. writing functions to package computations in programs

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [10/12]
❖ Exercise: Queries on Beer Database

ER design for Beer database:

[Diagram:Pics/sql/beer-er.png]

COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [11/12]
❖ Exercise: Queries on Beer Database (cont)

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. Which beers does John like?
  6. Find pairs of beers by the same manufacturer.
  7. How many beers does each brewer make?
  8. Which brewers make only one beer?
  9. Which brewer makes the most beers?
COMP3311 20T3 ♢ SQL: Queries on One Table ♢ [12/12]


Produced: 28 Sep 2020