Skip to content
Advertisement

count() results without using group by

I am attempting something very similar to last example (Using GROUP BY) on this page: https://thecodedeveloper.com/mysql-count-function/

Referring to the following table of data:

The following query:

Will produce the following output:

Except I want to see the number of employees in each department as well as every user in the table.

So I want the output to look like this:

I have managed to achieve what I want using a second query to test every result from the first query but it is extremely slow and I am convinced that there is a faster way to do it in a single query.

Advertisement

Answer

That’s a window count. In MySQL 8.0:

In earlier versions, an alternative uses a correlated subquery:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement