Aggregate Functions Project some trivial questions

https://www.codecademy.com/paths/data-science/tracks/sql-intermediate/modules/dspath-sql-aggregates-projects-and-applications/projects/sql-aggregates-crypto

While going through the project, trying to solve the 4th instruction, I’ve got a question in order of SELECT clause.

instruction
It was reported that Bitcoin dominates Fiddy Cent’s exchange. Let’s see if it is true within these dates by answering two questions:

  • How many money_in transactions are in this table?
  • How many money_in transactions are in this table where ‘BIT’ is the currency ?
SELECT currency, COUNT(money_in), COUNT(money_out)
FROM transactions
WHERE currency = 'BIT'
GROUP BY 1;

The code above is my code and somehow the code given by the instruction was different from mine.

SELECT COUNT(money_in)
FROM transactions;

SELECT COUNT(money_in)
FROM transactions
WHERE currency = 'BIT';

And the code above is the code given by the instruction.

So my question is does the order of putting the column names in SELECT clause cause difference in output?

For instance,

SELECT currency, COUNT(money_in), COUNT(money_out)
FROM transactions
WHERE currency = 'BIT'
GROUP BY 1;

And

SELECT currency, COUNT(money_out), COUNT(money_in)
FROM transactions
WHERE currency = 'BIT'
GROUP BY 1;

would these two codes return different result??
I happened to think of the possibility of different result coming up because I think the first clause will filter the second clause and the second clause will filter the third clause.

Yes, in the sense that the columns of your output will be returned in the order you specify in your SELECT statement. No, in the sense that SELECT simply tells SQL what fields you are interested in and so the actual data returned will be the same.

See how in both output rows, we get the same output but in a different order: 21 in both COUNT fields, and BIT for the currency.

Nothing in your SELECT statement will do any kind of filtering. It’s a list of the fields we want to see.

Refining the selection in your example is done by the WHERE clause, as you’re specifying that you only want to count rows where the currency field is BIT.

1 Like

Thank you @thepitycoder for your sincere help! That was crystal clear! :smiley: :+1:

1 Like