Search This Blog

SQL GROUP BY Clause

 

SQL: GROUP BY Clause

This SQL tutorial explains how to use the SQL GROUP BY clause with syntax and examples.

Description

The SQL GROUP BY clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns.

Syntax

The syntax for the GROUP BY clause in SQL is:

SELECT expression1, expression2, ... expression_n, 
       aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
[ORDER BY expression [ ASC | DESC ]];

Parameters or Arguments

expression1, expression2, ... expression_n
Expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY Clause at the end of the SQL statement.
aggregate_function
This is an aggregate function such as the SUM, COUNT, MIN, MAX, or AVG functions.
aggregate_expression
This is the column or expression that the aggregate_function will be used on.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to be selected.
ORDER BY expression
Optional. The expression used to sort the records in the result set. If more than one expression is provided, the values should be comma separated.
ASC
Optional. ASC sorts the result set in ascending order by expression. This is the default behavior, if no modifier is provider.
DESC
Optional. DESC sorts the result set in descending order by expression.

DDL/DML for Examples

If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!

Get DDL/DML

Example - Using GROUP BY with the SUM Function

Let's look at how to use the GROUP BY clause with the SUM function in SQL.

In this example, we have a table called employees with the following data:

employee_numberlast_namefirst_namesalarydept_id
1001SmithJohn62000500
1002AndersonJane57500500
1003EverestBrad71000501
1004HorvathJack42000501

Enter the following SQL statement:


SELECT dept_id, SUM(salary) AS total_salaries
FROM employees
GROUP BY dept_id;

There will be 2 records selected. These are the results that you should see:

dept_idtotal_salaries
500119500
501113000

In this example, we've used the SUM function to add up all of the salaries for each dept_id and we've aliased the results of the SUM function as total_salaries. Because the dept_id is not encapsulated in the SUM function, it must be listed in the GROUP BY clause.

Example - Using GROUP BY with the COUNT function

Let's look at how to use the GROUP BY clause with the COUNT function in SQL.

In this example, we have a table called products with the following data:

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

Enter the following SQL statement:


SELECT category_id, COUNT(*) AS total_products
FROM products
WHERE category_id IS NOT NULL
GROUP BY category_id
ORDER BY category_id;

There will be 3 records selected. These are the results that you should see:

category_idtotal_products
251
504
751

In this example, we've used the COUNT function to calculate the number of products for each category_id and we've aliased the results of the COUNT function as total_products. We've excluded any category_id values that are NULL by filtering them out in the WHERE clause. Because the category_id is not encapsulated in the COUNT function, it must be listed in the GROUP BY clause.

Example - Using GROUP BY with the MIN function

Let's next look at how to use the GROUP BY clause with the MIN function in SQL.

In this example, we will use the employees table again that is populated the following data:

employee_numberlast_namefirst_namesalarydept_id
1001SmithJohn62000500
1002AndersonJane57500500
1003EverestBrad71000501
1004HorvathJack42000501

Enter the following SQL statement:


SELECT dept_id, MIN(salary) AS lowest_salary
FROM employees
GROUP BY dept_id;

There will be 2 records selected. These are the results that you should see:

dept_idlowest_salary
50057500
50142000

In this example, we've used the MIN function to return the lowest salary for each dept_id and we've aliased the results of the MIN function as lowest_salary. Because the dept_id is not encapsulated in the MIN function, it must be listed in the GROUP BY clause.

Example - Using GROUP BY with the MAX function

Finally, let's look at how to use the GROUP BY clause with the MAX function.

Let's use the employees table again, but this time find the highest salary for each dept_id:

employee_numberlast_namefirst_namesalarydept_id
1001SmithJohn62000500
1002AndersonJane57500500
1003EverestBrad71000501
1004HorvathJack42000501

Enter the following SQL statement:


SELECT dept_id, MAX(salary) AS highest_salary
FROM employees
GROUP BY dept_id;

There will be 2 records selected. These are the results that you should see:

dept_idhighest_salary
50062000
50171000

In this example, we've used the MAX function to return the highest salary for each dept_id and we've aliased the results of the MAX function as highest_salary. The dept_id column must be listed in the GROUP BY clause because it is not encapsulated in the MAX function.

No comments:

Post a Comment

Using SQL*Plus to Unlock Accounts and Reset Passwords

  Using SQL*Plus to Unlock Accounts and Reset Passwords Use this SQL*Plus procedure to unlock and reset user account passwords. Log in as th...