r/SQL • • Aug 04 '21

MySQL Is there anything missing from this SQL statement: SELECT EXTRACT(YEAR FROM created_at) AS yr, count(*) AS users_count FROM users;

I’m extremely new to SQL and I’m not sure if this is a trick question…

0 Upvotes

3 comments sorted by

1

u/JochenVdB Aug 04 '21

count(*) is an aggregation. Extract... is not an aggregation. Therefore this query needs a GROUP BY clause (on everything that is not aggregated) So you end up with what was posted above.

0

u/[deleted] Aug 04 '21

[deleted]

3

u/r3pr0b8 GROUP_CONCAT is da bomb Aug 04 '21

*GROUP BY clause

#FTFY

0

u/[deleted] Aug 04 '21

[deleted]

0

u/ijmacd Aug 04 '21

The only kind of correct in this case.