4.1 Sorting Data: Function Output

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Lab 4

Learning objectives:
 Sort the data in the resulting query
 Apply SQL aggregate functions
 Apply GROUP BY clause

4.1 Sorting Data

ORDER BY Clause
Sorts the result-set in ascending or descending order.
Default is ascending order, use the DESC keyword otherwise.
Listed last in the select command sequence.
Lists null columns first (Depends on DBMS)

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

Show contact name and country from the "Customers" table, sorted by the "Country" column:
SELECT ContactName, Country FROM Customers
ORDER BY Country;

Show contact name and country from the "Customers" table in DESCENDING order by the
"Country" column:
SELECT ContactName, Country FROM Customers
ORDER BY Country DESC;

ORDER BY Several Columns


Display contact name, city, and country from the "Customers" table, sorted by the "Country" and the
"City" column. This means that it orders by Country, but if some rows have the same Country, it
orders them by City:
SELECT ContactName, Country, City FROM Customers
ORDER BY Country, City;

4.2 Aggregate Functions


SQL can perform mathematical summaries through the use of aggregate (or group) functions.
Aggregate functions return results based on groups of rows.

Function Output
MIN The minimum attribute value encountered in a given column
MAX The maximum attribute value encountered in a given column
COUNT The number of rows containing non-null values
AVG The arithmetic mean (average) for a specified column
SUM The sum of all values for a given column
Find the minimum price among all products from the “Products” table.
SELECT MIN(UnitPrice) FROM Products;

Find the maximum price among all products from the “Products” table.
SELECT MAX(UnitPrice) FROM Products;

Find the number of all products from the “Products” table.


SELECT COUNT(ProductID) FROM Products;

Find the average price of all products from the “Products” table.
SELECT AVG(UnitPrice) FROM Products;

Find the total price of all products from the “Products” table.
SELECT SUM(UnitPrice) FROM Products;

Find the average price of all products from the “Products” table without using AVG function.
SELECT (SUM(UnitPrice)/COUNT(ProductID)) from products;

4.3 GROUP BY Clause


Groups rows that have the same values into summary rows, like "find the number of customers
in each country".
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);

List the number of customers in each country.


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

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;

You might also like