↓ Skip to main content
  1. Courses/
  2. SQL/

Group By, Order By

·547 words·3 mins· loading · loading ·
Russell Chubb
Author
Russell Chubb
Working at the intersection of Technology and Art.
Learn SQL - This article is part of a series.
Part 2: This Article

Video 📹
#

GROUP BY
#

The GROUP BY statement is used to group rows that have the same values into summary rows, like “Find the number of customers in each country”.

Note

The GROUP BY statement is almost always used in conjunction with aggregate functions, like COUNT(), MAX(), MIN(), SUM(), AVG(), to perform calculations on each group.

GROUP BY Syntax
#

SELECT column1, aggregate_function(column2), column3, ...
FROM table_name
WHERE condition
GROUP BY column1, column3
ORDER BY column_name;

Demo Database
#

Below is a selection from a made-up Customers table (from Russell’s Surf Store Database).

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Coastal Surf Co.Oscar Macdonald14 Harbour RoadBrightonBN1 4QFUK
2Alpine Outdoor GearKian Knight27 BergstrasseMunich80331Germany
3Pacific Wave SuppliesJock Adams82 Ocean AvenueSydneyNSW 2000Australia
4North Shore AdventuresLiam Barnes51 Beach RoadVancouverV6K 2G2Canada
5Atlantic SurfwearJake Faville9 Seaview TerraceCorkT12 X2F5Ireland

SQL GROUP BY Examples
#

The following SQL returns the number of customers in each country:

-- Example
SELECT Country, COUNT(CustomerID) AS [Number of Customers]
FROM Customers
GROUP BY Country;

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

-- Example
SELECT Country, COUNT(CustomerID) AS [Number of Customers]
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

ORDER BY
#

The ORDER BY keyword is used to sort the result set in ascending or descending order. By default, it sorts in ascending order (ASC).

-- Example:
-- Sort the products from lowest to highest price:
SELECT *
FROM Products
ORDER BY Price;

Order By Syntax
#

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Example Database
#

Below is a made-up selection from a Products table (from Russell’s Surf Store Database) used in the examples below:

ProductIDProductNameSupplierIDCategoryIDUnitPrice
16’ Fish Surfboard111 board449.99
27’ Funboard111 board599.99
33/2mm Wetsuit221 wetsuit129.99
4Reef Surf Wax2312 bars24.99
5Leash 7ft341 leash34.99

Order Descending
#

To sort the records in descending order, use the DESC keyword.

-- Example:
-- Sort the products from highest to lowest price:
SELECT * 
FROM Products
ORDER BY Price DESC;

Order Alphabetically
#

For string values, the ORDER BY keyword sorts the values in the column alphabetically:

-- Example
-- Sort the ProductName column alphabetically:
SELECT *
FROM Products
ORDER BY ProductName;

Alphabetically DESC
#

To sort text values in descending order, use the DESC keyword:

-- Example
-- Sort the ProductName column in reverse alphabetical order:
SELECT *
FROM Products
ORDER BY ProductName DESC;

The following SQL statement selects all customers from the Customers table and sorts them by Country, then by CustomerName.

This means it sorts by Country first, and if two records share the same country, it sorts them by CustomerName:

-- Example
SELECT * 
FROM Customers
ORDER BY Country, CustomerName;

Combine ASC and DESC
#

The following SQL statement selects all customers from the Customers table and sorts them in ascending order by Country and descending order by CustomerName:

-- Example
SELECT * 
FROM Customers
ORDER BY Country ASC, CustomerName DESC;
Learn SQL - This article is part of a series.
Part 2: This Article

Related

My Gym Split

512 words·3 mins· loading · loading
Want to learn how I’ve grown my mid-physique? - Check out this post…

Classes

562 words·3 mins· loading · loading
Classes in Python

About

2115 words·10 mins· loading · loading