Week2 153634
Week2 153634
Week2 153634
Phones
or
Change Profile to Silent
Mode
Lecture Plan
Functions, Features of Functions
Types of Functions
Single- Row Functions, Features of Single- Row
Functions
Types of Single- Row Functions
Displaying Data from multiple tables
Joins and its types
• are programs that take zero or more arguments and return a single
value as output.
• makes a simple query more powerful and is used to manipulate data
values.
• Aggregate/Multiple-Row Function:
These functions can manipulate groups of rows to produce one result
per group of rows.
Types of Functions
• Single-Row Function:
• An argument can be one of the following:
User supplied content
Variable value
Column name
Expression
Case-manipulation Functions:
This sub-category of function converts the case of the character string.
There are basically three functions which fall under this sub-category;
these are:
Character Functions
• used to manipulate the characters within the string
and as well as the strings itself.
RPAD(column This function returns the column| SELECT RPAD (‘HELLO’, 8, HELLO***
|expression, m, expression expanded in length to m ‘*’) FROM Dual;
‘string’) characters, using string to fill in spaces
as needed on the right of column|
expression.
TRIM(leading This function returns the trim_source SELECT TRIM(‘T’ FROM he Three
|trailing |both, with all leading | trailing | both ‘The Three Musketeers’) Musketeers
trim_character occurrences of characters in FROM Dual;
FROM trim_character removed. A NULL is
trim_source) returned if any of the three arguments
is NULL.
Function Purpose Example Result
ASCII(String) This function returns the ASCII SELECT ASCII(‘Hello’) FROM Dual;
decimal equivalent of the first
72
character in the String
CHR(m [ USING This function returns the character SELECT CHR(78) FROM Dual;
equivalent of the decimal (binary)
N
NCHAR_CS])
representation of the character. If
the optional USING NCHAR_CS is
included, the character from
National character set is returned.
• All the function coming under this category take some argument and
returns a numeric values.
Function Purpose Example Result
ABS(n) This function returns the absolute SELECT ABS(-23) AS “Negative Value” FROM Dual; NEGATIVE VALUE
value of n. -------------------------
23
CEIL(n) This function returns the smallest SELECT CEIL(-32.86) AS “Round up whole number” Round up whole number
integer that is greater than or equals FROM Dual; ---------------------------
to n. it rounds up to a whole number. ------
-32
FLOOR(n) This function returns the largest integer that SELECT FLOOR(-32.86) AS “Round down whole Round down whole number
is less than or equal to n. It rounds down to a number” FROM Dual; ---------------------------
whole number. ------
-33
MOD(n1, n2) This function returns n1 modulo n2 or the SELECT MOD(8,3) AS “8 divided by 3” FROM Dual; 8 divided by 3
remainder of n1 divided by n2. If the n1 is -------------------------
negative the result is negative. The sign of n2 2
doesn’t have any effect on the result.
ROUND(n, i) This function returns n rounded to i digits of SELECT ROUND(123.4523,2) AS “Positive round to Positive round to right
precision to the right of the decimal. If i is right” FROM Dual; ---------------------------
negative, i is rounded to left of the decimal. ------
123.45
TRUNC(n, i) This function returns n truncated to i digits of SELECT TRUNC(49.926,2) FROM Dual; TRUNC(49.926)
precision to the right of the decimal. If i is ---------------------
negative n is truncated to left of the decimal. 49.92
Date Functions
• Oracle database stores the date in a numeric format, representing the
country, year, month, day, hours, minutes and seconds.
• The default display and input format for any date is DD-MON-YY.
Example
SELECT SYSDATE FROM Dual;
Date Functions
• Oracle database stores Dates in numeric format which makes it
possible to perform calculations on them.
Operation Result Description
Date – Date Number of Days Subtract one date from another and returns
the number of days between the two.
For Assignment
For Expressions
Conversion Functions
Conversion Functions
Explicit data type conversion: These types of conversion uses
function to convert a value from one data type to another.
TO_CHAR explicit data type
conversion function
• The Oracle TO_CHAR function converts a number or date value to a
VARCHAR2 character string.
• the TO_CHAR function is divided into two sub-categories for the two
data types:
Example:
SELECT TO_CHAR(Price,’$99,999.00’) AS PRICE FROM Books;
Format Element for number
conversion
TO_CHAR Date
• Syntax:conversion
TO_CHAR(date, ‘format_model’)
Example:
SELECT TO_CHAR(SYSDATE, ‘fmDDTH MON YYYY’) AS
“Formatted Date”
FROM Dual;
General Functions
• General Functions work with any data type and relates to the use of
NULL values in the expression list.
General Functions
• Example 1: select employee_id,nvl(commission_pct,'3') from
hr.employees;
Syntax:
SELECT Table1.column, Table2.column FROM Table1, Table2
Equi Join
Non-Equi Join
Outer Join
Self Join
combines rows from two tables where there are same values for
the specified columns.
returns only those rows which satisfy the join condition in the
WHERE clause.
Equality operator (=) is the most common operator used to relate
two tables.
Are also known as inner join and simple join
Example :
SELECT I.B_Code, B.Title, B.Price, I.Quantity FROM Inventory I,
Books B
A non-equijoin join is a type of join in which we can join two
tables with the join condition where the join condition uses other
than equal operator (=).
Syntax:
SELECT table1.column, table2.column FROM table1, table2
WHERE table1.column[(+)] = table2.column [(+)]
Using LEFT OUTER JOIN to display data from two tables
Example
SELECT I.B_Code, B.Title, B.Price, I.Quantity FROM Books B,
Inventory I
WHERE I.B_Code (+) = B.B_Code;
Using RIGHT OUTER JOIN to display data from multiple tables
Example:
SELECT I.B_Code, B.Title, B.Price, I.Quantity FROM Books B,
Inventory I
WHERE I.B_Code = B.B_Code (+);
A table joining to itself is called Self join.
In self-join a table appears twice in the FROM clause of
the SELECT statement.
Example:
SELECT First.Author_ID, Second.Author_ID, First.B_Code
FROM Written_By First, Written_By Second
WHERE First.B_Code = Second.B_Code;
Any Questions ??