Get startedGet started for free

Column does not exist

When using WHERE as a filter condition, it is important to think about the processing order in the query. In this exercise, you want a query that returns NBA players with average total rebounds of 12 or more per game. The following formula calculates average total rebounds from the PlayerStats table;

$$Average Total Rebounds = \dfrac{(Defensive Rebounds + Offensive Rebounds)}{Games Played}$$

The first query in Step 1 returns an error. Select Run Code to view the error. The second query, in Step 2, will give you the results you want, without error, by using a sub-query.

Note that GamesPlayed is CAST AS numeric to ensure we get decimal points in our output, as opposed to whole numbers.

This exercise is part of the course

Improving Query Performance in SQL Server

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

-- First query

SELECT PlayerName, 
    Team, 
    Position,
    (DRebound+ORebound)/CAST(GamesPlayed AS numeric) AS AvgRebounds
FROM PlayerStats
WHERE AvgRebounds >= 12;
Edit and Run Code