SELECT .. SELECT

From SQLZOO
Revision as of 00:52, 9 August 2012 by Andr3w (Talk | contribs)

Jump to: navigation, search

The Derived Table (SELECT FROM SELECT)

You can use the results from one query in another query

You may use a SELECT statement in the FROM line. In this case the derived table X has columns name and gdp_per_capita. The calculated values in the inner SELECT can be used in the outer SELECT.

SELECT name, ROUND(gdp_per_capita)
  FROM
  (SELECT name,
          gdp/population AS gdp_per_capita
     FROM bbc) X
 WHERE region='North America'

What about ties?

If one region has two countries with the same, highest population then the query will produce the right answers but there will be duplication- both countries will be shown.

See also:
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense