Which aggregate function can be used for returning the total number of employees?

The COUNT operator is usually used in combination with a GROUP BY clause. It is one of the SQL “aggregate” functions, which include AVG (average) and SUM.

This function will count the number of rows and return that count as a column in the result set.

Here are examples of what you would use COUNT for:

  • Counting all rows in a table (no group by required)
  • Counting the totals of subsets of data (requires a Group By section of the statement)

For reference, here is the current data for all the rows in our example student database.

select studentID, FullName, programOfStudy, sat_score from student; -- all records with fields of interest

This SQL statement provides a count of all rows. Note that you can give the resulting COUNT column a name using “AS”.

select count(*) AS studentCount from student; -- count of all records

Here we get a count of students in each field of study.

 select studentID, FullName, count(*) AS studentCount from the student table with a group by programOfStudy;

Here we get a count of students with the same SAT scores.

select studentID, FullName, count(*) AS studentCount from the student table with a group by sat_score;

Here is an example using the campaign funds table. This is a sum total of the dollars in each transaction and the number of contributions for each political party during the 2016 US Presidential Campaign.

select Specific_Party, Election_Year, format(sum(Total_$),2) AS contribution$Total, count(*) AS numberOfContributions 
from combined_party_data
group by Specific_Party,Election_Year
having Election_Year = 2016;

As with all of these things there is much more to it, so please see the manual for your database manager and have fun trying different tests yourself.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


If this article was helpful, tweet it.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

SQL allows the user to store more than 30 types of data in as many columns as required, so sometimes, it becomes difficult to find similar data in these columns. Group By in SQL helps us club together identical rows present in the columns of a table. This is an essential statement in SQL as it provides us with a neat dataset by letting us summarize important data like sales, cost, and salary.

What Is Group By in SQL?

The Group By statement is used to group together any rows of a column with the same value stored in them, based on a function specified in the statement. Generally, these functions are one of the aggregate functions such as MAX() and SUM(). This statement is used with the SELECT command in SQL.

The SQL Group By statement uses the split-apply-combine strategy. 

  • Split: The different groups are split with their values.
  • Apply: The aggregate function is applied to the values of these groups.
  • Combine: The values are combined in a single row.

Which aggregate function can be used for returning the total number of employees?

The SUM() aggregate function, which results in the arithmetic sum of the rows’ values, has been applied to the groups in the above illustration. 

To better understand how SQL Order By is used, let’s get an insight into this statement's syntax.

Business Analyst Master's Program

Gain expertise in Business analytics toolsExplore Program

Which aggregate function can be used for returning the total number of employees?

The Syntax for Using Group By in SQL

SELECT column_1, function_name(column_2)

FROM table_name

WHERE [condition]

GROUP BY column_name

ORDER BY column_name;

  • The columns to be retrieved are specified in the SELECT statement and separated by commas. Any of the aggregate functions can be used on one or more than one of the columns being retrieved.
  • The table being used is specified in the table_name parameter of the FROM statement.
  • There is an optional WHERE clause, which can be used to specify any condition according to which the rows are to be selected.

NOTE: The WHERE clause always precedes the ORDER BY statement.

  • The GROUP BY statement lets the database system know that we wish to group the same value rows of the columns specified in this statement's column_names parameter.
  • The optional ORDER BY statement is used to sort the resulting table in ascending order based on the column specified in this statement's column_name parameter.

NOTE: The ORDER BY statement always appears after the GROUP BY statement and is applied to the groups of rows formed.

Aggregate functions are one of the most vital parts of the SQL Group By statement, so let’s see what these are.

Aggregate Functions

The five aggregate functions that we can use with the SQL Order By statement are:

  • AVG(): Calculates the average of the set of values.
  • COUNT(): Returns the count of rows.
  • SUM(): Calculates the arithmetic sum of the set of numeric values.
  • MAX(): From a group of values, returns the maximum value.
  • MIN(): From a group of values, returns the minimum value.

Let’s apply the concepts stated above to the columns of a table to get an even clearer picture of the Group By statement.

Using Group By on a Single Column

Let’s take a sample “Employee_dept” table:

Which aggregate function can be used for returning the total number of employees?

  • In this table, to find the average salary for each department’s employees, we’ll use the following query:

Which aggregate function can be used for returning the total number of employees?

We have used the AVG() aggregate function for this purpose.

This will result in the following table:

Which aggregate function can be used for returning the total number of employees?

There was no ORDER BY clause, so the table has no definite order of rows.

  • Let’s see what happens when we use the ORDER BY clause.

To find the number of employees from each city, this is what is required:

Which aggregate function can be used for returning the total number of employees?

And it will result in the following table:

Which aggregate function can be used for returning the total number of employees?

As we can see, the groups have been ordered in ascending order, according to the number of employees from each city.

  • The WHERE clause can also be used in the query, and it precedes the GROUP BY statement. Let’s see an example of this.

To find the maximum salary for each department with “Dept_ID” greater than 1003:

Which aggregate function can be used for returning the total number of employees?

The WHERE clause will be applied to the rows before they are grouped.

The above query will result in the following:

Which aggregate function can be used for returning the total number of employees?

  • Let’s see what happens when we use an aggregate function without the GROUP BY statement.

To find the total salary of all the employees:

Which aggregate function can be used for returning the total number of employees?

As we can see, the count function on “Dept_ID” returns the total number of records in the table, and the sum function on “Salary” returns the arithmetic sum of all the employees’ salaries.

The SQL Group By statement can be applied to multiple columns of a table in a single query. Let's see how this is done.

Introduction To Data Analytics (FREE)

Mastery to Data Analytics Basics is a Click Away!Start Learning

Which aggregate function can be used for returning the total number of employees?

Using Group By on Multiple Columns

In the “Employee_dept” table, to find the number of employees from each city in all the  departments, we’ll use the following query:

Which aggregate function can be used for returning the total number of employees?

As we can see, the GROUP BY is applied to two columns. 

This will result in the following table:

Which aggregate function can be used for returning the total number of employees?

As we can see, each city and department combination is represented in the table, and the number of employees for each of these combinations has been calculated.

Using the Having Clause With Group By

The WHERE clause cannot be used to filter the grouped rows based on a condition. We have the HAVING clause for this purpose. The syntax for using this clause is:

SELECT column_1, function(column_2)

FROM table_name

WHERE [condition]

GROUP BY column_names

HAVING [condition]

ORDER BY column_names;

The HAVING clause always precedes the ORDER BY statement in the query.

In the “Employee_dept” table, we'll use the following query to find the cities with multiple employees.

Which aggregate function can be used for returning the total number of employees?

As we can see, a condition has been applied to the group of cities formed by the GROUP BY statement. This will result in the following:

Which aggregate function can be used for returning the total number of employees?

Only the cities with multiple employees are returned.

With this, we come to the end of this article about the popular Group By statement.

Gain expertise in the latest Business analytics tools and techniques with the Post Graduate Program in Business Analysis. Enroll now!

Next Steps

Data aggregation or summarization is one of the most important tasks and skills for anyone who manages data. From profit and loss statements to creating a perfect visualization of data, SQL Group By is the tool that allows us to do these kinds of tasks efficiently.

Now that you know how to aggregate and summarize data, it is time for you to start querying, manipulating, and visualizing all kinds of data to move forward in your journey to become an expert in SQL. If you liked this article and want to get certified, check out our Business Analyst Master’s Program as it will help you learn the A-Z of SQL as well.

Do you have any questions for us? Please mention them in the comment section of our “How to Aggregate Data Using Group By in SQL” article, and we’ll have our experts in the field answer them for you right away!

Find our Business Analyst Online Bootcamp in top cities:

NameDatePlaceBusiness AnalystClass starts on 10th Dec 2022,
Weekend batchYour CityView DetailsBusiness Analyst Course in SingaporeClass starts on 17th Dec 2022,
Weekend batchSingaporeView DetailsBusiness AnalystClass starts on 3rd Jan 2023,
Weekdays batchYour CityView Details

About the Author

Which aggregate function can be used for returning the total number of employees?
Ravikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

Which aggregate function would return a value with the total number of rows?

The COUNT() aggregate function returns the number of rows in a group, including rows with NULL values.

Which aggregate function is used to give the number?

The COUNT() function returns the number of rows in a database table.

Which aggregate function returns the total SUM of a numeric column?

The SUM() function returns the total sum of a numeric column.

What aggregate function is used to return the number of records in a group?

Using the COUNT function, we can query the database to return the total number of rows in a table. Aggregate functions are used only after the SELECT clause.