Understanding Subqueries and Collections in OQL: From Queries to Output, Slides of Database Management Systems (DBMS)

The concept of subqueries and collections in object query language (oql). It covers the use of subqueries in from clauses with quantifiers like exists and forall, and the use of boolean-valued expressions in where clauses with for all and exists. The document also provides examples and explanations of how to extract collection elements and perform aggregation using oql.

Typology: Slides

2011/2012

Uploaded on 01/31/2012

marphy
marphy 🇺🇸

4.4

(31)

284 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Subqueries
Used mainly in
FROM
clauses and with
quantiers
EXISTS
and
FORALL
.
Example: Subquery in
FROM
Find the manufacturers of the beers served at
Joe's.
SELECT DISTINCT b.manf
FROM (
SELECT s.beer
FROM Sells s
WHERE s.bar.name = "Joe's Bar"
)b
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Subqueries and Collections in OQL: From Queries to Output and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Sub queries

 Used mainly in FROM clauses and with quanti ers EXISTS and FORALL.

Example: Sub query in FROM

Find the manufacturers of the b eers served at Jo e's. SELECT DISTINCT b.manf FROM ( SELECT s.beer FROM Sells s WHERE s.bar.name = "Joe's Bar" ) b

Quanti ers

 Bo olean-valued expressions for use in WHERE- clauses. FOR ALL x IN : EXISTS x IN :

 The expression has value TRUE if the condition is true for all (resp. at least one) elements of the collect i on.

Example

Find all bars that sell some b eer for more than $5.

SELECT b.name FROM Bars b WHERE EXISTS s IN b.beersSold : s.price > 5.

Problem

How would you nd the bars that only sold b eers for more than $5?

Extraction of Collecti on Elements

a) A collect i on with a single memb er: Extract the memb er with ELEMENT.

Example

Find the price Jo e charges for Bud and put the result in a variable p.

p = ELEMENT( SELECT s.price FROM Sells s WHERE s.bar.name = "Joe's Bar" AND s.beer.name = "Bud" )

b) Extracting all elements of a collecti on, one at a time:

  1. Turn the collecti on into a list.
  2. Extract elements of a list with [i].

Example

Print Jo e's menu, in order of price, with b eers of the same price listed alphab etical ly.

L = SELECT s.beer.name, s.price FROM Sells s WHERE s.bar.name = "Joe's Bar" ORDER BY s.price, s.beer.name;

printf("Beer\tPri ce\n\ n"); for(i=1; i<=COUNT(L); i++) printf("%s\t%f\n ", L[i].name, L[i].price );

Grouping

Recall SQL grouping, for example:

SELECT bar, AVG(price) FROM Sells GROUP BY bar;

 Is the bar value the \name" of the group, or the common value for the bar comp onent of all tuples in the group?

 In SQL it do esn't matter, but in OQL, you can create groups from the values of any function(s), not just attributes. F Thus, groups are identi ed by common values, not \name." F Example: group by rst letter of bar names (metho d needed).

Outline of OQL Group-By

Collectio n De ned by FROM, WHERE

Collecti on with function values and partition

Group by values of function(s)

Terms from SELECT clause

Output collecti on

  1. Intermediate collect io n:

F One function: s.bar.name maps Sell ob jects s to the value of the name of the bar referred to by s. F Collecti on is a set of structs of typ e: Struct{barName: string, partition: Set< Struct{s: Sell}

} For example: Struct(barName = "Joe's Bar", partition = fs 1 ; : : : ; sn g) where s 1 ; : : : ; sn are all the structs with one eld, named s, whose value is one of the Sell ob jects that represent Jo e's Bar selling some b eer.

  1. Output collecti on: consists of b eer-average price pairs, one for each struct in the intermediate collecti on.

F Typ e of structures in the output: Struct{barName: string, avgPrice: real}

F Note that in the sub query of the SELECT clause: SELECT barName, avgPrice: AVG( SELECT p.s.price FROM partition p ) We let p range over all structs in partition. Each of these structs contains a single eld named s and has a Sell ob ject as its value. Thus, p.s.price extracts the price from one of the Sell ob jects. F Typical output struct: Struct(barName = "Joe's Bar", avgPrice = 2.83)

The Query

SELECT beerName, low, high, count: COUNT(partition) FROM Beers b, b.soldBy s GROUP BY beerName: b.name, low: s.price <= 2.00, high: s.price >= 4.

  1. Initial collecti on: Pairs (b; s), where b is a Beer ob ject, and s is a Sell ob ject representing the sale of that b eer at some bar. F Typ e of collecti on memb ers: Struct{b: Beer, s: Sell}
  1. Intermediate collect io n: Quadruples consisting of a b eer name, b o oleans telli ng whether this group is for high, low, or neither prices for that b eer, and the partition for that group. F The partition is a set of structs of the typ e: Struct{b: Beer, s: Sell} A typical value: Struct(b: "Bud" ob ject, s: a Sell ob ject involving Bud)
  1. Output collecti on: The rst three comp onents of each group's struct are copied to the output, and the last (partition) is counted. The result: b eerName low high count Bud TRUE FALSE 27 Bud FALSE TRUE 14 Bud FALSE FALSE 36