14
Subqueries Steve Perry Email: [email protected] 1

Subqueries Steve Perry Email: [email protected] 1

Embed Size (px)

Citation preview

Page 1: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

1

Subqueries

Steve Perry

Email: [email protected]

Page 2: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

2

Subqueries

• A Subquery nests a SELECT statement in the WHERE clause of another SELECT, INSERT, UPDATE, or DELETE statement

• A Subquery’s Outer SELECT operates on rows returned from the Inner SELECT• The Inner and Outer SELECT statements may refer to the same table

Page 3: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

3

Example - without a Subquery

• Find all the books that have the same price as Straight Talk About Computers

SELECT price

FROM book

WHERE title = 'Straight Talk About Computers';

SELECT title, price

FROM book

WHERE price = 19.99;

Page 4: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

4

Example - Using a Subquery

• SELECT title, priceFROM bookWHERE price IN

(SELECT price FROM book WHERE title = 'Straight Talk About Computers');

Page 5: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

5

Joins vs. Subqueries• Join

– Advantages: Can return results from all tables– Disadvantages: May need two statements to get desired results

• Subquery– Advantages: Can compare aggregates to other values. Can get information in a single

statement– Disadvantages: Can only return results from the Outer Select statement

Page 6: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

6

Example - Two Ways

• SELECT name, lastname, firstnameFROM publisher JOIN author USING (city);

• SELECT nameFROM publisherWHERE city IN

(SELECT city FROM author);

Page 7: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

7

The IN operator

• In a simple SELECT statement you can avoid specifying many OR’s by using an IN list– Instead of…

SELECT lastname, firstname, state

FROM author

WHERE state='TN' or state='KS' or state='MD'

– Do …SELECT lastname, firstname, state

FROM author

WHERE state IN ('TN', 'KS', 'MD')

Page 8: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

8

The IN operator with a Subquery• Use the ‘IN’ operator when the results of the inner query will have 0, one, or more

values• Once the inner query returns results, the outer query will make use of them

SELECT lastname, firstname, state

FROM author

WHERE state IN

(SELECT state

FROM author

WHERE state != 'CA')

Page 9: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

9

Conceptually...

• Inner Query-

SELECT state

FROM author

WHERE state != 'CA'

• Outer Query-

SELECT name

FROM author

WHERE state IN ('KS', 'TN', 'OR', 'MI', 'IN', 'MD', 'UT');

Page 10: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

10

Exercise

• List the author’s name who appear as the third author on a book.

Page 11: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

11

Answer

• SELECT lastname, firstnameFROM authorWHERE ssn IN

(SELECT ssn FROM bookauthor WHERE author_order = 3);

Page 12: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

12

Using Aggregate Functions

• Aggregate Functions always return a single value for a set of rows

• SELECT title, ytd_sales * price, advanceFROM bookWHERE ytd_sales * price >

(SELECT MAX(advance) FROM book);

Page 13: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

13

Insert, Update, and Delete

• UPDATE bookSET price = price * 2WHERE pub_id IN

(SELECT pub_id FROM publisher WHERE name = 'New Age Books');

Page 14: Subqueries Steve Perry Email: steveperrymail@yahoo.com 1

14

Last Slide