SQL GROUP BY Statement

## SQL Basic

SQL HOME SQL Intro SQL Syntax SQL SELECT SQL SELECT DISTINCT SQL WHERE SQL AND & OR SQL ORDER BY SQL INSERT INTO SQL UPDATE SQL DELETE SQL Injection

## SQL Advanced

SQL SELECT TOP SQL LIKE SQL Wildcards SQL IN SQL BETWEEN SQL Aliases SQL Joins SQL INNER JOIN SQL LEFT JOIN SQL RIGHT JOIN SQL FULL JOIN SQL UNION SQL SELECT INTO SQL INSERT INTO SELECT SQL CREATE DB SQL CREATE TABLE SQL Constraints SQL NOT NULL SQL UNIQUE SQL PRIMARY KEY SQL FOREIGN KEY SQL CHECK SQL DEFAULT SQL CREATE INDEX SQL DROP SQL ALTER SQL Auto Increment SQL Views SQL Dates SQL NULL Values SQL NULL Functions SQL General Data Types SQL DB Data Types

## SQL Functions

SQL Functions SQL AVG() SQL COUNT() SQL FIRST() SQL LAST() SQL MAX() SQL MIN() SQL SUM() SQL GROUP BY SQL HAVING SQL UCASE() SQL LCASE() SQL MID() SQL LEN() SQL ROUND() SQL NOW() SQL FORMAT()
SQL Quick Ref SQL Hosting SQL Summary

## SQL Quiz

SQL Quiz

# SQL GROUP BY Statement

« Previous
Next Chapter »

Aggregate functions often need an added GROUP BY statement.

## The GROUP BY Statement

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

### SQL GROUP BY Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

## Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Orders" table:

OrderID | CustomerID | EmployeeID | OrderDate | ShipperID
--- | --- | --- | --- | ---
10248 | 90 | 5 | 1996-07-04 | 3
10249 | 81 | 6 | 1996-07-05 | 1
10250 | 34 | 4 | 1996-07-08 | 2

And a selection from the "Shippers" table:

ShipperID | ShipperName | Phone
--- | --- | ---
1 | Speedy Express | (503) 555-9831
2 | United Package | (503) 555-3199
3 | Federal Shipping | (503) 555-9931

And a selection from the "Employees" table:

EmployeeID | LastName | FirstName | BirthDate | Photo | Notes
--- | --- | --- | --- | --- | ---
1 | Davolio | Nancy | 1968-12-08 | EmpID1.pic | Education includes a BA....
2 | Fuller | Andrew | 1952-02-19 | EmpID2.pic | Andrew received his BTS....
3 | Leverling | Janet | 1963-08-30 | EmpID3.pic | Janet has a BS degree....

## SQL GROUP BY Example

Now we want to find the number of orders sent by each shipper.

The following SQL statement counts as orders grouped by shippers:

## Example

SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;
Try it yourself »

## GROUP BY More Than One Column

We can also use the GROUP BY statement on more than one column, like this:

## Example

SELECT Shippers.ShipperName, Employees.LastName,
COUNT(Orders.OrderID) AS NumberOfOrders
FROM ((Orders
INNER JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID)
INNER JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID)
GROUP BY ShipperName,LastName;
Try it yourself »
« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE