Queries with derived tables (I)
The focus of this lesson is derived tables. You can use derived tables when you want to break down a complex query into smaller steps. A derived table is a query which is used in the place of a table. Derived tables are a great solution if you want to create intermediate calculations that need to be used in a larger query.
In this exercise, you will calculate the maximum value of the blood glucose level for each record by age.
This exercise is part of the course
Intermediate SQL Server
Exercise instructions
- Return
MaxGlucose
from the derived table. - Join the derived table to the main query on
Age
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
SELECT a.RecordId, a.Age, a.BloodGlucoseRandom,
-- Select maximum glucose value (use colname from derived table)
b.___
FROM Kidney a
-- Join to derived table
___ (SELECT Age, MAX(BloodGlucoseRandom) AS MaxGlucose FROM Kidney GROUP BY Age) b
-- Join on Age
___