Presentation is loading. Please wait.

Presentation is loading. Please wait.

Textbook Chapter 9 (incl. example queries at end)

Similar presentations


Presentation on theme: "Textbook Chapter 9 (incl. example queries at end)"— Presentation transcript:

1 Textbook Chapter 9 (incl. example queries at end)
SQL Textbook Chapter 9 (incl. example queries at end) 1/16/2019

2 Structured Query Language
Started as "Sequel" Now an ANSI (X3H2) / ISO standard SQL2 or SQL-92 SQL3 is in the works Lots of uses and versions As a direct language our focus As an embedded language in a program in a conventional language As a protocol from client to DB server increasingly important 1/16/2019

3 SQL DDL TABLE/SCHEMA/CATALOG CREATE/DROP/ALTER SQL DATA TYPES
DB files are often character-based, descended from COBOL INT, FLOAT, etc. DEC(i,j) CHAR(n), VARCHAR(n) DATE/TIME 1/16/2019

4 SQL DML 90% of what you need to know:
SELECT <attributes> FROM <tables> WHERE <conditions> SELECT is a projection into the output FROM is effectively a Cartesian product of all the tables used WHERE are conditions, which could be very elaborate and even involve nested SELECTs. 1/16/2019

5 Some Notation Attributes can be qualified Tables can be renamed
SELECT S.FNAME, S.LNAME FROM EMPLOYEE E, EMPLOYEE S WHERE E.SUPERSSN = S.ESSN SELECT * means display all columns No WHERE means display all rows 1/16/2019

6 More SQL Topics Conditionals Tables vs sets
NOT, AND, OR (in precedence order) Tables vs sets Duplicates are NOT eliminated on certain operations! DISTINCT keyword eliminates dups in SELECT 1/16/2019

7 Set Operations UNION, intersections ("INTERSECT"), difference ("EXCEPT") DO eliminate duplicates Use to connect whole sets (result of queries), not within WHERE EXCEPT not supported by MS Access 97 Division is not an SQL operation Older SQL had a CONTAINS (set inclusion) 1/16/2019

8 IN and EXISTS IN EXISTS Tests for membership in a set: x  A
Binary operator returning Boolean value used within conditions (WHERE) left side a row (or value construed as a row) right side a table, frequently the result of a (nested) SELECT EXISTS Unary operator returning a Boolean tests a table for non-empty: A 1/16/2019

9 Aggregate Functions Five standard functions: COUNT, SUM, MIN, MAX, AVG
Normally appears in SELECT clause SELECT SUM (SALARY) Is applied after WHERE Result is a column in the output can sometimes think of as a scalar 1/16/2019

10 Grouping GROUP BY <attributes>
causes rows with common values to be grouped together no particular ordering among groups Might be used just to improve output presentation, but… Most commonly used in connection with aggregate functions 1/16/2019

11 Grouping and Functions
In presence of grouping, aggregate functions apply to each group individually. Output has one row per group Order of processing: WHERE conditions, then GROUPING, then SELECT (including agg. functions). But… what if you want a condition applied after the grouping?? 1/16/2019

12 HAVING Applies a condition after grouping May use aggregate functions
condition is applied to each group May use aggregate functions HAVING SUM(QUANTITY) > 350 1/16/2019

13 p.185 "Find the names of sailors who have reserved boat #103"
SELECT S.sname FROM Sailors S, Reserve R WHERE S.sid = R.sid AND R.bid = 103 1/16/2019

14 p.189 "Find the names of sailors who have reserved a red or a green boat" SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid AND R.bid = B.bid AND (B.color = 'red' OR B.color = 'green') Careful: can you change OR to AND? 1/16/2019

15 p.189b "Find the names of sailors who have reserved both a red and a green boat." SELECT S.sname FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid = R1.sid AND R1.bid = B1.bid AND S.sid = R2.sid AND R2.bid = B2.bid AND (B1.color = 'red' AND B2.color = 'green') 1/16/2019

16 p.192 "Find the names of sailors who have reserved boat #103"
SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103) 1/16/2019

17 p.193a "Find the names of sailors who have reserved a red boat"
SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid IN (SELECT B.bid FROM Boats B WHERE B.color = 'red') 1/16/2019

18 p.193b "Find the names of sailors who have reserved boat #103
SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 AND S.sid = R.sid) 1/16/2019

19 p.197 "Find the average age of sailors with a rating of 10"
SELECT AVG (S.age) FROM Sailors S WHERE S.rating = 10 1/16/2019

20 p.197-8 "Find the name and age of the oldest sailor"
SELECT S.name, S.age FROM Sailors S WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2) 1/16/2019

21 p.200 "Find age of the youngest sailor who is eligible to vote, for each rating level with at least two such sailors" SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 1/16/2019

22 p.202 "For each red boat, find the number of reservations for this boat" SELECT B.bid, COUNT (*) AS sailorcount FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = 'red' GROUP BY B.bid 1/16/2019


Download ppt "Textbook Chapter 9 (incl. example queries at end)"

Similar presentations


Ads by Google