SQLite GROUP BY Statement
The GROUP BY Statement
GROUP BY puts the rows that have the same values into groups. It is used with aggregate functions, like COUNT(), MAX(), MIN(), SUM() and AVG(), to calculate one value for each group.
This example counts the customers in each country:
GROUP BY With ORDER BY
Example
Count the customers in each country, sorted from the most to the fewest customers:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
Try it Yourself »
GROUP BY With JOIN
This example counts the orders that each shipper has delivered:
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_CONCAT()
GROUP_CONCAT() joins the values in each group into one text. This example lists the cities in each country: