SQL group by - can it be this simple?

1647 views sql
7

can someone please check if this is correct?

enter image description here

Not sure if my answer to Q6 is correct, I am not sure if the group_by I am using is right or not, the rest I think is ok Thanks

answered question

1 Answer

11

You are close. You need to:

  • Use COUNT() instead of SUM().
  • There's no need to use the HAVING clause.
  • Optionally, you can add aliases to the columns, so they become easier to read.

Your query should look like:

select a.author_id, count(*) as titles, sum(b.quantity_ordered) as units
from a join b on a.book_id = b.book_id
group by a.author_id

posted this

Have an answer?

JD

Please login first before posting an answer.