T-SQL: Subquery – SELECT

Please see my other Database Development articles.

Performing multiple, concurrent queries

Often when needing a resultset, there’s a need to perform multiple queries to derive the desired data.
In this example, we have multiple records of people, from which we need an aggregated report.

1-t-sql-subquery-select

In order to accomplish this goal, I must perform two separate queries on the same data.
First, I list each person’s age, the sum of all peoples’ ages, then their age as a percentage compared against the total of all ages.

2-t-sql-subquery-select

The final data illustrates the intended aggregation.

3-t-sql-subquery-select

Leave a comment