A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1…...

35
ADVANCED SQL Joe Meehean 1

Transcript of A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1…...

Page 1: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

1

ADVANCED SQL

Joe Meehean

Page 2: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

2

SQL SET OPERATIONS

SyntaxSELECT column1, column2, …FROM table1…WHERE conditionsSET_KEYWORDSELECT column1, column2, …FROM table2…WHERE conditions

Page 3: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

3

UNION OPERATION

Uses UNION keyword Combines results of two queries Result contains all rows from both queries

Page 4: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

4

UNION OPERATION

Example: “red and green part ids” Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)

SELECT PartIdFROM Parts P1WHERE P1.color = ‘red’UNIONSELECT PartIdFROM Parts P2WHERE P2.color = ‘green’

Page 5: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

5

INTERSECTION OPERATION

Uses the INTERSECT keyword Combines results of two queries Results contains only rows that are in

results of both queries

Page 6: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

6

INTERSECTION OPERATION

Example: “pids of orange bolts” SELECT pid

FROM Parts P1WHERE P1.color = ‘orange’INTERSECTSELECT pidFROM Parts P2WHERE P2.pname LIKE ‘%bolt%’

Page 7: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

7

DIFFERENCE OPERATION

Uses the EXCEPT keyword Combines results of two queries Rows in the first query, but not in second

Page 8: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

8

DIFFERENCE OPERATION

Example: “pids of orange parts that are not bolts”

SELECT pidFROM Parts P1WHERE P1.color = ‘orange’EXCEPTSELECT pidFROM Parts P2WHERE P2.pname LIKE ‘%bolt%’

Page 9: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

9

UNION COMPATIBLE

Union, intersect, and difference require union compatible queries results of queries must be union compatible cannot combine queries that are not

Union compatible tables they have the same number of columns AND corresponding columns have the same

data type (e.g., VARCHAR) AND length

Page 10: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

10

SQL SET OPERATIONS AND UNIQUENESS

SQL set operations UNION, INTERSECT, and EXCEPT removes duplicates

If you want duplicates use UNION ALL, INTERSECT ALL, and EXCEPT ALL

Number of duplicate rows in result for m in 1st table and n in 2nd table UNION ALL: m + n INTERSECT ALL: min(m, n) EXCEPT ALL: m − n

Page 11: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

QUESTIONS?

11

Page 12: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

12

WHAT WE ARE GOING TO LEARN

In SQL the result of a query is a table? Can we query the resultant table? I.e., can we query a query? Nested queries

query results of a query SELECT inside of a SELECT two types of results two different ways to construct

Page 13: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

13

2 TYPES OF SUBQUERY RESULTS

Scalar result is a single column and row can use as a subquery to replace any single

value e.g., 5000 e.g., students with better than average GPA

SELECT StudentIDFROM StudentsWHERE GPA >

( SELECT AVG(GPA) FROM Students )

Page 14: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

14

2 TYPES OF SUBQUERY RESULTS

Table result of subquery is a table outer query uses set operations to check

contents of inner query results SELECT column1,….

FROM table1WHERE columnX set_operator

( SELECT columnA, … FROM tableA WHERE condition)

Page 15: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

15

NESTED QUERIES SET OPERATORS

IN e.g., WHERE a IN nested_query checks whether a is in the results of the nested

query EXISTS

e.g., WHERE/HAVING EXIST nested_query true if the nested query returned at least one

row UNIQUE

e.g., WHERE UNIQUE nested_query true if the nested query contains only unique

rows NOT

e.g., WHERE a NOT IN nested_query negates IN, EXISTS, and UNIQUE

Page 16: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

16

NESTED QUERIES SET OPERATORS

ANY e.g., WHERE a op ANY nested_query a is a column or a constant op is a comparison operator (<,<=,=,<>,>=,>) returns true if a op is true for any row in the

nested query e.g., 5,000 < ANY (SELECT salary FROM

employee) returns true if any employee salary is greater than

5,000

ALL e.g., WHERE a op ALL nested_query returns true if a op is true for all rows in the

nested query

Page 17: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

17

TYPE I NESTED QUERY

Nested query evaluates 1 time Produces a table Outer query compares its rows to this table e.g., course descriptions of Spring offerings

without a join between Offering and Course tables

SELECT CourseDescFROM CourseWHERE CourseNo IN

( SELECT CourseNo FROM Offerings WHERE term = ‘Spring’)

Page 18: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

18

WHEN TO USE TYPE I NESTED QUERY

Do not need to reference outer query at all nested query is independent of outer query Type I queries cannot reference outer query

Type I queries like a procedure call takes no parameters from outer query returns result for outer query to use

Page 19: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

19

WHEN TO USE TYPE I NESTED QUERY

Deleting rows related to other rows delete with join only supported by Access more generally done nested queries

e.g., remove failing athletes from Athletes(StudentId, TeamId) DELETE FROM Athletes

WHERE StudentNo IN ( SELECT StudentNo FROM Students WHERE Students.GPA < 2.0 )

Page 20: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

20

WHEN TO USE TYPE I NESTED QUERY

Simple difference problems like EXCEPT without union compatible requirement generally of the form “blank that are not blank” e.g., “all the employees who are not pilots: e.g., “all the parts not supplied by ‘Knockoff Parts’”

e.g., “all of the students who are not athletes”SELECT FirstName, LastNameFROM StudentsWHERE StudentNo NOT IN ( SELECT StudentNo

FROM Athletes )

Page 21: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

21

WHEN TO USE TYPE I NESTED QUERY

Why can’t we use “not equals” (<>) for this e.g., “all of the students who are not

athletes”SELECT FirstName, LastNameFROM Students, AthletesWHERE Students.StudentNo <> Athletes.StudentNo

Will return all students effectively does a cross-product compares Students.StudentNo to all

Athletes.StudentNo each student has an athlete that has a different

StudentNo

Page 22: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

22

REFERENCING SAME TABLE IN NESTED QUERY

Use the rename operator AS e.g., “Employees who are not managers”

SELECT E1.EmpIDFROM Employees AS E1WHERE E1.EmpID NOT IN

(SELECT E2. ManagerIDFROM Employees AS E2)

Page 23: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

23

TYPE II NESTED QUERY

References column(s) from outer query Executed once for every row in outer query

like a nested loop e.g., “all of the students who are not

athletes”SELECT FirstName, LastNameFROM StudentsWHERE NOT EXISTS

( SELECT * FROM Athletes WHERE Students.StudentNo =

Athletes.StudentNo )

Page 24: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

24

WHEN TO USE TYPE II NESTED QUERY

More difficult difference problems

Page 25: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

25

WHEN TO USE TYPE II NESTED QUERY

e.g., “Sophmores that never took a class from Phil Park”SELECT FirstName, LastNameFROM Students SWHERE Class = ‘So’ AND NOT EXISTS (SELECT * FROM (Enrollment E JOIN Offering O USING OfferNo)

JOIN Faculty F USING FacultyNo WHERE S.StudentNo = E.StudentNo

AND F.FirstName = ‘Phil’AND F.LastName = ‘Park’)

Page 26: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

26

WHEN TO USE NESTED QUERIES

You need to query a query we did not cover all possible examples difference problems are most common division problems also require nested queries

Then decide whether to use a Type I or II Type I is cheaper

Page 27: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

QUESTIONS?

27

Page 28: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

28

DIVISION

Represented by / Combines two relations (tables), A & B

A has two attributes (x,y) B has one attribute (y)

A/B is all x’s such that (x,y) exists in A for all y’s in B

Result has only a single attribute (x)

Page 29: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

29

DIVISION OPERATOR

For each x value in A consider the set of y values x maps to in A if this set contains all y values in B then x is in A/B

e.g. supplier names that supply all parts find all the parts (pids) see if an sid maps to all of those pids see if the catalog has a supplier that has a row

for every part

Page 30: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

30

DIVISION OPERATOR

Derive A/B think about what doesn’t belong in result get set of all x,y mappings remove those mappings that appear in A what’s left must be x’s that don’t map to all y’s remove them from the set of x’s

Page 31: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

31

DIVISION OPERATOR

Derive A/B example derivation on chalk board

Page 32: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

32

DIVISION IN SQL

How do we do this in SQL? Use

Type I nested query GROUP BY HAVING COUNT (*)

Page 33: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

33

DIVISION IN SQL

Example: “sids of suppliers who supply all parts”

SELECT sidFROM CatalogGROUP BY sidHAVING COUNT(*) = (

SELECT COUNT(*) FROM Parts)

Page 34: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

34

DIVISION IN SQL

Example: “sids of suppliers who supply all of the red parts”

SELECT sidFROM Catalog C JOIN Parts P1 USING pidWHERE P1.color = ‘red’GROUP BY sidHAVING COUNT(*) = (

SELECT COUNT(*) FROM Parts P2WHERE P2.color = ‘red’)

Page 35: A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,

QUESTIONS?

35