Subquery restrictions  Qualifying column names

Chapter 5: Subqueries: Using Queries Within Other Queries

Example of using a subquery

Suppose you want to find the books that have the same price as Straight Talk About Computers. First, you find the price of Straight Talk:

select price 
from titles 
where title = "Straight Talk About Computers" 
price 
------------- 
       $19.99 
 
(1 row affected)

You 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                                         price 
------------------------------------------    -----
The Busy Executive’s Database Guide           19.99 
Straight Talk About Computers                 19.99 
Silicon Valley Gastronomic Treats             19.99 
Prolonged Data Deprivation: Four Case Studies 19.99 
  
(4 rows affected) 

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 Busy Executive’s Database Guide           19.99 
Straight Talk About Computers                 19.99 
Silicon Valley Gastronomic Treats             19.99 
Prolonged Data Deprivation: Four Case Studies 19.99 
 
(4 rows affected) 




Copyright © 2005. Sybase Inc. All rights reserved. Qualifying column names

View this book as PDF