Sie sind auf Seite 1von 13

SQL - Subqueries

Subqueries

Subquery one select statement inside another Used in the WHERE clause Subqueries can return many rows. Used as a replacement for view or selfjoin. Some programmers see them as easier to understand than other options. The main drawback is that they can be much slower than selfjoin or view.

Simple Example
Who in the database is older than Jim Smith? SELECT dob FROM driver WHERE name = Jim Smith; Output: Dob 11 Jan 1980 SELECT name FROM driver WHERE dob > 11 Jan 1980; Output: name Bob Smith Bob Jones

Combined together: SELECT name FROM driver WHERE dob > (SELECT dob FROM driver WHERE name = Jim Smith);

ANY and ALL

To support subqueries which return more than 1 row we need some additional operators ANY and ALL. ANY means greater than at least one value ALL all means equal to every value The ANY or ALL operator goes immediately before the open bracket of the subquery.

Example
What cars are the same colour as a car owned by Jim Smith? Jim owns 2 cars, one is RED and the other BLUE. We looking for cars which are either RED or BLUE. SELECT regno FROM car WHERE colour = ANY ( SELECT colour FROM car WHERE owner = Jim Smith);

Ex: find the books that have the same price as Straight Talk About Computers. select price from titles where title = "Straight Talk About Computers" Output: price ------------$19.99 (1 row affected) Use the results of the first query in a second query to find all the books that cost the same as Straight Talk:

select title, price from titles where price = $19.99 title ---------------------------------The Database Guide Straight Talk to Computers Gastronomic Treats price ----------------------19.99 19.99 19.99

You can use a subquery to receive the same results in only one step: select title, price from titles where price = (select price from titles where title = "Straight Talk About Computers") ; title price ---------------------------------- ----------------------The Database Guide 19.99 Straight Talk to Computers 19.99 Gastronomic Treats 19.99

Multiple levels of nesting


"Find the names of authors who have participated in
writing at least one popular computing book:" select au_lname, au_fname from authors where au_id in (select au_id from titleauthor where title_id in (select title_id from titles where type = "popular_comp") );
The outermost query selects all author names. The next query finds the authors' IDs, and the innermost query returns the title ID numbers PC1035, PC8888, and PC9999.

Subqueries in update, delete, and insert statement The following query doubles the price of all books published by New Age Books. The statement updates the titles table; its subquery references the publishers table.

update titles set price = price * 2 where pub_id in (select pub_id from publishers where pub_name = "New Age Books")

You can remove all records of sales of business books with this nested select statement: delete salesdetail where title_id in (select title_id from titles where type = "business") ;

Subqueries vs Join
Sub-queries are mainly useful for when you actually do need to use 2 queries to find data. An example would be, select all people who's sales were above average. Well, first you have to find out the average (1 query there)and then you have to compare everyone's sales against that average (the second select). while the join will only be selecting one select statement, so by this attribute a join statement will always be faster...

Das könnte Ihnen auch gefallen