Sign up and we'll send you the best freelance opportunities straight to
your inbox.
We're building the largest self-service freelancing marketplace for people like you.
Problem: List the number of customers in each country.
SELECT Country, COUNT(Id) AS Customers
FROM Customer
GROUP BY Country
Result: 21 records.
Country
Customers
Argentina
3
Austria
2
Belgium
2
Brazil
9
Canada
3
GROUP BY with COUNT, ORDER BY
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List the number of customers in each country, sorted high to low.
SELECT Country, COUNT(Id) AS Customers
FROM Customer
GROUP BY Country
ORDER BY COUNT(Id) DESC
Result: 21 records.
Country
Customers
USA
13
France
11
Germany
11
Brazil
9
UK
7
GROUP BY with SUM, ORDER BY
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
Problem: List the total order amount for each customer, sorted high to low.
SELECT C.FirstName, C.LastName,
SUM(O.TotalAmount) AS Total
FROM [Order] O
JOIN Customer C ON O.CustomerId = C.Id
GROUP BY C.FirstName, C.LastName
ORDER BY SUM(O.TotalAmount) DESC
Sign up and we'll send you the best freelance opportunities straight to
your inbox.
We're building the largest self-service freelancing marketplace for people like you.