Lab 04: SQL Functions & Regular Expressions: CSC-252: Database Management System
Lab 04: SQL Functions & Regular Expressions: CSC-252: Database Management System
1: 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 column
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:
1
Department of Computer Science
CSC-252: Database Management System Lab 04: SQL Functions & Regular Expressions
First, specify an aggregate function that you want to use e.g., MIN, MAX, AVG, SUM or
COUNT.
2: 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 ;
3: 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)
2
Department of Computer Science
CSC-252: Database Management System Lab 04: SQL Functions & Regular Expressions
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):
3: 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 *
3
Department of Computer Science
CSC-252: Database Management System Lab 04: SQL Functions & Regular Expressions
FROM employees
WHERE FIRST_NAME
REGEXP '^[a, b, c, d]';
4
Department of Computer Science
CSC-252: Database Management System Lab 04: SQL Functions & Regular Expressions
[A-Z] The [A-Z] is used to match any SELECT * FROM `members` WHERE
upper case letter. `postal_address` REGEXP '[A-Z]'; will
give all the members that have postal
address containing any character from A
to Z. .For Example, Janet Jones with
membership number 1.
[a-z] The [a-z] is used to match any lower SELECT * FROM `members` WHERE
case letter `postal_address` REGEXP '[a-z]'; will give
all the members that have postal
addresses containing any character from
a to z. .For Example, Janet Jones with
membership number 1.
[0-9] The [0-9] is used to match any digit SELECT * FROM `members` WHERE
from 0 through to 9. `contact_number` REGEXP '[0-9]' will
give all the members have submitted
contact numbers containing characters
"[0-9]" .For Example, Robert Phil.
| The vertical bar (|) is used to isolate SELECT * FROM `movies` WHERE `title`
alternatives. REGEXP '^[cd]|^[u]'; gives all the movies
with the title starting with any of the
characters in "cd" or "u" .For Example,
Code Name Black, Daddy's Little Girl, Da
Vinci Code and Underworld - Awakening.
https://dev.mysql.com/doc/refman/5.6/en/regexp.html
Lab Task(s):
Exercise
5
Department of Computer Science
CSC-252: Database Management System Lab 04: SQL Functions & Regular Expressions
9. Write a query to display the number of employees with the same job.
10. Display the manager number and the salary of the lowest paid employee of that
manager. Exclude anyone whose manager is not known. Exclude any groups where
the minimum salary is 2000. Sort the output is descending order of the salary.
11. Display the total number of employees who have no commission.
12. Write a query to display FIRST_NAME, LASTNAME of all employees whose first name
small ‘t’.
6
Department of Computer Science
CSC-252: Database Management System Lab 04: SQL Functions & Regular Expressions