Home > Ask the Oracle Experts > SQL Questions & Answers > Selecting from several tables
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Selecting from several tables

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: 05 June 2002
I have several tables that I need to search for a specific WHERE condition. I want to end up with one column when it's all done. I'm trying to do something to the effect of "SELECT FieldName FROM table6,table7,table8 WHERE Condition='1';", however this doesn't work for several reasons. I've tried modifying the query to be "SELECT table6.FieldName,table7.FieldName...", but then I end up with an ambiguous WHERE condition. Help!

>
EXPERT RESPONSE

You're right that "SELECT FieldName FROM table6, table7, table8 WHERE Condition" won't work for several reasons -- at the very least, you'll end up with a gazillion useless rows, because without a join condition you'll get all possible combinations of all rows from all the tables; this is called a cross join. The tables are probably not even joinable, which is to say that although you can cross-join them, it doesn't make sense to.

Try a UNION. This allows you to select rows from each table individually, and merge all the resulting rows together into one result set.

select FieldName
    from table6
   where Condition='1'
union 
  select FieldName
    from table7
   where Condition='1'
union 
  select FieldName
    from table8
   where Condition='1'

If you use UNION, then any duplicate FieldName values, coming from more than one of the subselects, will be merged into one result row. If you want to preserve the individual (duplicate) values in the result set, use UNION ALL.

For More Information


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


RELATED CONTENT
SQL
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
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

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