SQL Usage Funnels - Understanding the syntax

Hi everyone!

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)

from survey_responses

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!

Hello @arc1144614037, welcome to the forums! I would imagine they want you to select the question_text first, as they want the question to be shown first on the results table:

question_text    | number
How are you?     | number
What's your name?| number

The reason for count(distinct_user_id) is that it returns count of all the distinct user ids, which is important in counting the number of people who answered each question. The overall results table should then look like (layout, not values):

question_text    | count(distinct_user_id)
How are you?     | 8
What's your name?| 7

I hope this has helped!

2 Likes