Lab 2 SQL
Lab 2 SQL
Ruba Sultan
SQL FUNCTIONS
Functions are a very powerful feature of SQL can
be used to do the following:
2
TWO TYPES OF SQL FUNCTIONS2
2 Introduction to Oracle: SQL and PL/SQL P3-4. Neena Kochhar, Ellen Gravina and Priya Nathan, July 1999
SINGLE ROW FUNCTION
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
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
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.
13
DATE FUNCTIONS
Date Functions
MONTHS_BETWEEN (date1,date2)
ADD_MONTHS (date ,n)
NEXT_DAY( date, ‘char’)
LAST_DAY (date)
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.
16
GENERAL FUNCTIONS (CONT.)
DECODE Function
It has similar capability as CASE or IF ELSE statements.
DECODE syntax
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
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