N
The Daily Insight

How do you use max and count together?

Author

Sarah Martinez

Updated on April 02, 2026

The solution is to use the first table as a subquery. We will create an additional query, an outer query, which uses the first table in its FROM clause. It will be able to use MAX() on the COUNT() result from the first table, thus circumventing the direct use of two layered aggregate functions.

How can I get maximum group count in SQL?

SQL – Displaying entries that are the max of a count?

  1. CREATE TABLE doctor( patient CHAR(13), docname CHAR(30) );
  2. SELECT MAX(count) FROM (SELECT COUNT(docname) FROM doctor GROUP BY docname) a;
  3. SELECT docname, COUNT(docname) FROM doctor GROUP BY name;

How use both count and Max in SQL?

To get one row with the highest count, you can use ORDER BY ct LIMIT 1 : SELECT c. yr, count(*) AS ct FROM actor a JOIN casting c ON c. actorid = a.id WHERE a.name = ‘John Travolta’ GROUP BY c.

Can I do max count in SQL?

No, we can’t use a MAX(COUNT(*) and we can not layer aggregate functions on top of one another in the same SELECT clause. In a subquery, the inner aggregate would have to be performed.

Can we use Max in having?

In the context of HAVING , MAX finds the max of each group. Only the latest entry in each group will satisfy date_updated=max(date_updated) . If there’s a tie for latest within a group, both will pass the HAVING filter, but GROUP BY means that only one will appear in the returned table.

Does Max need GROUP BY?

As per the error, use of an aggregate like Max requires a Group By clause if there are any non-aggregated columns in the select list (In your case, you are trying to find the MAX(Num) and then return the value(s) associated in the ID column).

Can I use having Without group by clause?

Having can be used without groupby clause,in aggregate function,in that case it behaves like where clause. groupby can be used without having clause with the select statement. 3. The having clause can contain aggregate functions.

How do I SELECT Max in SQL?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

How to use Max() and count() with having?

SQL MAX() and COUNT() with HAVING. the following SQL statement can be used : SELECT agent_code, COUNT(agent_code) FROM orders GROUP BY agent_code HAVING COUNT (agent_code)=( SELECT MAX(mycount) FROM ( SELECT agent_code, COUNT(agent_code) mycount FROM orders GROUP BY agent_code)); Output: Here is a slide presentation of all aggregate functions.

How to find the maximum value of a column over each group?

In this article we have discussed how SQL HAVING CLAUSE can be used along with the SQL MAX () to find the maximum value of a column over each group. The SQL HAVING CLAUSE is reserved for aggregate function. The usage of WHERE clause along with SQL MAX () have also described in this page.

How to get the maximum number of agents in a table?

To get the maximum number of agents as column alias ‘mycount’ from the ‘orders’ table with the following condition – 1. ‘agent_code’ should be in a group, the following SQL statement can be used : SELECT MAX (mycount) FROM (SELECT agent_code,COUNT(agent_code) mycount FROM orders GROUP BY agent_code); Sample table: orders

Why Max(count (*) is not supported?

For most databases, it needs a 2nd level sub-query as MAX ( COUNT (*) ) is not supported. It uses SQL extensions for a specific database. Also, using TOP / LIMIT of 1 will only give one row – what if there are two or more doctors with the same maximum number of patients?