So, I’m doing the Usage Funnels module in the Analyze Data with SQL lesson and I’m having trouble understanding the somethings about the select statement with aggregate functions in the syntax.
Now, I understand the basic function of select is to get the data from the specified column in the table, and I understand that the basic functions of aggregates is to calculate and return the values of the specified data in the table, but what I don’t understand is the correct order of the columns in the syntax when two or more columns are presented and one or more of those columns is attached to an aggregate.
For example, in this exercise: https://www.codecademy.com/paths/analyze-data-with-sql/tracks/analyze-data-sql-analyze-real-data/modules/analyze-data-sql-usage-funnels/lessons/sql-funnels/exercises/survey-funnel-i
the task is to return the number of distinct users who answered each question.
this is the syntax:
select question_text, count(distinct user_id)
group by 1;
I don’t understand why the syntax is select question_text, count(distinct user_id) rather than select distinct user_id, count(all), or select distinct user_id, question_text, count(all). [I had to use the word ALL here because the asterisks aren’t showing in the post.]
I have noticed a pattern in the way the instructions are written where the columns are presented in the instructions in the opposite way they are to be written in the syntax, by why is it that way? Why do we have to select the question_text first, THEN count the distinct user id?
Thanks a lot in advance for your help!