SQL MAX() with COUNT()
MAX() with Count function
Sample table: orders
In this part, you will see the usage of SQL COUNT() along with the SQL MAX().
Example:
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) -- Selecting the maximum value of the column 'mycount'
FROM ( -- Subquery: Creating a derived table
SELECT agent_code, COUNT(agent_code) AS mycount -- Selecting 'agent_code' and its count, aliased as 'mycount'
FROM orders -- From the 'orders' table
GROUP BY agent_code -- Grouping the results by 'agent_code'
);
Explanation:
- SELECT MAX(mycount): This line selects the maximum value of the column mycount from the result set returned by the subquery.
- (SELECT agent_code, COUNT(agent_code) AS mycount FROM orders GROUP BY agent_code): This is a subquery that generates a derived table. It selects the agent_code column and counts the occurrences of each agent_code in the orders table. The COUNT(agent_code) function is used to count the occurrences of each agent_code. The results are grouped by agent_code.
- SELECT agent_code, COUNT(agent_code) AS mycount: This line within the subquery selects the agent_code column and counts the occurrences of each agent_code in the orders table. The COUNT(agent_code) function is used to count the occurrences of each agent_code, and it is aliased as mycount.
- FROM orders: This specifies the table from which the subquery is selecting data, which is the orders table.
- GROUP BY agent_code: This line groups the results of the subquery by the agent_code column. This is necessary because we're using an aggregate function (COUNT) in conjunction with a non-aggregated column (agent_code). Grouping allows us to count the occurrences of each agent_code separately.
Output:
MAX(MYCOUNT) ------------ 7
Pictorial Presentation :
SQL MAX() and COUNT() with HAVING
Sample table: orders
To get data of 'agent_code', and number of agents for each group of 'agent_code' from the orders table with the following conditions -
'agent_code' for a group will be equal to the result of an outer query [SELECT MAX(agent_code).......] with following condition -
the outer query produce the maximum number of agents mentioned as
'mycount' from an inner query [SELECT agent_code,
COUNT(agent_code) mycount FROM orders GROUP BY agent_code]
with following condition -
the inner query produced the data 'agent_code' number of agents as column alias 'mycount' from the 'orders' table with the following condition -
'agent_code' should be in a group,
the following SQL statement can be used :
SELECT agent_code, COUNT(agent_code) -- Selecting 'agent_code' and its count
FROM orders -- From the 'orders' table
GROUP BY agent_code -- Grouping the results by 'agent_code'
HAVING COUNT(agent_code) = ( -- Applying a condition on the grouped counts
SELECT MAX(mycount) -- Selecting the maximum count from a subquery
FROM ( -- Subquery: Creating a derived table
SELECT agent_code, COUNT(agent_code) AS mycount -- Selecting 'agent_code' and its count, aliased as 'mycount'
FROM orders -- From the 'orders' table
GROUP BY agent_code -- Grouping the results by 'agent_code'
)
);
Explanation:
- SELECT agent_code, COUNT(agent_code): This line selects the agent_code column and counts the occurrences of each agent_code in the orders table.
- FROM orders: This specifies the table from which data is being selected, which is the orders table.
- GROUP BY agent_code: This line groups the results by the agent_code column. This is necessary because we're using an aggregate function (COUNT) in conjunction with a non-aggregated column (agent_code). Grouping allows us to count the occurrences of each agent_code separately.
- HAVING COUNT(agent_code) = (...): This line filters the grouped results based on a condition. It selects groups where the count of agent_code matches the result of the subquery.
- SELECT MAX(mycount): This subquery selects the maximum value of the column mycount.
- (SELECT agent_code, COUNT(agent_code) AS mycount ... ): This is a subquery that generates a derived table. It calculates the count of occurrences of each agent_code in the orders table and aliases it as mycount.
- SELECT agent_code, COUNT(agent_code) AS mycount: This line within the subquery selects the agent_code column and counts the occurrences of each agent_code in the orders table. The COUNT(agent_code) function is used to count the occurrences of each agent_code, and it is aliased as mycount.
- FROM orders: This specifies the table from which the subquery is selecting data, which is the orders table.
- GROUP BY agent_code: This line groups the results of the subquery by the agent_code column. This is necessary because we're using an aggregate function (COUNT) in conjunction with a non-aggregated column (agent_code). Grouping allows us to count the occurrences of each agent_code separately.
Output:
AGENT_CODE COUNT(AGENT_CODE) ---------- ----------------- A002 7
Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition
Here is a slide presentation of all aggregate functions.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: Max Date
Next: Min function
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics