# How does MySQL handles group by?

**URL:** <https://forum.kirupa.com/t/how-does-mysql-handles-group-by/659239>\
**Category:** Uncategorized\
**Created:** [June 10, 2023, 8:31am UTC](https://forum.kirupa.com/t/how-does-mysql-handles-group-by/659239 "2023-06-10T08:31:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 10, 2023, 8:31am UTC](https://forum.kirupa.com/t/how-does-mysql-handles-group-by/659239/1 "2023-06-10T08:31:26Z")

</div>

I’m on Windows 10, MySQL Workbench. I’m on MySQL 8.0.

Here’s the dataset. northwind.sql.

> <https://pastebin.com/bMgjXvfT>

Objective:

**Write a query to get the order ID, customer’s name, grand total of each order, and the name of the employee who handled each order. See below the expected first four rows.**

Output should look like this.

 ![](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/1/5/1550ceb949c6d7220e01d3047149793123ef0d7d.png)

This is the database schema diagram.

> **[Northwind-Sample-Database-Diagram.pdf](https://brucebauer.info/assets/ITEC3610/Northwind/Northwind-Sample-Database-Diagram.pdf)**
>
> 151.66 KB

It’s northwind.sql database.

This is my query.

```auto

SELECT

o.orderid,

c.contactname,

SUM(od.unitprice * od.quantity),

CONCAT(e.lastname, ' ', e.firstname) AS emp_name

FROM

orders o

INNER JOIN

customers c ON o.customerid = c.customerid

INNER JOIN

orderdetails od ON o.orderid = od.OrderID

INNER JOIN

employees e ON o.EmployeeID = e.EmployeeID

GROUP BY emp_name

ORDER BY orderid

LIMIT 4;

```

But it was producing an error.

Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘northwind.o.OrderID’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql\_mode=only\_full\_group\_by

But if I order by orderid alone, it’ll work, Why does it work? Should not it be producing the same error as above as not all non-aggregated query aren’t present in group by clause ?

What’s the computer science(Database concept) behind all this? Can anyone explain it?
