Search code examples
sqlselectcounth2min

SQL: select count with min rows in table


I have a table:

TABLE employee (
    ID               bigint,
    name             varchar,
    department bigint
);

I would like to find a department that has minimal employees. (Count of rows in this table) I believe this would require a HAVING statement with a nested sub-query, any help would be much appreciated. I am using H2 database.


Solution

  • You could group by department and get the count of users in each department, order by the count and select top 1?

    SELECT TOP 1
    [department],
    COUNT(*) AS [NoOfEmployees]
    FROM [employee]
    GROUP BY [department]
    ORDER BY COUNT(*) ASC