Pattern Matching, Aggregated Functions, Aliases

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

SQL-3

Pattern Matching, Aggregated functions,


aliases

-Cherry Khosla
Pattern Matching
• The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
• There are two wildcards often used in conjunction with the LIKE operator:
• % The percent sign represents zero, one, or multiple characters
•_ The underscore represents a single character

• Syntax
SELECT column(s) FROM table_name WHERE column_name LIKE
pattern;
Exam ple
• select * from employee where first_name LIKE 'S%';
• select * from employee where first_name LIKE’%m%';
• select * from employee where first_name LIKE ‘S
%'
• select * from employee where first_name LIKE 'S_%'
Q uestion
• To find out the names of employees where name of employee starts
with a and ends with a and consists of 5 characters in total
A. select name from employee where name like 'a%a'
B. select name from employee where name like 'a*a'
C. select * from employee where name like 'a_ _ _a'
D. select name from employee where name like 'a_ _ _a'
Aggregate Functions
• Performing calculations on multiple rows
• Of a single column of a table
• And returning a single value.
• The ISO standard defines five (5) aggregate functions namely;
1) COUNT
2) SUM
3) AVG
4) MIN
5) MAX
W h y to use them?
• From a business perspective, different organization levels have
different information requirements. Top levels managers are usually
interested in knowing whole figures and not necessary the individual
details.
• Aggregate functions allow us to easily produce summarized data from
our database.
Aggregate function
Built-in Function Description
Count Returns the number of non-NULL values in a column over a set
of rows
Max Returns the maximum value of a column over a set of rows
Min Returns the minimum value of a column over a set of rows
Sum Sums the values in a column for a set of rows
Avg Returns the average value of a column over a set of rows

COUNT (*) is a special implementation of the COUNT function that returns the
count of all the rows in a specified table. COUNT (*) also considers Nulls and
duplicates.
Exam ple
• select avg(salary) from employee
• select max(salary) from employee
• select min(salary) from employee
• select sum(salary) from employee
• select count(salary) from employee
• Select count(*) from employee
Q uestion
• Which of the following includes null and duplicate values
A. Count(column name)
B. Count(*)
Aliases
• Alias is a temporary name given to the column or the table in a query,
for the user’s convenience in readability for minimizing the query
length.
• Applicable to column and table names
• Specified with a keyword 'AS'
Column Alias
• Column Alias renames a column heading in a Query.
• The AS keyword can be used between the column name and the Alias
name.

You might also like