DEV Community

LogicWiz Admin
LogicWiz Admin

Posted on

An AI explained SQL GROUP BY in four lines. One of them is wrong.

We asked Aria, our AI, how filtering works with GROUP BY. She answered in four lines and sounded completely sure about all of them:

1. WHERE filters rows before they are grouped.
2. HAVING filters the groups after GROUP BY runs.
3. COUNT(*) counts every row in a group, NULLs included.
4. So WHERE COUNT(*) > 2 keeps only the groups with more than two rows.
Enter fullscreen mode Exit fullscreen mode

One line is wrong. Read them again before you scroll.

.

.

.

.

.

It's line 4. WHERE runs before the groups exist, so it can't use an aggregate like COUNT(). Most databases throw an error on that query. The filter you want is `HAVING COUNT() > 2`.

The other three are right. People often mix up line 3 with COUNT(column), which skips NULLs.

We built a free game out of this. Aria answers a coding question in 5 or 6 lines and you tap the one that's wrong, then pick why. Five rounds take about five minutes on a phone, across Python, SQL, frontend, backend, data, cloud, DSA and AI.

Our October event closes Monday 19 Oct at 11:59 pm IST, and the top 10 get LogicWiz goodies.

Play it here: https://arena.logicwiz.ai/club/BJSRBK?utm_source=devto

Which line did you pick first?

Top comments (0)