Difference between revisions of "Using nested SELECT"

From SQLZOO
Jump to: navigation, search
(Using a query on the SELECT line)
Line 4: Line 4:
 
     <p>The result of a SELECT statement may be used as a value in
 
     <p>The result of a SELECT statement may be used as a value in
 
     another statement. For example the statement  
 
     another statement. For example the statement  
     <b>SELECT region FROM bbc WHERE name = 'Brazil'</b>
+
     <b>SELECT continent FROM world WHERE name = 'Brazil'</b>
  
 
     evaluates to <code>'South America'</code> so we can use this value to
 
     evaluates to <code>'South America'</code> so we can use this value to
     obtain a list of all countries in the same region as
+
     obtain a list of all countries in the same continent as
 
     'Brazil'</p>
 
     'Brazil'</p>
  
 
<div class='qu'>
 
<div class='qu'>
List each country in the same region as 'Brazil'.
+
List each country in the same continent as 'Brazil'.
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name FROM bbc WHERE region =  
+
SELECT name FROM world WHERE continent =  
(SELECT region
+
(SELECT continent
FROM bbc WHERE name = 'Brazil')
+
FROM world WHERE name = 'Brazil')
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
 
SELECT name  
 
SELECT name  
FROM bbc WHERE region =  
+
FROM world WHERE continent =  
   (SELECT region FROM bbc WHERE name = 'Brazil')
+
   (SELECT continent FROM world WHERE name = 'Brazil')
 
</source>
 
</source>
 
</div>
 
</div>
Line 28: Line 28:
 
<p>Some versions of SQL insist that you give the subquery an <i>alias</i>. Simply put <code>AS somename</code> after the closing bracket:
 
<p>Some versions of SQL insist that you give the subquery an <i>alias</i>. Simply put <code>AS somename</code> after the closing bracket:
 
<pre>
 
<pre>
SELECT name FROM bbc WHERE region =  
+
SELECT name FROM world WHERE continent =  
   (SELECT region FROM bbc WHERE name='Brazil') AS brazil_region
+
   (SELECT continent FROM world WHERE name='Brazil') AS brazil_continent
 
</pre>
 
</pre>
 
</p>
 
</p>
Line 38: Line 38:
 
It is safer to use IN to cope with this possibility.
 
It is safer to use IN to cope with this possibility.
  
The phrase <code>(SELECT region FROM bbc WHERE name = 'Brazil' OR name='Mexico')</code>
+
The phrase <code>(SELECT continent FROM world WHERE name = 'Brazil' OR name='Mexico')</code>
 
will return two values ('North America' and 'South America'). You should use:
 
will return two values ('North America' and 'South America'). You should use:
  SELECT name, region FROM bbc
+
  SELECT name, continent FROM world
  WHERE region IN (SELECT region FROM bbc WHERE name='Brazil'
+
  WHERE continent IN (SELECT continent FROM world WHERE name='Brazil'
 
                                             OR name='Mexico')
 
                                             OR name='Mexico')
  
  
 
<div class='qu'>
 
<div class='qu'>
List each country and its region in the same region as 'Brazil' or 'Mexico'.
+
List each country and its continent in the same continent as 'Brazil' or 'Mexico'.
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name, region FROM bbc
+
SELECT name, continent FROM world
WHERE region IN (SELECT region
+
WHERE continent IN (SELECT continent
                   FROM bbc WHERE name='Brazil'
+
                   FROM world WHERE name='Brazil'
 
                               OR name ='Mexico')
 
                               OR name ='Mexico')
 
</source>
 
</source>
Line 60: Line 60:
 
<p class='imper'>Show the population of China as a multiple of the population of the United Kingdom</p>
 
<p class='imper'>Show the population of China as a multiple of the population of the United Kingdom</p>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT population/(SELECT population FROM bbc
+
SELECT population/(SELECT population FROM world
 
                     WHERE name='United Kingdom')
 
                     WHERE name='United Kingdom')
   FROM bbc
+
   FROM world
 
WHERE name = 'China'
 
WHERE name = 'China'
 
</source>
 
</source>
Line 79: Line 79:
 
Note that we mean greater than every single country in Europe; not the combined population of Europe.
 
Note that we mean greater than every single country in Europe; not the combined population of Europe.
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name FROM bbc
+
SELECT name FROM world
 
  WHERE population > ALL
 
  WHERE population > ALL
       (SELECT population FROM bbc
+
       (SELECT population FROM world
         WHERE region='Europe')
+
         WHERE continent='Europe')
 
</source>
 
</source>
 
</div>
 
</div>
  
 
Return to the [[SELECT within SELECT Tutorial]]
 
Return to the [[SELECT within SELECT Tutorial]]

Revision as of 23:02, 8 December 2012

Return to the SELECT within SELECT Tutorial

Contents

Using SELECT in SELECT

See SELECT FROM SELECT for how to use a derived table.

The result of a SELECT statement may be used as a value in another statement. For example the statement SELECT continent FROM world WHERE name = 'Brazil' evaluates to 'South America' so we can use this value to obtain a list of all countries in the same continent as 'Brazil'

List each country in the same continent as 'Brazil'.

SELECT name FROM world WHERE continent = 
(SELECT continent 
FROM world WHERE name = 'Brazil')
SELECT name 
FROM world WHERE continent = 
  (SELECT continent FROM world WHERE name = 'Brazil')

Notes

Some versions of SQL insist that you give the subquery an alias. Simply put AS somename after the closing bracket:

SELECT name FROM world WHERE continent = 
  (SELECT continent FROM world WHERE name='Brazil') AS brazil_continent

Multiple results from the subquery

The subquery may return more than one result - if this happens the query above will fail as you are testing one value against more than one value. It is safer to use IN to cope with this possibility.

The phrase (SELECT continent FROM world WHERE name = 'Brazil' OR name='Mexico') will return two values ('North America' and 'South America'). You should use:

SELECT name, continent FROM world
WHERE continent IN (SELECT continent FROM world WHERE name='Brazil'
                                           OR name='Mexico')


List each country and its continent in the same continent as 'Brazil' or 'Mexico'.

SELECT name, continent FROM world
WHERE continent IN (SELECT continent 
                   FROM world WHERE name='Brazil'
                               OR name ='Mexico')

Using a subquery on the SELECT line

If you are certain that only one value will be returned you can use that query on the SELECT line.

Show the population of China as a multiple of the population of the United Kingdom

SELECT population/(SELECT population FROM world
                    WHERE name='United Kingdom')
  FROM world
WHERE name = 'China'

Using binary operators over a set

These operators are binary - they normally take two parameters:

=     equals
>     greater than
<     less than
>=    greater or equal
<=    less or equal

You can use the words ALL or ANY where the right side of the operator might have multiple values.

Show each country that has a population greater than the population of ALL countries in Europe.

Note that we mean greater than every single country in Europe; not the combined population of Europe.

SELECT name FROM world
 WHERE population > ALL
      (SELECT population FROM world
        WHERE continent='Europe')

Return to the SELECT within SELECT Tutorial

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense