BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
If you’ve been writing SQL queries for a while you’re probably fairly confident with writing single SELECT statements to return a set of records. If you’re ready for a bit more of a challenge, this blog will teach you how to nest one SELECT statement inside another in order to create a subquery. If you’re thinking “that sounds simple enough” then you might want to read to the end of this series, where we’ll attempt to melt your brain by explaining correlated subqueries!
- Creating a Simple Subquery
- Subqueries and Aggregate Functions (this blog)
- Using ALL, ANY, SOME and IN with Subqueries
- Correlated Subqueries
Posted by Andrew Gould on 28 January 2013
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Subqueries and Aggregate Functions
In the first part of this series we saw a simple example of a subquery which included the MAX function. Subqueries are often used with aggregate functions such as MAX, and this part of the series will show a couple of the other useful functions that you can use.
A Reminder of the Aggregate Functions
You might find this blog on grouping data useful as a reminder of which aggregate functions are available in SQL.
Finding the Highest or Lowest Value in a Column
We've already seen how to use the MAX function to match records against the largest value in a column. You can also use the MIN function to find the smallest value in a field. The query below would find the film or films with the shortest running time:
The results of this query are shown below:
Short duration, long title!
Finding Records Above or Below Average
You can use the AVG function in a subquery to return the average of a column of numbers. You can then combine this with the greater-than and less-than operators to find records that are above or below average. The query below shows all of the films whose running time is longer than the average running time of all the films in the table:
Adding More Criteria
You can add further criteria to the WHERE clause of both the inner and outer queries to make your queries even more specific. The example below looks for all films which have been released since the year 2000, and whose budget is greater than the highest budget of any film released before the year 2000.
FilmReleaseDate >= '2000-01-01'
FilmReleaseDate < '2000-01-01')
The results of the query are shown below:
Three films in our database cost more to make than the most expensive film made before the year 2000.
Technically, we don't need the criterion which asks for films released on or after January 1st 2000. The only films that could be more expensive than the most expensive film made prior to that date must have been made after that date! I've included it here just to show how to use multiple criteria.
Now that you've seen how to use basic criteria with subqueries it's time to take a look at some of the more exotic comparisons using keywords such as ALL, ANY and IN.