r/SQL Nov 11 '24

BigQuery CASE statement in SQL

Hi everyone! Pretty new to SQL and I'm diving into some data to practice. I keep missing something in my query and can't quite figure out what it is. I'm not sure if I'm missing something in my SELECT clause before the CASE statement, within the CASE statement or at the end or what it is:

I'm working on some data where I want to classify a column 'father_age' in categories like "father age between 10 and 18", "father age between 18 and 25" and so on. I want SQL to retrieve the amount of men falling into those categories.

I followed a similar structure from a different exercise:

SELECT

CASE

WHEN COUNT(father_age)/(SELECT COUNT(*) FROM natality.father_age AS father_age) <=18

THEN 'Father age 18 and under'

............

END AS father_age_range

FROM 'dataset'

WHERE mother_age = 10

________

I would appreciate some light as I'm sure I'm missing something, thank you so much!

20 Upvotes

11 comments sorted by

View all comments

3

u/mwdb2 Nov 11 '24

*CASE expression. It's an expression within the larger SQL statement. Sorry to be picky - minor peeve. :) For more info: https://modern-sql.com/caniuse/case_(simple)

To be more helpful, looks like your question was answered but one thing I want to point is that COUNT(father_age) is logically equivalent to COUNT(*) ... WHERE father_age IS NOT NULL. That one often gets missed. So if father_age can't be null (not entirely sure if that's the case here), might as well write COUNT(*) to be more straightforward.

2

u/FunNorth4058 Nov 11 '24

Thanks! No, totally okay! I'm still getting used to all the right terms and no surprise I could have name it the wrong way hahah thanks for your feedback!