Home > Ask the Oracle Experts > SQL Questions & Answers > Having all values in a list
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Having all values in a list

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 21 June 2004
I have a table that has two ids: id1 and id2, and there are multiple id2's for every id1. I have a list of id2s, and I want to write a select statement that will pull the id1s that have a row for each of the id2s in my list. How would I do that?

>
EXPERT RESPONSE

This question often comes up when trying to find all job candidates who have a certain list of skills, all products that have a certain list of keywords, etc. These situations all share one characteristic: they involve a one-to-many relationship. You're trying to find instances of the "one" value which have every specified instance of the "many" values. Never mind that the "relationship" in this particular situation is within the same table, the principle still holds.

The first thing you do is to set up a basic SELECT for the "many" values which have a value in the list:

select id1
  from yourtable
 where id2 
    in ( list of values )

The above query will give you all id1's which have any of the id2 values in the list. Clearly, you just want the id1's that have all of them.

The trick here is to ask yourself how many id2 values are in the list. Then just put that number into the query as an aggregate condition:

select id1
  from yourtable
 where id2 
    in ( list of values )
group
    by id1
having count(distinct id2) = number

Neat, eh?

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts