Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Review.

Similar presentations


Presentation on theme: "SQL Review."— Presentation transcript:

1 SQL Review

2 Example Schema We will use these table definitions in our examples.
Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) Make reservations

3 Basic SQL Query SELECT [DISTINCT] target-list FROM relation-list
WHERE qualification relation-list A list of relation names target-list A list of attributes of relations in relation-list qualification Comparisons (“Attr op const” or “Attr1 op Attr2,” where op is one of ˂, ˃, ≤, ≥, =, ≠ ) combined using AND, OR, and NOT. DISTINCT is an optional keyword indicating that the answer should not contain duplicates.

4 Retrieve names of students and the courses they received an “A” grade
Querying Relations (1) What does the following query compute? Enrolled SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=“A” A student with “sid” has an entry in Enrolled The Enrolled entry has a grade of “A” Students sid name login age gpa 53831 Zhang 19 3.2 53650 Smith 21 3.9 53666 Jones 20 3.5 Retrieve names of students and the courses they received an “A” grade

5 Querying Relations (2) Enrolled Students SELECT S.name, E.cid
sid name login age gpa 53831 Zhang 19 3.2 53650 Smith 21 3.9 53666 Jones 20 3.5 SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=“A” S.name E.cid Smith Topology112

6 Query Result Semantics of SQL ˂, ˃, ≤, ≥, =, ≠ R1 × R2 × R3 × · · ·
QUERY PROCESSOR SELECT [DISTINCT] target-list FROM relation-list WHERE qualification target-list Define search space relation-list R1 × R2 × R3 × · · · qualification This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. Select rows ˂, ˃, ≤, ≥, =, ≠ Query Result Select columns Projection

7 Example of Conceptual Evaluation
SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 Range variable Reservation Sailors

8 Example of Conceptual Evaluation
SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 S×R (sid) sname rating age bid day 22 dustin 7 45.0 101 10/10/96 58 103 11/12/96 31 lubber 8 55.5 rusty 10 35.0 Remove Irrelevant columns Disqualified Answer

9 A Note on Range Variables
Really needed only if the same relation appears twice in the FROM clause. The previous query can be written in two ways: Range variable SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 It is good style, however, to use range variables always! SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 OR

10 Aggregate Operators Significant extension of relational algebra
COUNT (*) The number of rows in the relation COUNT ([DISTINCT] A) The number of (unique) values in the A column SUM ([DISTINCT] A) The sum of all (unique) values in the A column AVG ([DISTINCT] A) The average of all (unique) values in the A column MAX (A) The maximum value in the A column MIN (A) The minimum value in the A column The number of rows in the relation

11 Aggregate Operators SELECT COUNT (*) FROM Sailors S SELECT S.sname
Count the number of sailors Find the name of sailors with the highest rating SELECT COUNT (*) FROM Sailors S Compute maximum rating SELECT S.sname FROM Sailors S WHERE S.rating= (SELECT MAX(S2.rating) FROM Sailors S2) Find the average age of sailors with a rating of 10 Find the average of the distinct ages of sailors with a rating of 10 SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 Count the number of distinct ratings of sailors called “Bob” SELECT AVG (DISTINCT S.age) FROM Sailors S WHERE S.rating=10 SELECT COUNT (DISTINCT S.rating) FROM Sailors S WHERE S.sname=‘Bob’

12 Find the average age of sailors with a rating of 10
GROUP BY and HAVING (1) So far, we’ve applied aggregate operators to all (qualifying) tuples. Relation Aggregator Qualifier 32 Aggregator Find the average age of sailors with a rating of 10 SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 Qualifier

13 GROUP BY and HAVING (1) So far, we’ve applied aggregate operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples. Relation Aggregator Qualifier 32 Relation Group 1 Aggregator 12 Group 2 Group 3 9 11

14 Queries With GROUP BY and HAVING
SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification MIN(Attribute) HAVING GROUP BY Output a table Aggregator 12 9 Qualifier selecting groups Group 1 Group 2 Group 3 SELECT FROM WHERE

15 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 (*) > 1 Input relation Sailors age Answer Disqualify Only one group satisfies HAVING 4 rating groups Only S.rating and S.age are mentioned in SELECT

16 Summary SQL was an important factor in the early acceptance of the relational model; more natural than earlier, procedural query languages. Relationally complete; in fact, significantly more expressive power than relational algebra. Even queries that can be expressed in RA can often be expressed more naturally in SQL. Many alternative ways to write a query; optimizer should look for most efficient evaluation plan. In practice, users need to be aware of how queries are optimized and evaluated for best results.


Download ppt "SQL Review."

Similar presentations


Ads by Google