Home > OS >  Trying to follow GROUP BY logic in SQL
Trying to follow GROUP BY logic in SQL

Time:03-23

I'm prepping for an SQL interview and was going over this guide.

The eventual code the author wrote was:

SELECT cust_id,
       first_name,
       sum(total_order_cost)
FROM customers
JOIN orders ON customers.id = orders.cust_id
GROUP BY cust_id,
         first_name

MY QUESTION: Why is first_name used in the GROUP_BY? If I wrote the code without first_name in the GROUP BY, I'm getting errors.

thanks in advance.

CodePudding user response:

Unaggregated columns in the SELECT (cust_id and first_name in this case) need to be listed in the GROUP BY. Even in cases like this one, where there's (presumably) only one first_name per cust_id, the DB engine still expects every column in the SELECT to either be in an aggregate function or in the GROUP BY.

  • Related