Lab 04
Lab 04
o Aggregate Functions
o GROUP BY Statement
o HAVING clause
o Regular Expressions
Aggregate Functions
An aggregate function allows you to perform a calculation on a set of values to return a single
scalar value. We often use aggregate functions with the GROUP BY and HAVING clauses of
the SELECT statement.
WHERE clause comes before GROUP BY clause.
HAVING clause can come after GROUP BY clause.
Alias names cannot be used in GROUP BY clause.
GROUP BY columns are not necessarily be in the SELECT clause
GROUP BY clause can be applied on more than one columns
The following are the most commonly used SQL aggregate functions:
Notice that all aggregate functions above ignore NULL values except for the COUNT
function.
SQL aggregate functions syntax: To call an aggregate function, you use the following
syntax:
Second, put DISTINCT or ALL modifier followed by an expression inside parentheses. If you
explicitly use the DISTINCT modifier, the aggregate function ignores duplicate values and
only consider the unique values. If you use the ALL modifier, the aggregate function uses all
values for calculation or evaluation. The ALL modifier is used by default if you do not specify
any modifier explicitly.
GROUP BY Statement
The GROUP BY statement group rows that have the same values into summary rows, like
"find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN,
SUM, AVG) to group the result-set by one or more columns.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
DISTINCT vs GROUP BY
Distinct is used to find unique/distinct records where as a group by is used to group a selected
set of rows into summary rows by one or more columns or an expression.
The group by can also be used to find distinct values as shown in below query.
SELECT DEPARTMENT_ID
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID;
OR
SELECT DISTINCT DEPARTMENT_ID
FROM EMPLOYEES ;
HAVING clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
The above SQL statement lists the number of customers in each country, sorted high to low
(Only include countries with more than 5 customers):
Regular Expressions
Regular Expressions help search data matching complex criteria. We looked at wildcards in
the previous labs. If you have worked with wildcards before, you may be asking why learn
regular expressions when you can get similar results using the wildcards. Because, compared to
wildcards, regular expressions allow us to search data matching even more complex criterion.
Syntax:
SELECT columns
FROM table
WHERE columnName
REGEXP 'pattern';
Example:
SELECT *
FROM employees
WHERE FIRST_NAME
REGEXP 'al';
The above query searches for all the employees’ first name that have the word “al” in them. It
does not matter whether the “al” is at the beginning, middle or end of the first name. As long as
it is contained in the name then it will be considered.
Let's suppose that we want to search employees whose first name start with a, b, c or d ,
followed by any number of other characters. We can use a regular expression together with the
metacharacters to achieve our desired results.
SELECT *
FROM employees
WHERE FIRST_NAME
REGEXP '^[a, b, c, d]';