Docsity

Database Management Systems - SQL Queries | EECS 484, Study Guides, Projects, Research of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 1
SQL Queries
Chapter 5,
Sections 5.1-5.6
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 2
SQL Query Language
•Implements relational algebra…
•Select, Project, Join, Set operators
•And so much more…
•Correlated subqueries
•Ordering of results
•Aggregate queries (e.g., SUM, MAX, AVG)
•Three-valued logic for NULL values
•Etc.
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 3
Basic SQL Query
SELECT [DISTINCT] attr-list
FROM relation-list
WHERE qualification
Optional
List of relations
Attr1 op Attr2
OPS: <, >, =, <=, >= , <>
Combine using AND, O R, NOT
Attributes from
input relations
(Conceptual)
Evaluation:
1. Take cross-product of relation-list
2. Select rows satisfying qualification
3. Project columns in attr- list
(eliminate duplicates only if DISTINCT)
Optimizer
chooses
efficient
plan!!
pf3
pf4
pf5
pf8

Partial preview of the text

Download Database Management Systems - SQL Queries | EECS 484 and more Study Guides, Projects, Research Database Management Systems (DBMS) in PDF only on Docsity!

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 1

SQL Queries

Chapter 5,

Sections 5.1-5.

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 2

SQL Query Language

• Implements relational algebra…

  • Select, Project, Join, Set operators

• And so much more…

  • Correlated subqueries
  • Ordering of results
  • Aggregate queries (e.g., SUM, MAX, AVG)
  • Three-valued logic for NULL values
  • Etc. 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 3

Basic SQL Query

SELECT [DISTINCT] attr-list

FROM relation-list

WHERE qualification

Optional List of relations Attr1 op Attr OPS: <, >, =, <=, >=, <> Combine using AND, OR, NOT Attributes from input relations

(Conceptual) Evaluation:

1. Take cross-product of relation-list

2. Select rows satisfying qualification

3. Project columns in attr-list

(eliminate duplicates only if DISTINCT)

Optimizer chooses efficient plan!!

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 4

Example of Basic Query

• Schema:

  • Sailors (sid, sname, rating, age)
  • Boats (bid, bname, color)
  • Reserves (sid, bid, day)

• Find the names of sailors who have

reserved boat

SELECT S.sname

FROM Sailors S, Reserves R

WHERE S.sid = R.sid AND R.bid = 103

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 5

Example of Basic Query

sid bid day Reserves 58 rusty 10 35 8

rating 31 lubber 55 22 dustin 45 sid sname age Sailors 22 101 10/10 31 lubber 8 55 58 103 11/12 22 dustin 7 45 58 103 11/12 58 rusty 10 35 58 22 22 sid 103 101 101 bid 11/ 10/ 10/ day 58 rusty 10 35 8 7 rating 31 lubber 55 22 dustin 45 sid sname age Reserves x Sailors 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 6

Example of Basic Query

SELECT DISTINCT sname

FROM Sailors S, Reserves R

WHERE S.sid = R.sid AND R.bid = 103

What’s the effect of adding DISTINCT? What’s the equivalent relational algebra?

πname (σbid=103(Sailors  Reserves))

RA project removes duplicates!

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 10

ORDER BY clause

SELECT S.sname, S.age, S.rating FROM Sailors S ORDER BY S.age ASC, S.rating DESC Find the names, ages, and rankings of all sailors. Sort the result in increasing order of age. If there is a tie, sort those tuples in decreasing order of rating. What does this query compute? 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 11

Union

• Find names of sailors who have reserved a

red or a green boat

SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND (B.color = ‘red’ OR B.color = ‘green’)) SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’ UNION SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid AND B.color = ‘green’ 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 12

Intersect

• Find sids of sailors who have reserved a

red and a green boat

SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’ INTERSECT SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid AND B.color = ‘green’ What if we replace sid with sname (non-key)?

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 13

More Set Operators

• Also EXCEPT (set difference)

• (Painful) SQL oddities

  • For UNION, default is to eliminate duplicates!
  • To keep duplicates, must specify UNION ALL 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 14

Nested Queries

• Query with another query embedded inside

SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) What does this Compute? Conceptual evaluation: For each row of Sailors, evaluate the subquery over reserves To find sailors who have not reserved 103, use NOT IN 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 15

Nested Queries with Correlation

SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT UNIQUE R.bid FROM Reserves R WHERE S.sid=R.sid) Find names of sailors who have reserved exactly one boat. True if value appears once Illustrates why subquery must be evaluated for each tuple in Sailors.

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 19

GROUP BY

• Conceptual evaluation

  • Partition data into groups according to some

criterion

  • Evaluate the aggregate for each group

Example: For each rating level, find the age

of the youngest sailor SELECT MIN (S.age), S.rating FROM Sailors S GROUP BY S.rating How many tuples in the result? 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 20

GROUP BY and HAVING

SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification Conceptual Evaluation:

  1. Eliminate tuples that don’t satisfy qualification
  2. Partition remaining data into groups
  3. Eliminate groups according to group-qualification
  4. Evaluate aggregate operation(s) for each group Target-list contains:
  • Attribute names (subset of grouping-list)
  • Aggregate operations (e.g., min(age)) Find the age of the youngest sailor with age >= 18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 2

sid sname rating age

22 dustin 7 45. 0

31 lubber 8 55. 5

71 zorba 10 16. 0

64 horatio 7 35. 0

29 brutus 1 33. 0

58 rusty 10 35. 0

rating age 1 33. 0 7 45. 0 7 35. 0 8 55. 5 10 35. 0 rating 7 35. 0 Answer relation rating

1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 22

NULL Values in SQL

  • NULL represents ‘unknown’ or ‘inapplicable’
  • Query evaluation complications - Q: Is (rating > 10) true when rating is NULL? - A: Condition evaluates to ‘unknown’ (not T or F)
  • What about AND, OR connectives? - Need 3-valued logic
  • WHERE clause eliminates rows that don’t evaluate to true UU U U UF F U UT U T FU F U FF F F FT F T TU U T TF F T TT T T pq p AND q p OR q 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 23

NULL Values Example

58 rusty 10 NULL 8

rating 31 lubber 55 22 dustin 45 sid sname age What does this query sailors return? SELECT sname FROM sailors WHERE age > 45 OR age <= 45 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 24

NULL Values in Aggregates

• NULL values generally ignored when

computing aggregates

  • Modulo some special cases (see textbook) SELECT AVG(age) FROM sailors 58 rusty 10 NULL 8

rating 31 lubber 55 22 dustin 45 sid sname age sailors Returns 50!