Add function in SQL


#1

https://www.codecademy.com/en/courses/learn-sql/lessons/aggregate-functions/exercises/groupby
and
https://www.codecademy.com/en/courses/learn-sql/lessons/aggregate-functions/exercises/sum

We used the
SELECT price, COUNT(*) FROM fake_apps GROUP BY price;
function because we wanted to count all rows and wanted to group by price. So when we want to add just the values of the rows of the downloads column, why can't I do the following:
select * from fake_Apps
sum (downloads);

why does it have to be select Sum (downloads) from fake_apps; ?


#2

GROUP BY is basically a way of categorizing how the function, COUNT in this case, is being ran. If we ran COUNT(*) without GROUP BY would just return a count of all the rows. When we use GROUP BY price we can see a count of all the rows categorized by price. So all the free, 0.99, 1.99 etc...

SUM(downloads) is different from GROUP BY because SUM(downloads) is telling SQL what data to pull whereas GROUP BY is telling SQL how to sort it and pull it.

Hopefully that makes sense, if not I can try and explain it more.


#3

could you clarify what * is used for. Is it referring to the usage of all rows or all columns?
In https://www.codecademy.com/en/courses/learn-sql/lessons/manipulation/exercises/select,

it mentions that * is a special wildcard character that we have been using. It allows you to select every column in a table without having to name each one individually.

But in other instances * seems to refer to all rows.


#4

* is a wilecard meaning everything. If we run SELECT * FROM fake_apps we are saying grab every column from the fake_apps table. When we use COUNT(*) we are telling it to count every row. Context is everything, in a select statement we are choosing which columns to select data from. When we use aggregate functions such as COUNT and SUM we are dealing with rows.


#5

That makes a lot of sense. That disclaimer should be created inside the lesson. I think many may be confused by the same point.


#6

That's a good point! Unfortunately the SQL lessons are rather poorly written. It's kind of disappointing because so many of Codecademy's lesson are great.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.