Subqueries used with exists  Finding intersection and difference with exists

Chapter 5: Subqueries: Using Queries Within Other Queries

Subqueries used with not exists

not exists is just like exists except that the where clause in which it is used is satisfied when no rows are returned by the subquery.

For example, to find the names of publishers who do not publish business books, the query is:

select pub_name 
from publishers 
where not exists 
   (select * 
    from titles 
    where pub_id = publishers.pub_id 
    and type = "business")
pub_name 
---------------------------------------- 
Binnet & Hardley 
 
(1 row affected) 

This query finds the titles for which there have been no sales:

select title 
from titles 
where not exists 
   (select title_id 
    from salesdetail 
    where title_id = titles.title_id) 
title 
----------------------------------------- 
The Psychology of Computer Cooking 
Net Etiquette 
 
(2 rows affected) 




Copyright © 2005. Sybase Inc. All rights reserved. Finding intersection and difference with exists

View this book as PDF