SQL Programming
  • Introduction to SQL
  • Data Definition Language
    • DATA TYPES
    • CREATE DATABASE
    • DROP DATABASE
    • CREATE TABLE Statement
    • CONSTRAINTS
      • SQL PRIMARY KEY Constraint
      • SQL NOT NULL Constraint
      • SQL UNIQUE Constraint
      • SQL FOREIGN KEY Constraint
      • SQL DEFAULT Constraint
      • SQL CHECK Constraint
    • DROP & TRUNCATE TABLE Statement
    • ALTER TABLE Statement
    • AUTO INCREMENT Field
    • SQL SELECT INTO Statement
    • CREATE INDEX
    • DDL Practice Problems
  • Data Manipulation Language
    • INSERT INTO Statement
    • INSERT INTO SELECT Statement
    • DELETE Statement
    • UPDATE Statement
    • DML Practice Problems
  • SQL NULL Values
  • SQL Comments
  • Data Query Language
    • SQL SELECT DISTINCT Statement
    • SQL WHERE Clause
      • SQL And, Or, Not (Logical Operators)
      • SQL Wildcards
      • SQL LIKE Operator
      • SQL IN Operator
      • SQL BETWEEN Operator
      • SQL WHERE Exercises
    • SQL TOP CLAUSE
    • SQL Order By
    • SQL Server Scalar Functions (Non-aggregate)
      • SQL MIN() and MAX()
      • SQL COUNT(), AVG() and SUM() Functions
      • Functions Problems
    • SQL ISNULL Functions
    • SQL Aliases
    • SQL CASE Statement
    • SQL SET Operator
    • SQL GROUP BY Statement
    • SQL HAVING Clause
    • Analytical Function
    • SQL Joins
      • SQL INNER JOIN Keyword
      • SQL LEFT JOIN Keyword
      • SQL RIGHT JOIN Keyword
      • SQL FULL OUTER JOIN Keyword
      • SQL Self JOIN
      • SQL CROSS JOIN
    • SQL Subqueries
    • SQL EXISTS Operator
    • SQL ANY and ALL Operators
  • CREATE VIEWS
  • Common Table Expressions (CTE)
  • Data Control Language
  • SQL Stored Procedures for SQL Server
  • Practice Papers
  • SQL PROJECT
Powered by GitBook
On this page
  • The SQL GROUP BY Statement
  • Demo Database
  • SQL GROUP BY Examples
  • GROUP BY With JOIN Example

Was this helpful?

  1. Data Query Language

SQL GROUP BY Statement

PreviousSQL SET OperatorNextSQL HAVING Clause

Last updated 6 years ago

Was this helpful?

The SQL GROUP BY Statement

The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.

GROUP BY Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

SQL GROUP BY Examples

The following SQL statement lists the number of customers in each country

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY
Country;

The following SQL statement lists the number of customers in each country, sorted high to low:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

Demo Database

Below is a selection from the "Orders" table in the Northwind sample database

And a selection from the "Shippers" table:

GROUP BY With JOIN Example

The following SQL statement lists the number of orders sent by each shipper:

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