w3resource

SQL MIN() with COUNT()

MIN() with COUNT()

In this page, we are discussing the usage of SQL COUNT() function along with the SQL MIN() in a query to get a complex result.

Example:

Sample table: orders


To get minimum number of agents mentioned as 'mycount' which comes from a group of result set from a query [SELECT agent_code,COUNT(agent_code) mycount...] with the following condition -

 to produce data 'agent_code' and the number of agents as the column alias
 'mycount' into the result set from the 'orders' table with the following
condition -

'agent_code' should comes in a group,

the following SQL statement can be used :

SELECT MIN(mycount) 
FROM (SELECT agent_code,COUNT(agent_code) mycount 
FROM orders 
GROUP BY agent_code);

Output:

MIN(MYCOUNT)
------------
           1

All Aggregate Functions

SQL Aggregate Functions, slide presentation

See our Model Database

Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: Min group by, order by
Next: SQL Arithmetic function



Follow us on Facebook and Twitter for latest update.