Question:
Write a query that counts the number of 300lb+ players for each of the following regions: West Coast (CA, OR, WA), Texas, and Other (everywhere else). Reference : https://mode.com/sql-tutorial/sql-case/
The relevant part of the table looks like this
name | weight | state |
---|---|---|
Giga | 370 | WY |
Chad | 350 | OR |
this is the correct solution:
SELECT CASE WHEN state IN ('CA', 'OR', 'WA') THEN 'West Coast'
WHEN state = 'TX' THEN 'Texas'
ELSE 'Other' END AS arbitrary_regional_designation,
COUNT(*) AS players
FROM benn.college_football_players
WHERE weight >= 300
GROUP BY 1
output:
| arbitrary_regional_designation | players |
|:-------------------------------|:---------|
| Other | 1590 |
| West Coast | 186 |
| Texas | 208 |
this makes sense to me, I am wondering why the below code doesn't give the right answer
SELECT CASE WHEN state IN ('CA','OR', 'WA') THEN 'West Coast'
WHEN state = 'TX' THEN 'Texas'
ELSE 'Other' END AS region,
count(weight>=300) --what's different here?
FROM benn.college_football_players
group by region
output for the above query:
| region | count |
|:---- |:------:|
| Other | 20513 |
| West Coast | 2944 |
| Texas | 2841 |
would appreciate your insight.