You are on page 1of 7

SQL command categories

DDL :- Data Definition Language


Create, drop, alter, truncate
DML :- Data Manipulation Language
Insert, Update, Delete
DCL :- Data Control Language
Grant, Revoke
DQL :- Data Query Language
Select

Select fieldlist | *
From tablename
Where fieldname operator expression

Fieldlist :- projection
Where :- restriction
Operators
Relational – comparison
Logical – and not or
Arithmetic :- + - / *
Nullable :- is null | is not null

Oracle Built in Functions


There are two types of functions in Oracle.

1) Single Row Functions: Single row or Scalar functions return a value for


every row that is processed in a query.
(OneRow – OneColumn Result set)

2) Group Functions: These functions group the rows of data based on the


values returned by the query. This is discussed in SQL GROUP Functions. The
group functions are used to calculate aggregate values like total or average,
which return just one total or one average value after processing a group of
rows.

There are four types of single row functions. They are:

1) Numeric Functions: These are functions that accept numeric input and


return numeric values.

2) Character or Text Functions: These are functions that accept character


input and can return both character and number values.

3) Date Functions: These are functions that take values that are of datatype
DATE as input and return values of datatype DATE, except for the
MONTHS_BETWEEN function, which returns a number.

4) Conversion Functions: These are functions that help us to convert a value


in one form to another form. For Example: a null value into an actual value, or a
value from one datatype to another datatype like NVL, TO_CHAR, TO_NUMBER,
TO_DATE etc.
You can combine more than one function together in an expression. This is
known as nesting of functions.

1) Numeric Functions:
Numeric functions are used to perform operations on numbers. They accept
numeric values as input and return numeric values as output. Few of the
Numeric functions are:
Function NameReturn Value

ABS (x) Absolute value of the number 'x'

CEIL (x) Integer value that is Greater than or equal to the number 'x'

FLOOR (x) Integer value that is Less than or equal to the number 'x'

TRUNC (x, y) Truncates value of number 'x' up to 'y' decimal places

ROUND (x, y) Rounded off value of the number 'x' up to the number 'y' decimal places

The following examples explains the usage of the above numeric functions
Function Name Examples Return Value

ABS (1) 1
ABS (x)
ABS (-1) -1

CEIL (2.83) 3
CEIL (x) CEIL (2.49) 3
CEIL (-1.6) -1

FLOOR (2.83) 2
FLOOR (x) FLOOR (2.49) 2
FLOOR (-1.6) -2
ROUND (125.456, 1) 125.4
TRUNC (x, y) ROUND (125.456, 0) 125
ROUND (124.456, -1) 120

TRUNC (140.234, 2) 140.23


TRUNC (-54, 1) 54
ROUND (x, y)
TRUNC (5.7) 5
TRUNC (142, -1) 140

2) Character or Text Functions:


Character or text functions are used to manipulate text strings. They accept
strings or characters as input and can return both character and number values
as output.
Few of the character or text functions are as given below:
Function Name Return Value

LOWER (string_value) All the letters in 'string_value' is converted to lowercase.

UPPER (string_value) All the letters in 'string_value' is converted to uppercase.

INITCAP (string_value) All the letters in 'string_value' is converted to mixed case.

LTRIM (string_value,
All occurrences of 'trim_text' is removed from the left of 'string_value'.
trim_text)

RTRIM (string_value,
All occurrences of 'trim_text' is removed from the right of 'string_value' .
trim_text)

TRIM (trim_text FROM All occurrences of 'trim_text' from the left and right
string_value) of 'string_value' , 'trim_text' can also be only one character long .

SUBSTR (string_value, m,Returns 'n' number of characters from 'string_value' starting from the


n) 'm' position.

LENGTH (string_value) Number of characters in 'string_value' in returned.

LPAD (string_value, n, Returns 'string_value' left-padded with 'pad_value' . The length of the


pad_value) whole string will be of 'n'  characters.

RPAD (string_value, n, Returns 'string_value' right-padded with 'pad_value' . The length of the


pad_value) whole string will be of 'n'  characters.

3) Date Functions:
These are functions that take values that are of datatype DATE as input and
return values of datatypes DATE, except for the MONTHS_BETWEEN function,
which returns a number as output.
Return
Function Name Examples
Value
ADD_MONTHS ( ) ADD_MONTHS ('16-Sep-81', 3) 16-Dec-81
MONTHS_BETWEEN ('16-Sep-81', '16-Dec-
MONTHS_BETWEEN( ) 3
81')
NEXT_DAY( ) NEXT_DAY ('01-Jun-08', 'Wednesday') 04-JUN-08
LAST_DAY( ) LAST_DAY ('01-Jun-08') 30-Jun-08
NEW_TIME( ) NEW_TIME ('01-Jun-08', 'IST', 'EST') 31-May-08

Deterministic and Nondeterministic Functions


Functions can be either deterministic or nondeterministic.
A deterministic function always returns the same results if given the
same input values. A nondeterministic function may return different
results every time it is called, even when the same input values are
provided.
Example :-
SYSDATE is non-deterministic – the current date and time will change depending on when
SYSDATE is called, but
LAST_DAY(date) is deterministic – for a given input date, the last date of the month will be
unchanged no matter when or how many times LAST_DAY(date) is called.

NVL, NVL2, COALESCE and NULLIF


Function in Oracle
These functions work with any data type and pertain to the use of null values in the
expression list. These functions use of null value and returns result value.

NVL : NVL() converts a null value to an actual value. Data types that can be
used are date, character and number. Data type must match with each other
i.e. expr1 and expr2 must of same data type.

Syntax –
NVL (expr1, expr2)
expr1 is the source value or expression that may contain a null.
expr2 is the target value for converting the null.
Example –

SELECT salary, NVL(commission_pct, 0),


(salary*12) + (salary*12*NVL(commission_pct, 0))
annual_salary FROM employees;

NVL2(expr1, expr2, expr3) : The NVL2 function examines the first


expression. If the first expression is not null, then the NVL2 function returns
the second expression. If the first expression is null, then the third
expression is returned i.e. If expr1 is not null, NVL2 returns expr2. If expr1 is
null, NVL2 returns expr3. The argument expr1 can have any data type.
Syntax –
NVL2 (expr1, expr2, expr3)
expr1 is the source value or expression that may contain null
expr2 is the value returned if expr1 is not null
expr3 is the value returned if expr1 is null
Example –
SELECT last_name, salary, commission_pct,
NVL2(commission_pct, ’SAL+COMM’, ’SAL’)
income FROM employees;
COALESCE() : The COALESCE() function examines the first expression, if
the first expression is not null, it returns that expression; Otherwise, it does a
COALESCE of the remaining expressions.
The advantage of the COALESCE() function over the NVL() function is that
the COALESCE function can take multiple alternate values. In simple words
COALESCE() function returns the first non-null expression in the list.
Syntax –

COALESCE (expr_1, expr_2, ... expr_n)


Examples –
SELECT last_name,
COALESCE(commission_pct, salary, 10) comm
FROM employees ORDER BY commission_pct;

NULLIF() : The NULLIF function compares two expressions. If they are


equal, the function returns null. If they are not equal, the function returns the
first expression. You cannot specify the literal NULL for first expression.
Syntax –
NULLIF (expr_1, expr_2)
Examples –
SELECT LENGTH(first_name) "expr1",
LENGTH(last_name) "expr2",
NULLIF(LENGTH(first_name),LENGTH(last_name))
result FROM employees;

DECODE() : Facilitates conditional inquiries by doing the work of a CASE or


IF-THEN-ELSE statement.
The DECODE function decodes an expression in a way similar to the IF-
THEN-ELSE logic used in various languages. The DECODE function
decodes expression after comparing it to each search value. If the
expression is the same as search, result is returned.
If the default value is omitted, a null value is returned where a search value
does not match any of the result values.
Syntax –
DECODE(col|expression, search1, result1
[, search2, result2,...,][, default])
Example –
SELECT last_name, job_id, salary,
DECODE(job_id, ’IT_PROG’, 1.10*salary,
’ST_CLERK’, 1.15*salary,
’SA_REP’, 1.20*salary,salary)
REVISED_SALARY FROM employees;

Aggregate Functions
Aggregate functions return a single result row based on groups of rows, rather than on single
rows. Aggregate functions can appear in select lists and in ORDER BY and HAVING clauses. They
are commonly used with the GROUP BY clause in a SELECT statement, where Oracle Database
divides the rows of a queried table or view into groups. In a query containing a GROUP BY clause,
the elements of the select list can be aggregate functions, GROUP BY expressions, constants, or
expressions involving one of these. Oracle applies the aggregate functions to each group of rows
and returns a single result row for each group.

select count(distinct(department_id)) from employees;

create table students ( student_no varchar(10), surname varchar(20), forename varchar(20));


create table modules ( module_code varchar(8), module_name varchar(20));

create table marks ( student_no varchar(10), module_code varchar(8), mark integer);

You might also like