Home > Ask the Oracle Experts > SQL Questions & Answers > Querying complex derived tables
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Querying complex derived tables

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

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


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 19 June 2008
I have a query similar to the one in the article Optimize SQL query but with the difference that my inner query results come from five different tables rather than just one (store_sales in this case). So can I use "d. " notation in the outer query to refer to these elements for the select? Right now, it's also showing an error: Sql Not properly closed command at the " as d " location in the query. Could you help me with this please?


>
EXPERT RESPONSE
Yes, your derived table can be any valid query. Just to recap, the query structure from the previous answer looked like this:

select d.foo
  from (
       select ...
         from store_sales
        where ...
       ) as d

A derived table is the result of running the query that is inside the parentheses. More correctly, it's a subquery, because it's inside the main query. Notice that the outer query's SELECT clause uses the "d." prefix on the columns.

You can put any query into parentheses in the FROM clause like this. However, it is usually necessary to name the derived table, and that's done with the AS clause, which assigns a table alias. Some database systems allow you to omit this alias:

select foo
  from (
       select ...
         from store_sales
        where ...
       )

However, you would of course need to use an alias if you wanted to join the derived table to another table in the main query.

Your syntax error might be the result of using the AS keyword in Oracle. For some reason, Oracle doesn't allow it. Try this instead:

select d.foo
  from (
       complex 5-table query here
       ) d

Note the AS keyword is omitted.


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


RELATED CONTENT
SQL
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL

Oracle development languages
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table
SQL to select rows 1000 through 3000 in a table
SQL query to combine rows
The SQL REPLACE function
CASE expressions in the ORDER BY clause
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL

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