Many Uses of SQL Subqueries

From sasCommunity
Jump to: navigation, search

by Tasha Chapman

This paper acquaints the reader with SQL subqueries, and provides some basic examples of how they might be used.

Subqueries are very useful tools in SQL. A subquery is an embedded query nested within another query. They’re most often used when a particular value that is needed for the outer query is unknown, but can be searched for using an SQL query. The subquery is processed first, and then the results of the subquery are passed back and used as part of the outer query. Subqueries are most often used in the WHERE or HAVING clause, in which the results of the subquery are used as a condition of the outer query. But subqueries can also be used in the SELECT or FROM clause, if appropriate.

Still confused? No worries. This paper includes plenty examples to illustrate the concept of subqueries and how you might use them.


Example datasets:



Part two of the series can be found here.


Portions of this paper were presented at the Western Users of SAS Software (WUSS) 2011 Conference in San Francisco, California. (Winner - Best Contributed Paper)
Presented at a Puget Sound SAS Users Group meeting.
Presented at a State of Oregon SAS Users Group meeting.

Find more SOSUG presentations, as well as other tips, tricks, and tools created by or recommended by SOSUG members here.