SQL Sub-Queries in SQL Peter Y. Wu Department of Computer and Information Systems Robert Morris University Data Definition Language Create table for schema and constraints Drop table Data Manipulation Language (Update) Insert, Delete, Update Data Manipulation Language (Query) Select-from-where: simple queries with conditions Select-from-where: aggregate functions and grouping Select-from-where: multiple tables with JOIN operation Select-from-where: sub-queries 2 3 Types of Sub-Queries An SQL query generates a result table even when there is no data! Scalar: table with one column and one row. Vector: table with one column only. Table: any number of columns and rows. An SQL sub-query is an SQL query embedded inside an (outer) SQL query. 3 Types of Sub-Queries We can use sub-queries in different ways: Scalar: as if it is a scalar involved in a term of the where conditions. Vector: as if it is a list/set involved in quantified comparison in where conditions. Table: used in the from clause as a table. An SQL sub-query can be embedded many many levels deep. 3 4 Scalar Sub-Query Example Find the s who died the latest. We will need a scalar result from the database. Scalar Sub-Query Example We need a scalar value from the database to be used in a query Piece PNo PName CNo 1000 The Four Seasons 1 1002 B-minor Mass 2 1003 Christmas Oratorio 2 1004 Missa Solemnis 3 1006 Classical Symphony 4 1007 Cinderella 4 select CName from where Died = (select max(died) from ); The sub-query generates a scalar result a number for our use in the (outer) query. 5 6 (c) Peter Y Wu - RMU. 1
Vector Sub-Query The SQL Query generates a result table with exactly one column. We use the result table as a list/set for comparison in the (outer) Query. The Vector sub-query may or may not contain duplicates. Let us review the use of list/set for comparison in the where conditions. 7 Vector Sub-Query Example Find the s who was born the earliest. without using an aggregate function... Piece PNo PName CNo 1000 The Four Seasons 1 1002 B-minor Mass 2 1003 Christmas Oratorio 2 1004 Missa Solemnis 3 1006 Classical Symphony 4 1007 Cinderella 4 8 Vector Sub-Query Example We need to find the minimum from a list of values (under column Born in the table). select CName from where Born <= all (select Born from ); The sub-query generates a list also called a vector for use in the (outer) query. The sub-query is called a vector sub-query. Vector Sub-query: returning a list select CName from where Born <= all (select Born from ); Picks the earliest born composer select CName from where not(born > any (select Born from )); The two conditions are logically equivalent; the two SQL statements give the same result for any set of data. Also called a nested QUERY, with inner query (that is, the sub-query returning a set) in the outer query. 9 10 SQL: quantified list comparison Note that these operator pairs are equivalent: = any ( ) and not <> all ( ) > any ( ) and not < all ( ) < any ( ) and not > all ( ) >= any ( ) and not <= all ( ) <= any ( ) and not >= all ( ) 11 Vector Sub-Query Examples STUDENT ENROLLMENT SSN First Last SSN CNum 123-45-6789 John Smith 123-45-6789 10011 111-22-3333 Ann Miller 111-22-3333 10066 543-76-9821 David Meyer 543-76-9821 10066 900-50-3000 Mary Jones 900-50-3000 10011 COURSE CNum Topic Title Instructor Enroll 10011 1050 Information Systems Wood 25 11007 2240 Data Structures Kovacs 30 10066 4240 Database Management Wu 28 21003 4770 Java Programming Wu 17 21004 4990 Electronic Commerce Kovacs 20 12 (c) Peter Y Wu - RMU. 2
Vector Sub-Query Examples STUDENT ENROLLMENT SSN First Last SSN CNum Section Term 123-45-6789 John Smith 123-45-6789 1050 A Spring 07 111-22-3333 Ann Miller 111-22-3333 4240 A Spring 08 543-76-9821 David Meyer 543-76-9821 4240 A Spring 08 900-50-3000 Mary Jones 900-50-3000 1050 A Spring 07 COURSE CNum Section Term Title Instructor Enroll 1050 A Spring 07 Information Systems Wood 25 2240 B Fall 08 Data Structures Kovacs 30 4240 A Spring 08 Database Systems Wu 28 4770 A Fall 07 Java Programming Wu 17 4990 A Spring 07 Electronic Commerce Kovacs 20 13 SQL Query with multiple tables List the names of the students who are taking courses taught by instructor Wu. select s.first, s.last from student s, enrollment e, course c and e.cnum = c.cnum and e.section = c.section and e.term = c.term and c.instructor = Wu ; 14 SQL operators: in, not in Select from where <term> [not] in (<list>); <term> in (<list>) is true if the value of term is found in the set. (4240, 4770); or using a vector sub-query: (select CNum from course where Instructor = Wu ); 15 Sub-query: returning a list (select CNum from course where Instructor= Wu ); The sub-query returns the set of CNum s of courses taught by Instructor Wu. The outer query therefore lists the students who take courses taught by Instructor Wu. 16 SQL: quantified list comparison operators in ( ) and not in ( ) Select from where <term> in (<list>); is the same as Select from where <term> = any (<list>); Select from where <term> not in (<list>); is the same as Select from where <term> <> all (<list>); 17 SQL operator: exists Select from where [not] exists (<list>); select s.first, s.last from student s where not exists (select * from enrollment e ); Listing all students who are not enrolled in any course at all. 18 (c) Peter Y Wu - RMU. 3
Sub-query: union of two lists union (select CNum from course where Title like Data% ); The sets returned are unioned: listing all students who take courses taught by Wu OR courses with a title beginning with Data. 19 Sub-query: union of two lists select s.first, s.last from student s, enrollment e union (4240, 4770); A set is a list of terms, or a vector sub-query. Recall that <list> ::= <term>,, <term> <sub-query> 20 Sub-query: union all select s.first, s.last from student s, enrollment e union all (4240, 4770); will not remove duplicates, simply concatenate the two result sets may be more efficient. Duplicate removal can be quite expensive. 21 Use a generated table in the from clause. Same as using a view a table defined by an SQL query: the data depends on the base tables the query selects from. A special form of insert statement also uses a table sub-query see example. 22 Select CNo, CName, 1+floor(Born/100) as Century from ; CNo CName Century 1 Vivaldi 17 2 Bach 17 3 Mozart 18 4 Prokofiev 19 5 Dvorak 19 23 CNo CName Century 1 Vivaldi 17 2 Bach 17 3 Mozart 18 4 Prokofiev 19 5 Dvorak 19 Select Century, count(cname) from (select CNo, CName, 1+floor(Born/100) as Century from ) group by Century; Century count(cname) 17 2 18 1 19 2 24 (c) Peter Y Wu - RMU. 4
SQL: insert two forms TWO forms of insert statement: insert into <table-name> [ (<column>,, <column>) ] values (<expr>,, <expr>); insert into <table-name> [ (<column>,, <column>) ] ( <select-statement> ); SQL insert: using a table sub-query create table T2 (N1 integer, N2 integer); insert into T2 (N2,N1) select c1,c2 from T1; C1 T1 C2 11 23 22 45 33 67 44 N1 T2 N2 23 11 45 22 67 33 44 25 26 (c) Peter Y Wu - RMU. 5