Group and Aggregation Introduction
Group and Aggregation Introduction
Functions
SQL provides powerful capabilities for
grouping query results and calculating
aggregates to analyze and summarize data.
GROUP By clause
The GROUP BY clause groups a result set by one or more
columns. For each distinct value in those columns it aggregates
the rows into a single summary row.
This groups all rows by the "city" column. For each unique city, it outputs a
single row with the city name and count of rows for that city.
We can group by multiple columns to create
subgroupings - like customers per city per
country:
To get the next 10 results, we can simply add an `OFFSET` with the
number of rows to skip.
SELECT customerName,
CONCAT(UCASE(contactFirstName), " ",
UCASE(contactLastName)) AS contact, phone
FROM customers WHERE country="USA" ORDER
BY customerName;
SUBSTRING and LCASE
**QUESTION**: Display a paginated list of
customers (sorted by customer name), with a
country code column. The country is simply
the first 3 letters in the country name, in lower
case.
select customerName,
LCASE(SUBSTRING(country, 1, 3)) AS
countryCode FROM customers ORDER BY
customerName;
**QUROUNDESTION**: Display the list of the 5
most expensive products in the "Motorcycles"
product line with their price (MSRP) rounded to
dollars.