DATABASE LAB
Lab2: SQL Functions
Ruba Sultan
SQL FUNCTIONS
Functions are a very powerful feature of SQL can
be used to do the following:
Perform calculations on data
Modify individual data items
Manipulate output for groups of rows
Format dates and numbers for display
Convert column data types
2
TWO TYPES OF SQL FUNCTIONS2
There are two distinct types of SQL Functions
Single Row Function (SRF)
Multiple Row Function (MRF)
2 Introduction to Oracle: SQL and PL/SQL P3-4. Neena Kochhar, Ellen Gravina and Priya Nathan, July 1999
SINGLE ROW FUNCTION
Single Row Function Categories3
Character Functions
Number Functions
Date Functions
Conversion Functions
General Functions
3 Introduction to Oracle: SQL and PL/SQL P3-6 Neena Kochhar, Ellen Gravina and Priya Nathan, July 1999
CHARACTER FUNCTIONS4
4 Introduction to Oracle: SQL and PL/SQL P3-7 Neena Kochhar, Ellen Gravina and Priya Nathan, July 1999
CHARACTER FUNCTIONS
Character Functions
LOWER (column | expression)
UPPER (column | expression)
INITCAP (column | expression)
CONCAT(column1|expression1,column2|expression2)
SUBSTR (column , m ,[n])
LENGTH (column | expression)
INSTR (column | expression , m)
6
LPAD (column , n , ‘string’)
QUESTIONS
Question1
Write a query to display all employee’s names and
their jobs.
Note: format must be as the following example
Blake is a manager
Question2
Write a query to display first name and three letters
of last name for all employees in department 90.
7
QUESTIONS
Question3
Write a query to display last three letters of
employee’s names.
Question4
Write a query to display all employee’s information
whose their names contain 4 letters.
8
NUMBER FUNCTIONS
Number Functions
ROUND (column | expression , n)
TRUNC (column | expression , n)
MOD (m , n)
9
EXAMPLES
SELECT
ROUND(65.723,2) , ROUND(65.723),
ROUND(65.723,-1), ROUND(65.723,-2)
FROM dual;
SELECT
TRUNC(65.723,2) , TRUNC(65.723),
TRUNC(65.723,-1), TRUNC(65.723,-2)
FROM dual;
10
DUAL TABLE
DUAL table owned by the user SYSTEM and can be
accessed by all users5.
It contains one column DUMMY and one row with the
value X5.
The DUAL table is useful when you want to return a
value once only5.
It can be used to test function on expressions.
It can be used to display current date using
SYSDATE.
11
5 Introduction to Oracle: SQL and PL/SQL P3-17 Neena Kochhar, Ellen Gravina and Priya Nathan, July 1999
ARITHMETIC WITH DATES6
Add or subtract a number to or from a date for a
resultant date value.
Subtract two dates to find the number of days
between those dates.
Add hours to a date by dividing the number of hours
by 24.
Operation Result Description
date + number Date Adds a number of days to a date
date - number Date Subtracts a number of days from a date
date - date Number of days Subtracts one date from another
date + number/24 Date Adds a number of hours to a12date
QUESTIONS
Question5
Write a query to display employee's name,
numbers and the number of weeks employed for
all employees who earns more than $5750.
13
DATE FUNCTIONS
Date Functions
MONTHS_BETWEEN (date1,date2)
ADD_MONTHS (date ,n)
NEXT_DAY( date, ‘char’)
LAST_DAY (date)
The default date format DD-MON-YY7
SYSDATE is a function returning date and time.
14
QUESTIONS
Question6
Write a query to display the date of the first FRIDAY.
Question7
Write a query to display your age in months.
Question8
Write a query to display last date of the current
month.
15
GENERAL FUNCTIONS
NVL Function
U sed to converts NULL into actual values.
NVL (column | expression , expression)
Question9
Write a query to display employee’s first names and
their annual income.
Note: For employees who have unknown commission
consider it to be 0.
16
GENERAL FUNCTIONS (CONT.)
DECODE Function
It has similar capability as CASE or IF ELSE statements.
DECODE syntax
DECODE (column | expression ,
search1 , result1,
search2 , result2,….,
default)
17
GENERAL FUNCTIONS (CONT.)
Question10
Write a query to display all employee’s names, their
salaries, and situation.
Note: situation of employee known form salary value.
Salary Situation
800 Low
3000 Moderate
5000 High
18
Otherwise Unknown
QUESTIONS
Question11
List ten functions other than listed in the
slides. Gives an example of each one.
19
JOIN
Join types
Equijoin
Non – Equijoin
Outer Join
Self Join
To join n tables together you need a minimum of (n-1)
join conditions.
Are used to obtain data from more than one table.
If the same column appears in more than one table, the
column name must be prefixed with the table name.
20
CARTESIAN PRODUCT9
Cartesian product formed when
Join condition is omitted
Join condition is invalid
To avoid a Cartesian product, always include a valid
join condition in a WHERE clause.
21
EQUIJOIN
Also called simple joins or inner joins.
SELECT
e.first_name,d.department_id,d.department_name
FROM employees e, deptartments d
WHERE e.department_id=d.department_id;
22
EQUIJOIN
Notes:
• Use table prefixes to qualify column names that
are in multiple tables.
• improve performance by using table prefixes.
• distinguish columns that have identical names
but reside in different tables by using column
aliases.
23
QUESTIONS
Question12
Write a query to display all employee’s first
names, their department’s names and their
job title for all employees in department 30
and 80.
24
OUTER JOIN
SELECT table1.column,table2.column
FROM table1 , table2
WHERE table1.column = table2.column(+)
Deficiency of Data
Question13
Write a query to display employee’s first
names, their last names, departments
numbers, and names of their departments.
Note: 25
Include departments that have no employees
SELF JOIN
This type occurs when join occurred to the table itself.
Question13
Write a query to display employee’s numbers,
their names, their managers numbers and
their managers names.
26