6.1

The Subquery In a SELECT Statement

The Subquery in a SELECT Statement

  • A subquery is a SELECT statement coded within another SELECT statement.
  • A subquery can return a single value or a list of values.
  • A subquery can return multiple columns.
  • A subquery cannot make use of the ORDER BY clause
  • A subquery can be nested within another subquery
  • You can use a subquery in a WHERE, HAVING, FROM and SELECT clause.

Code Sample:

1    USE world;
2    SELECT name, population 
3    FROM city 
4    WHERE CountryCode IN 
5        (SELECT code 
6        FROM country 
7        WHERE region = 'Caribbean') 
8    ORDER BY population 
9    LIMIT 5;

Results:

sub_01.png

SELECT name, population

FROM city

WHERE CountryCode IN

(SELECT code

 FROM country

 WHERE region = 'Caribbean')

CC BY-NC-ND International 4.0

CC BY-NC-ND International 4.0: This work is released under a CC BY-NC-ND International 4.0 license, which means that you are free to do with it as you please as long as you (1) properly attribute it, (2) do not use it for commercial gain, and (3) do not create derivative works.

End-of-Chapter Survey

: How would you rate the overall quality of this chapter?
  1. Very Low Quality
  2. Low Quality
  3. Moderate Quality
  4. High Quality
  5. Very High Quality
Comments will be automatically submitted when you navigate away from the page.
Like this? Endorse it!