0% found this document useful (0 votes)
2 views

Unit II

The document provides an overview of the relational model for database systems, originally proposed by Dr. E. F. Codd in 1970, highlighting its advantages over earlier models. It explains key concepts such as relations, attributes, keys (primary, candidate, and foreign), and integrity constraints, along with the significance of null values. Additionally, it covers SQL, its categories, and constraints that ensure data accuracy and reliability.

Uploaded by

gowtami
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit II

The document provides an overview of the relational model for database systems, originally proposed by Dr. E. F. Codd in 1970, highlighting its advantages over earlier models. It explains key concepts such as relations, attributes, keys (primary, candidate, and foreign), and integrity constraints, along with the significance of null values. Additionally, it covers SQL, its categories, and constraints that ensure data accuracy and reliability.

Uploaded by

gowtami
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Unit II

RELATIONAL MODEL
Introduction to relational model

The principles of the relational model were first outlined by Dr. E. F. Codd in a June 1970
paper called "A Relational Model of Data for Large Shared Data Banks:' In this paper. Dr.
Codd proposed the relational model for database systems. The more popular models used
at that time were hierarchical and network, or even simple flat file data structures.
Relational database management systems (RDBMS) soon became very popular, especially
for their ease of use and flexibility in structure. In addition, a number of innovative
vendors, such as Oracle, supplemented the RDBMS \with a suite of powerful application
development and user products, providing a total solution.Earlier we saw how to convert
an unorganized text description of information requirements into a conceptual design, by
the use of ER diagrams. The advantage of ER diagrams is that they force you to identify
data requirements that are implicitly known, but not explicitly written down in the original
description. Here we will see how to convert this ER into a logical design (this will be
defined below) of a relational database. The logical model is also called a Relational
Model.
Relational Model Concepts
We shall represent a relation as a table with columns and rows. Each column of the table
has a name, or attribute. Each row is called a tuple.

 Domain: a set of atomic values that an attribute can take


 Attribute: name of a column in a particular table
(all data is stored in tables). Each attribute Ai must
have a domain, dom(Ai).
 Relational Schema: The design of one table,
containing the name of the table (i.e. the name of
the relation), and the names of all the columns, or
attributes.
Example: STUDENT( Name, SID, Age, GPA)
 Degree of a Relation: the number of attributes in the relation's
schema.
 Tuple, t, of R( A1, A2, A3, …, An): an ORDERED set of values, <
v1, v2, v3,
…, vn>, where each vi is a value from dom( Ai).

 Relation Instance, r( R): a set of tuples; thus, r( R) = { t1, t2, t3, …, tm}

Properties of relations
Properties of database relations are:
 relation name is distinct from all other relations
 each cell of relation contains exactly one atomic (single) value
 each attribute has a distinct name
 values of an attribute are all from the same domain
 order of attributes has no significance
 each tuple is distinct; there are no duplicate tuples
 order of tuples has no significance, theoretically.
Relational keys
There are two kinds of keys in relations. The first are identifying keys: the
primary key is the main concept, while two other keys – super key and
candidate key – are related concepts. The second kind is the foreign key.

Identity Keys

Super keys

A super key is a set of attributes whose values can be used to uniquely


identify a tuple within a relation. A relation may have more than one super
key, but it always has at least one: the set of all attributes that make up the
relation.
Candidate Keys
A candidate key is a super key that is minimal; that is, there is no proper subset that is itself a
superkey. A relation may have more than one candidate key, and the different candidate keys
may have a different number of attributes. In other words, you should not interpret 'minimal'
to mean the super key with the fewest attributes.
A candidate key has two properties:
1.in each tuple of R, the values of K uniquely identify that tuple (uniqueness)

2.no proper subset of K has the uniqueness property (irreducibility).

Primary Key
The primary key of a relation is a candidate key especially selected to be the key for the
relation. In other words, it is a choice, and there can be only one candidate key designated to
be the primary key.
Relationship between identity keys
The relationship between keys:
Superkey  Candidate Key  Primary Key

Foreign keys
The attribute(s) within one relation that matches a candidate key of another relation. A
relation may have several foreign keys, associated with different target relations.Foreign keys
allow users to link information in one relation to information in another relation. Without
FKs, a database would be a collection of unrelated tables.

Relational Model Constraints

Integrity Constraints

Each relational schema must satisfy the following four types of constraints.
a.Domain constraints

Each attribute Ai must be an atomic value from dom( Ai) for that attribute. The attribute,
Name in the example is a BAD DESIGN (because sometimes we may want to search a person
by only using their last name.
b.Key Constraints
Superkey of R: A set of attributes, SK, of R such that no two tuples in any
valid relational instance, r( R), will have the same value for SK. Therefore,
for any two distinct tuples, t1 and t2 in r( R),
t1[ SK] != t2[SK].
Key of R: A minimal superkey. That is, a superkey, K, of R such that the
removal of ANY attribute from K will result in a set of attributes that are
not a super key.
Example CAR( State, LicensePlateNo, VehicleID, Model, Year, Manufacturer)

This schema has two keys:

K1 = { State, LicensePlateNo}

K2={Vehicle ID}
Both K1 and K2 are superkeys.
K3 = { VehicleID, Manufacturer} is a superkey, but not a key (Why?).
If a relation has more than one keys, we can select any one (arbitrarily) to be the primary key.
Primary Key attributes are underlined in the schema:

CAR(State, LicensePlateNo, VehicleID, Model, Year, Manufacturer)

c.Entity Integrity Constraints

The primary key attribute, PK, of any relational schema R in a database


cannot have null values in any tuple. In other words, for each table in a
DB, there must be a key; for each key, every row in the table must have
non-null values. This is because PK is used to identify the individual
tuples.
Mathematically, t[PK] != NULL for any tuple t € r( R).
D.Referential Integrity Constraints
Referential integrity constraints are used to specify the relationships between two relations
in a database.Consider a referencing relation, R1, and a referenced relation, R2. Tuples in
the referencing relation, R1, have attributed FK (called foreign key attributes) that
referencethe primary key attributes of the referenced relation, R2. A tuple, t1, in R1 is said
to reference a tuple, t2, in R2 if t1[FK] = t2[PK].A referential integrity constraint can be
displayed in a relational database schema as a directed arc from the referencing (foreign) key
to the referenced (primary) key. Examples are shown in the figure below:
Importance of null values:

The SQL NULL is the term used to represent a missing value. A NULL value in a table is a
value in a field that appears to be blank.

A field with a NULL value is a field with no value. It is very important to understand that a
NULL value is different than a zero value or a field that contains spaces.

Syntax

The basic syntax of NULL while creating a table.

SQL> CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

Here, NOT NULL signifies that column should always accept an explicit value of the given
data type. There are two columns where we did not use NOT NULL, which means these
columns could be NULL.

A field with a NULL value is the one that has been left blank during the record creation.

The NULL value can cause problems when selecting data. However, because when
comparing an unknown value to any other value, the result is always unknown and not
included in the results. You must use the IS NULL or IS NOT NULL operators to check for
a NULL value.

Consider the following CUSTOMERS table having the records as shown below.

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | |
| 7 | Muffy | 24 | Indore | |
+----+----------+-----+-----------+----------+

Now, following is the usage of the IS NOT NULLoperator.

SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY


FROM CUSTOMERS
WHERE SALARY IS NOT NULL;

This would produce the following result −

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
+----+----------+-----+-----------+----------+

Now, following is the usage of the IS NULL operator.

SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY


FROM CUSTOMERS
WHERE SALARY IS NULL;

This would produce the following result −

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | |
| 7 | Muffy | 24 | Indore | |

Significance of null values:

NULL value means that no entry has been made into the column. It states that the
corresponding value is either unknown or undefined. It is different from zero or "". They
should be avoided to avoid the complexity in select & update queries and also because
columns which have constraints like primary or foreign key constraints cannot contain a
NULL value.
why should we avoid permitting null values?

Null means no entry has been made. It implies that the value is either unknown or undefined.
We should avoid permitting null values because
1.Column with NULL values can't have PRIMARY KEY constraints.
2. Certain calculations can be inaccurate if NULL columns are involved.

Basic SQL:

SQL

What is SQL?
SQL is Structured Query Language, which is a computer language for storing, manipulating
and retrieving data stored in a relational database.

SQL is the standard language for Relational Database System. All the Relational Database
Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix,
Postgres and SQL Server use SQL as their standard database language.

Why SQL?
SQL is widely popular because it offers the following advantages −

 Allows users to access data in the relational database management


systems.
 Allows users to describe the data.
 Allows users to define the data in a database and manipulate that
data.
 Allows to embed within other languages using SQL modules,
libraries & pre-compilers.
 Allows users to create and drop databases and tables.
 Allows users to create view, stored procedure, functions in a
database.
 Allows users to set permissions on tables, procedures and views.

Categories in SQL:

There are 4 categories in SQL

1. Data Definition Language(DDL)


2. Data Manipulation Language(DML)

3. Data Control Language(DCL)

4. Transaction Control Language(TCL)

DATA DEFINITION LANGUAGE

 Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are
CREATE, ALTER, DROP, RENAME, and TRUNCATE.

CREATE: This command is used to create a table

Syntax:

CREATE TABLE table_name


(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
);
ALTER: This command is used for the following purposes

1. Add column in table.


2. Add multiple columns in table. ...
3. Modify column in table. ...
4. Drop column in table. ..
5. Rename column in table. ..
6. Rename table

Add a column to a table

Syntax:

ALTER TABLE table_name


ADD column_name column-definition;

Add multiple columns in table

Syntax:

ALTER TABLE table_name


ADD column_1 column-definition,
column_2 column-definition,
...
column_n column_definition;

Modify column in table

Syntax:

ALTER TABLE table_name


MODIFY column_name column_type

Drop column in table


Syntax:

ALTER TABLE table_name


DROP COLUMN column_name;

Rename column in table

Syntax:

ALTER TABLE table_name


RENAME COLUMN old_name to new_name;

Rename table

Syntax

ALTER TABLE table_name


RENAME TO new_table_name;

DROP: It is used to drop a table from the database

Syntax:
DROP TABLE <table_name>

TRUNCATE: It is used to delete all the rows of a table

Syntax:

TRUNCATE TABLE <Table_name>

DATA MANIPULATION LANGUAGE:

 Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving, modifying, and deleting data. These commands are SELECT, INSERT,
UPDATE, and DELETE.

INSERT: It is used to insert data into table

Syntax1:To insert data into specified columns


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Syntax2:To insert data into all columns

INSERT INTO table_name VALUES (value1, value2, value3, ...);

Syntax3: To insert multiple rows at a time

INSERT INTO table_name VALUES (&value1, &value2, &value3, ...);

DELETE: It is a used to delete one or more records from a table

Syntax:

DELETE FROM table


[WHERE conditions]

UPDATE:

It is used to update existing records in the tables.

Syntax:

UPDATE table
SET column1 = expression1,
column2 = expression2,
...
[WHERE conditions];

SQL CONSTRAINTS

Constraints are the rules enforced on the data columns of a table. These are used to limit
the type of data that can go into a table. This ensures the accuracy and reliability of the
data in the database.

Constraints can be defined in two ways.


Using Create command: When a table is created with the CREATE TABLE statement
we can specify constraints. Constraints could be either on a 1. Column level or 2. Table
level

Column level : Constraints can be specified at the time of column definition. Column-
level constraints refer to a single column in the table and do not specify a column name
(except check constraints). They refer to the column that they follow.

Syntax: create table table name(col1 datatype constraint constraint name constraint
type,

col2 data type,

col3 data type,.....coln data type);

Table level: Table-level constraints refer to one or more columns in the table. Table-
level constraints specify the names of the columns to which they apply. Table level
constraints are applied to the whole table

Syntax: create table table name(col1 datatype,

col2 data type,

col3 data type,.....coln data type,

constraint constraint name constraint type(column name));

Using Alter command: We can use the ALTER TABLE statement to add constraints
even after the table is created.

Syntax:

Alter table table name add constraint constraint name constraint type(column
name);

Following are some of the most commonly used constraints available in SQL.

NOT NULL Constraint − Ensures that a column cannot have a NULL value.

DEFAULT Constraint − Provides a default value for a column when none is specified.

UNIQUE Constraint − Ensures that all values in a column are different.


PRIMARY Key − Uniquely identifies each row/record in a database table.

FOREIGN Key − Uniquely identifies a row/record in any of the given database table.

CHECK Constraint − The CHECK constraint ensures that all the values in a column
satisfies certain conditions.

INDEX − Used to create and retrieve data from the database very quickly.

Column constraints include:

NOT NULL, PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK

Table constraints include:

PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK

Primary key constraints


A primary key is a field in a table which uniquely identifies each row/record in a
database table. Primary keys must contain unique values. A primary key column cannot
have NULL values.

A table can have only one primary key, which may consist of single or multiple fields.
When multiple fields are used as a primary key, they are called a composite key.

If a table has a primary key defined on any field(s), then you cannot have two records
having the same value of that field(s)

Syntax to define a Primary key at column level:


column name datatype [CONSTRAINT constraint_name] PRIMARY KEY

Syntax to define a Primary key at table level:


[constraint constraint name]primary key(column_name1, column_name2..)
column_name1, column_name2 are the names of the columns which define the
primary Key.
The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional

Not Null Constraint :

This constraint ensures all rows in the table contain a definite value for the column
which is specified as not null. Which means a null value is not allowed

Syntax to define a Not Null constraint:


[CONSTRAINT constraint name] NOT NULL
Unique Constraint:

The UNIQUE constraint ensures that all values in a column are different. Both the
UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE
constraint..We can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.

Syntax to define a Unique key at table level:


[CONSTRAINT constraint_name] UNIQUE

Syntax to define a Unique key at table level:


[CONSTRAINT constraint_name] UNIQUE(column_name)

FOREIGN Key Constraint

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.

The table containing the foreign key is called the child table, and the table containing the
candidate key is called the referenced or parent table.

Syntax to define a Foreign key at column level:

[constraint constraint name] references parent table name(column name)

Syntax to define a Foreign key at table level:

[constraint constraint_name] foreign key(column_name) references parent table


name(column name)
SQL Check Constraint :
This constraint defines a business rule on a column. All the rows must satisfy this rule.
The constraint can be applied for a single column or a group of columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK (condition)

SQL DEFAULT Constraint

The DEFAULT constraint is used to provide a default value for a column.

The default value will be added to all new records IF no other value is specified.

Syntax:

[Constraint constraint name] default default value

Dropping Constraints
Any constraint that you have defined can be dropped using the ALTER TABLE
command with the DROP CONSTRAINT option

Syntax: Alter table table name drop constraint constraint nam

Basic SQL querying (select and project) using where clause

The SQL WHERE clause is used to specify a condition while fetching the data from a single
table or by joining with multiple tables. If the given condition is satisfied, then only it returns
a specific value from the table. You should use the WHERE clause to filter the records and
fetching only the necessary records.
The WHERE clause is not only used in the SELECT statement, but it is also used in the
UPDATE, DELETE statement, etc., which we would examine in the subsequent chapters.

Syntax

The basic syntax of the SELECT statement with the WHERE clause is as shown below.

SELECT column1, column2, columnN


FROM table_name
WHERE [condition]

You can specify a condition using the comparison or logical operators like >, <, =, LIKE,
NOT, etc. The following examples would make this concept clear.

Example

Consider the CUSTOMERS table having the following records −

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

The following code is an example which would fetch the ID, Name and Salary fields from the
CUSTOMERS table, where the salary is greater than 2000 −

SQL> SELECT ID, NAME, SALARY


FROM CUSTOMERS
WHERE SALARY > 2000;

This would produce the following result −

+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |

Select Operation : This operation is used to select rows from a table


(relation) that specifies a given logic, which is called as a predicate. The
predicate is a user defined condition to select rows of user's choice.

Project Operation : If the user is interested in selecting the values of a


few attributes, rather than selection all attributes of the Table (Relation),
then one should go for PROJECT Operation. Project is not a statement. It is
the capability of the select statement. Select statement has three
capabilities. They are selection,projection,join. Selection-it retrieves the
rows that are satisfied by the given query. Projection-it chooses the
columns that are satisfied by the given query. Join-it joins the two or more
tables
Selection
SELECT * FROM customer WHERE state = 'NJ';

SELECT city, state, zipcode FROM customer;

SQL Operators:

Arithmetic Operators

Operator Meaning
+ (Add) Addition
- (Subtract) Subtraction
* (Multiply) Multiplication
/ (Divide) Division
% (Modulo) Returns the integer
remainder of a division.
For example, 17 % 5 = 2
because the remainder of
17 divided by 5 is 2.
SQL FUNCTIONS:
SQL functions are built into Oracle Database and are available for use in various appropriate SQL
statements.

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.
2) Group or Multiple 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.

1. Single Row Functions:


 Can manipulate data.
 Accept arguments and returns one value.
 Act on each row returned.
 Return one result per row.
 Can modify the date type(type casting).
 Can be nested.

There are four types of single row functions.

1. Numeric Functions or mathematical 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.

2. Group or Multiple Functions:

Group functions are built-in SQL functions that operate on groups of rows and return one value for
the entire group. These functions are: COUNT, MAX, MIN, AVG, SUM, DISTINCT, STDDEV

Mathematical Functions

Least Function:

least function returns the smallest value in a list of expressions.

Syntax

The syntax for the least function is:

least( expr1, expr2, ... expr_n )


expr1, expr2, . expr_n are expressions that are evaluated by the least function.

Note

If the datatypes of the expressions are different, all expressions will be converted to whatever
datatype expr1 is.

If the comparison is based on a character comparison, one character is considered smaller


than another if it has a lower character set value.

Having a NULL value in one of the expressions will return NULL as the least value.

For Example
least(2, 5, 12, 3) would return 2

least('2', '5', '12', '3') would return '12'

least('apples', 'oranges', 'bananas') would return 'apples'

least('apples', 'applis', 'applas') would return 'applas'

least('apples', 'applis', 'applas', null) would return NULL

Greatest Function

greatest function returns the greatest value in a list of expressions.

Syntax

The syntax for the greatest function is:

greatest( expr1, expr2, ... expr_n )

expr1, expr2, . expr_n are expressions that are evaluated by the greatest function.

Note

If the datatypes of the expressions are different, all expressions will be converted to whatever
datatype expr1 is.

If the comparison is based on a character comparison, one character is considered greater


than another if it has a higher character set value.

For Example
greatest(2, 5, 12, 3) would return 12

greatest('2', '5', '12', '3') would return '5'

greatest('apples', 'oranges', 'bananas') would return 'oranges'


greatest('apples', 'applis', 'applas') would return 'applis'

Ceil function

ceil function returns the smallest integer value that is greater than or equal to a number.

Syntax

The syntax for the ceil function is:

ceil( number )

number is the value used to find the smallest integer value.

For Example

would return
ceil(32.65)
33

would return
ceil(32)
32

would return
ceil(-32.65)
-32

would return
ceil(-32)
-32

Floor Function

In Oracle/PLSQL, the floor function returns the largest integer value that is
equal to or less than a number.

Syntax

The syntax for the floor function is:

floor( number )

number is the value used to determine the largest integer value that is equal to or
less than a number.

For Example
floor(5.9) would return 5

floor(34.29 would return 34


)

floor(-5.9) would return -6

MOD function:

mod function returns the remainder of m divided by n.

Syntax

The syntax for the mod function is:

mod( m, n )

The mod is calculated as:

m - n * floor(m/n)

Note
The mod function uses the floor function in its formula, whereas the remainder function uses the
round function in its formula.

The mod function returns m if n is 0.

For Example

would
mod(15, 4)
return 3

would
mod(15, 0) return
15

would
mod(11.6, 2) return
1.6

would
mod(11.6, 2.1) return
1.1

mod(-15, 4) would
return -
3

mod(-15, 0)

Remainder Function

remainder function returns the remainder of m divided by n.

Syntax

The syntax for the remainder function is:

remainder( m, n )

The remainder is calculated as:

m - (n * X) where X is the integer nearest m / n would


return -
Note 15

The remainder function uses the round function in its formula, whereas the mod
function uses the floor function in its formula.

For Example
remainder(15, 6) would return 3

remainder(15, 5) would return 0

remainder(15, 4) would return -1

remainder(11.6, 2) would return -0.4

remainder(11.6, 2.1) would return -1

remainder(-15, 4) would return 1


Round Function (with numbers)

the round function returns a number rounded to a certain number of decimal places.

Syntax

The syntax for the round function is:

round( number, [ decimal_places ] )

number is the number to round.

decimal_places is the number of decimal places rounded to. This value must be an
integer. If this parameter is omitted, the round function will round the number to 0
decimal places.

For Example
would
round(125.315)
return 125

would
round(125.315, 0)
return 125

would
round(125.315, 1) return
125.3

would
round(125.315, 2) return
125.32

would
round(125.315, 3) return
125.315

would
round(-125.315, 2) return -
125.32

Trunc Function (with numbers)


In Oracle/PLSQL, the trunc function returns a number truncated to a
certain number of decimal places.

Syntax

The syntax for the trunc function is:

trunc( number, [ decimal_places ] )

number is the number to truncate.

decimal_places is the number of decimal places to truncate to. This value


must be an integer. If this parameter is omitted, the trunc function will
truncate the number to 0 decimal places.

For Example
trunc(125.815) would return 125

trunc(125.815, 0) would return 125

trunc(125.815, 1) would return 125.8

trunc(125.815, 2) would return 125.81

trunc(125.815, 3) would return 125.815

trunc(-125.815, 2) would return -125.81

trunc(125.815, -1) would return 120

trunc(125.815, -2) would return 100

trunc(125.815, -3) would return 0

Power Function

the power function returns m raised to the nth power.

Syntax

The syntax for the power function is:

power( m, n )

m is the base.

n is the exponent.
Note

If m is negative, then n must be an integer.

For Example
power(3, 2) would return 9

power(5, 3) would return 125

power(-5, 3) would return -125

power(6.2, 3) would return 238.328

power(6.2, 3.5) would return 593.431934277892

Sin Function

In Oracle/PLSQL, the sin function returns the sine of n.

Syntax

The syntax for the sin function is:

sin( n )

n is a number. It is an angle expressed in radians.

For Example
would return
sin(3)
0.141120008059867

would return -
sin(5.2)
0.883454655720153

would return
sin(-5.2)
0.883454655720153

Sign Function

In Oracle/PLSQL, the sign function returns a value


indicating the sign of a number.

Syntax
The syntax for the sign function is:

sign( number )

number is the number to test for its sign.

Note

If number < 0, then sign returns -1.

If number = 0, then sign returns 0.

If number > 0, then sign returns 1.

For Example
would
sign(-23)
return -1

would
sign(-0.001)
return -1

would
sign(0)
return 0

would
sign(0.001)
return 1

would
sign(23)
return 1

would
sign(23.601)
return 1

Log Function

In Oracle/PLSQL, the log function returns


the logarithm of n base m.

Syntax

The syntax for the log function is:

log( m, n )

m must be a positive number, except 0 or


1.

n must be a positive number.

For Example
log(10, 20) would return 1.30102999566398

log(2, 15) would return 3.90689059560852

log(100, 1) would return 0

Exp Function

In Oracle/PLSQL, the exp function returns


e raised to the nth power, where e =
2.71828183.

Syntax

The syntax for the exp function is:

exp( number )

number is the power to raise e to.

For Example
exp(3) would return 20.0855369231877

exp(3.1) would return 22.1979512814416

exp(-3) would return 0.0497870683678639

Sqrt Function

sqrt function returns the square root of n.

Syntax

The syntax for the sqrt function is:

sqrt( n )

n is a positive number.

For Example
sqrt(9) would return 3

sqrt(37) would return 6.08276253029822

sqrt(5.617) would return 2.37002109695251

Character Functions:

Ascii Function

In Oracle/PLSQL, the ascii function returns the NUMBER code that represents the specified
character.

Syntax

The syntax for the ascii function is:

ascii( single_character )

single_character is the specified character to retrieve the NUMBER code for. If more than
one character is entered, the function will return the value for the first character and ignore all
of the characters after the first.

For Example
would return
ascii('t')
116

would return
ascii('T')
84

would also
ascii('T2')
return 84

Chr Function

In Oracle/PLSQL, the chr function is the opposite of the ascii function. It


returns the character based on the NUMBER code.

Syntax

The syntax for the chr function is:

chr( number_code )

number_code is the NUMBER code used to retrieve the character.

For Example
chr(116); would return 't'

chr(84); would return 'T'

Concat Function

In Oracle/PLSQL, the concat function allows you to concatenate two strings


together.

Syntax

The syntax for the concat function is:

concat( string1, string2 )

string1 is the first string to concatenate.

string2 is the second string to concatenate.

For Example
concat('Tech on', ' the Net'); would return 'Tech on the Net'

concat('a', 'b') would return 'ab'

Initcap Function

In Oracle/PLSQL, the initcap function sets the first character in each word to uppercase and
the rest to lowercase.

Syntax

The syntax for the initcap function is:

initcap( string1 )
string1 is the string argument whose first character in each word will be converted to
uppercase and all remaining characters converted to lowercase.

For Example
initcap('tech on the net'); would return 'Tech On The Net'

initcap('GEORGE BURNS'); would return 'George Burns'

Instr Function

In Oracle/PLSQL, the instr function returns the location of a substring in a string.

Syntax

The syntax for the instr Oracle function is:

instr( string1, string2 [, start_position [, nth_appearance ] ] )

string1 is the string to search.

string2 is the substring to search for in string1.

start_position is the position in string1 where the search will start. This argument is optional.
If omitted, it defaults to 1. The first position in the string is 1. If the start_position is negative,
the function counts back start_position number of characters from the end of string1 and then
searches towards the beginning of string1.

nth_appearance is the nth appearance of string2. This is optional. If omitted, it defaults to 1.

Note

If string2 is not found in string1, then the instr Oracle function will return 0.

For Example
instr('Tech on the net', 'e') would return 2; the first occurrence of 'e'

instr('Tech on the net', 'e', 1, 1) would return 2; the first occurrence of 'e'

instr('Tech on the net', 'e', 1, 2) would return 11; the second occurrence of 'e'

instr('Tech on the net', 'e', 1, 3) would return 14; the third occurrence of 'e'

instr('Tech on the net', 'e', -3, 2) would return 2

Length Function

In Oracle/PLSQL, the length function returns the length of the specified string.
Syntax

The syntax for the length function is:

length( string1 )

string1 is the string to return the length for. If string1 is NULL, then the function returns
NULL.

For Example
length(NULL) would return NULL

length('') would return NULL

length('Tech on the Net') would return 15

length('Tech on the Net ') would return 16

Lower Function

In Oracle/PLSQL, the lower function converts all letters in the specified string to lowercase.
If there are characters in the string that are not letters, they are unaffected by this function.

Syntax

The syntax for the lower function is:

lower( string1 )

string1 is the string to convert to lowercase.

For Example
lower('Tech on the Net'); would return 'tech on the net'

lower('GEORGE BURNS 123 '); would return 'george burns 123 '

Upper Function

In Oracle/PLSQL, the upper function converts all letters in the specified string to uppercase.
If there are characters in the string that are not letters, they are unaffected by this function.

Syntax

The syntax for the upper function is:

upper( string1 )
string1 is the string to convert to uppercase.

For Example
upper('Tech on the Net'); would return 'TECH ON THE NET'

upper('george burns 123 '); would return 'GEORGE BURNS 123 '

Lpad Function

In Oracle/PLSQL, the lpad function pads the left-side of a string with a specific set of
characters (when string1 is not null).

Syntax

The syntax for the lpad function is:

lpad( string1, padded_length, [ pad_string ] )

string1 is the string to pad characters to (the left-hand side).

padded_length is the number of characters to return. If the padded_length is smaller than the
original string, the lpad function will truncate the string to the size of padded_length.

pad_string is optional. This is the string that will be padded to the left-hand side of string1. If
this parameter is omitted, the lpad function will pad spaces to the left-side of string1.

For Example
lpad('tech', 7); would return ' tech'

lpad('tech', 2); would return 'te'

lpad('tech', 8, '0'); would return '0000tech'

lpad('tech on the net', 15, 'z'); would return 'tech on the net'

lpad('tech on the net', 16, 'z'); would return 'ztech on the net'

Rpad Function

In Oracle/PLSQL, the rpad function pads the right-side of a string with a specific set of
characters (when string1 is not null).

Syntax

The syntax for the rpad function is:

rpad( string1, padded_length, [ pad_string ] )


string1 is the string to pad characters to (the right-hand side).

padded_length is the number of characters to return. If the padded_length is smaller than the
original string, the rpad function will truncate the string to the size of padded_length.

pad_string is optional. This is the string that will be padded to the right-hand side of string1.
If this parameter is omitted, the rpad function will pad spaces to the right-side of string1.

For Example
rpad('tech', 7); would return 'tech '

rpad('tech', 2); would return 'te'

rpad('tech', 8, '0'); would return 'tech0000'

rpad('tech on the net', 15, 'z'); would return 'tech on the net'

rpad('tech on the net', 16, 'z'); would return 'tech on the netz'

Ltrim Function

In Oracle/PLSQL, the ltrim function removes all specified characters from the left-hand side
of a string.

Syntax

The syntax for the ltrim function is:

ltrim( string1, [ trim_string ] )

string1 is the string to trim the characters from the left-hand side.

trim_string is the string that will be removed from the left-hand side of string1. If this
parameter is omitted, the ltrim function will remove all leading spaces from string1.

For Example
ltrim(' tech'); would return 'tech'

ltrim(' tech', ' '); would return 'tech'

ltrim('000123', '0'); would return '123'

ltrim('123123Tech', '123'); would return 'Tech'

ltrim('123123Tech123', '123'); would return 'Tech123'

ltrim('xyxzyyyTech', 'xyz'); would return 'Tech'

ltrim('6372Tech', '0123456789'); would return 'Tech'


ltrim('xxyyxzyxyyxTech', 'xyz'); would return 'Tech'

ltrim( '637Tech', '0123456789'); would return 'Tech'

Rtrim Function

rtrim function removes all specified characters from the right-hand side of a string.

Syntax

The syntax for the rtrim function is:

rtrim( string1, [ trim_string ] )

string1 is the string to trim the characters from the right-hand side.

trim_string is the string that will be removed from the right-hand side of string1. If this
parameter is omitted, the rtrim function will remove all trailing spaces from string1.

For Example
rtrim('tech '); would return 'tech'

rtrim('tech ', ' '); would return 'tech'

rtrim('123000', '0'); would return '123'

rtrim('Tech123123', '123'); would return 'Tech'

rtrim('123Tech123', '123'); would return '123Tech'

rtrim('Techxyxzyyy', 'xyz'); would return 'Tech'

rtrim('Tech6372', '0123456789'); would return 'Tech'

rtrim('Techxyxxyzyyyxx', 'xyz'); would returN 'Tech'

rtrim('Tech6372', '0123456789'); would return 'Tech'


Replace Function

In Oracle/PLSQL, the replace function replaces a sequence of characters in a string with


another set of characters.

Syntax

The syntax for the replace function is:

replace( string1, string_to_replace, [ replacement_string ] )

string1 is the string to replace a sequence of characters with another set of characters.

string_to_replace is the string that will be searched for in string1.

replacement_string is optional. All occurrences of string_to_replace will be replaced with


replacement_string in string1. If the replacement_string parameter is omitted, the replace
function simply removes all occurrences of string_to_replace, and returns the resulting
string.

For Example
replace('123123tech', '123'); would return 'tech'

replace('123tech123', '123'); would return 'tech'

replace('222tech', '2', '3'); would return '333tech'

replace('0000123', '0'); would return '123'

replace('0000123', '0', ' '); would return ' 123'

Substr Function

the substr functions allows you to extract a substring from a string.

Syntax

The syntax for the substr function is:

substr( string, start_position, [ length ] )

string is the source string.

start_position is the position for extraction. The first position in the string is always 1.

length is optional. It is the number of characters to extract. If this parameter is omitted,


substr will return the entire string.
Note

If start_position is 0, then substr treats start_position as 1 (ie: the first position in the string).

If start_position is a positive number, then substr starts from the beginning of the string.

If start_position is a negative number, then substr starts from the end of the string and counts
backwards.

If length is a negative number, then substr will return a NULL value.

For Example
substr('This is a test', 6, 2) would return 'is'

substr('This is a test', 6) would return 'is a test'

substr('TechOnTheNet', 1, 4) would return 'Tech'

substr('TechOnTheNet', -3, 3) would return 'Net'

substr('TechOnTheNet', -6, 3) would return 'The'

substr('TechOnTheNet', -8, 2) would return 'On'

Translate Function

In Oracle/PLSQL, the translate function replaces a sequence of characters in a string with


another set of characters. However, it replaces a single character at a time. For example, it
will replace the 1st character in the string_to_replace with the 1st character in the
replacement_string. Then it will replace the 2nd character in the string_to_replace with the
2nd character in the replacement_string, and so on.

Syntax

The syntax for the translate function is:

translate( string1, string_to_replace, replacement_string )

string1 is the string to replace a sequence of characters with another set of characters.

string_to_replace is the string that will be searched for in string1.

replacement_string - All characters in the string_to_replace will be replaced with the


corresponding character in the replacement_string.

For Example
translate('1tech23', '123', '456'); would return '4tech56'

translate('222tech', '2ec', '3it'); would return '333tith'


VSize Function

vsize function returns the number of bytes in the internal representation of an expression.

Syntax

The syntax for the vsize function is:

vsize( expression )

expression is the string to evaluate.

For Example
vsize('Tech on the net') would return 15

vsize('Tech on the net ') would return 16

vsize(null) would return <null>

vsize('') would return <null>

vsize(' ') would return 1

Date Functions

Add_Months Function

add_months function returns a date plus n months.

Syntax

The syntax for the add_months function is:

add_months( date1, n )

date1 is the starting date (before the n months have been added).

n is the number of months to add to date1.

For Example
would return '01-Nov-
add_months('01-Aug-03', 3)
03'
would return '01-May-
add_months('01-Aug-03', -3)
03'

would return '21-May-


add_months('21-Aug-03', -3)
03'

would return '28-Feb-


add_months('31-Jan-03', 1)
03'

select current_date from dual;

Last_Day Function

last_day function returns the last day of the month based on a date
value.

Syntax

The syntax for the last_day function is:

last_day( date )

date is the date value to use to calculate the last day of the month.

For Example
last_day(to_date('2003/03/15', 'yyyy/mm/dd')) would return Mar 31, 2003

last_day(to_date('2003/02/03', 'yyyy/mm/dd')) would return Feb 28, 2003

last_day(to_date('2004/02/03', 'yyyy/mm/dd')) would return Feb 29, 2004

To_Date Function

to_date function converts a string to a date.

Syntax

The syntax for the to_date function is:

to_date( string1, [ format_mask ], [ nls_language ] )

string1 is the string that will be converted to a date.


format_mask is optional. This is the format that will be used to convert string1 to a date.

nls_language is optional. This is the nls language used to convert string1 to a date.

For Example
to_date('2003/07/09', 'yyyy/mm/dd') would return a date value of July 9, 2003

to_date('070903', 'MMDDYY') would return a date value of July 9, 2003

to_date('20020315', 'yyyymmdd') would return a date value of Mar 15, 2002

Months_Between Function

months_between function returns the number of months between date1 and date2.

Syntax

The syntax for the months_between function is:

months_between( date1, date2 )

date1 and date2 are the dates used to calculate the number of months.

If a fractional month is calculated, the months_between function calculates the fraction


based on a 31-day month.

Example #1
months_between (to_date ('2003/01/01', 'yyyy/mm/dd'), to_date ('2003/03/14', 'yyyy/mm/dd') )

would return -2.41935483870968

Example #2
months_between (to_date ('2003/07/01', 'yyyy/mm/dd'), to_date ('2003/03/14', 'yyyy/mm/dd') )

would return 3.58064516129032

Example #3
months_between (to_date ('2003/07/02', 'yyyy/mm/dd'), to_date ('2003/07/02', 'yyyy/mm/dd') )

would return 0

Example #4
months_between (to_date ('2003/08/02', 'yyyy/mm/dd'), to_date ('2003/06/02', 'yyyy/mm/dd') )

would return 2
Next_Day Function

next_day function returns the first weekday that is greater than a date.

Syntax

The syntax for the next_day function is:

next_day( date, weekday )

date is used to find the next weekday.

weekday is a day of the week (ie: SUNDAY, MONDAY, TUESDAY, WEDNESDAY,


THURSDAY, FRIDAY, SATURDAY)

For Example
next_day('01-Aug-03', 'TUESDAY') would return '05-Aug-03'

next_day('06-Aug-03', 'WEDNESDAY') would return '13-Aug-03'

next_day('06-Aug-03', 'SUNDAY') would return '10-Aug-03'

Sysdate function

sysdate function returns the current system date and time on your local database.

Syntax

The syntax for the sysdate function is:

sysdate
Example #1
select sysdate
from dual;

Trunc Function (with dates)

trunc function returns a date truncated to a specific unit of measure.

Syntax

The syntax for the trunc function is:

trunc ( date, [ format ] )

date is the date to truncate.


format is the unit of measure to apply for truncating. If the format parameter is omitted, the
trunc function will truncate the date to the day value, so that any hours, minutes, or seconds
will be truncated off.

Below are the valid format parameters:

Unit Valid format parameters

Year SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y

ISO Year IYYY, IY, I

Quarter Q

Month MONTH, MON, MM, RM

Week WW

IW IW

W W

Day DDD, DD, J

Start day of the week DAY, DY, D

Hour HH, HH12, HH24

Minute MI

For Example
trunc(to_date('22-AUG-03'), 'YEAR') would return '01-JAN-03'

trunc(to_date('22-AUG-03'), 'Q') would return '01-JUL-03'

trunc(to_date('22-AUG-03'), 'MONTH') would return '01-AUG-03'

trunc(to_date('22-AUG-03'), 'DDD') would return '22-AUG-03'

trunc(to_date('22-AUG-03'), 'DAY') would return '17-AUG-03'

//Term: ROUND

Definition:
In Oracle, ROUND is a built in overloaded function, which is used to round off numeric and
DATE type inputs to a specified precision.

Example Syntax:
ROUND( n, X)

In the syntax example above, "n" can be a numeric or date type input. "X" is the rounding format.
Date type inputs can be rounded by year, month, quarter, week, hour and minute.

Example Usage:

The below set of SQL statements show the use of the ROUND function with both Number and Date
inputs. In the first two examples, note the difference in the results by having positive and negative
precisions. If the precision is positive, it rounds the digits which are placed after the decimal. For
negative precision values, it rounds the digits which are placed before the decimal place.//

SQL> SELECT ROUND(1.2536,2) FROM DUAL;

ROUND(1.2536,2)

---------------

1.25

SQL> SELECT ROUND(1234,-2) FROM DUAL;

ROUND(1234,-2)

--------------

1200

SQL> SELECT ROUND(TO_DATE('02-AUG-2011','DD-MM-YYYY'),'YYYY') FROM DUAL;

ROUND(TO_

---------

01-JAN-12

SQL> SELECT ROUND(TO_DATE('02-AUG-2011','DD-MM-YYYY'),'MM') FROM DUAL;

ROUND(TO_

---------

01-AUG-11
Round Function (with dates)

round function returns a date rounded to a specific unit of measure.

Syntax

The syntax for the round function is:

round( date, [ format ] )

date is the date to round.

format is the unit of measure to apply for rounding. If the format parameter is omitted, the
round function will round to the nearest day.

Below are the valid format parameters:

Unit Valid format parameters Rounding Rule

SYYYY, YYYY, YEAR, SYEAR,


Year Rounds up on July 1st
YYY, YY, Y

ISO Year IYYY, IY, I

Rounds up on the 16th day of the second


Quarter Q
month of the quarter

Month MONTH, MON, MM, RM Rounds up on the 16th day of the month

Same day of the week as the first day of the


Week WW
year

Same day of the week as the first day of the


IW IW
ISO year

Same day of the week as the first day of the


W W
month

Day DDD, DD, J

Start day of the


DAY, DY, D
week

Hour HH, HH12, HH24

Minute MI

For Example
round(to_date ('22-AUG-03'),'YEAR') would return '01-JAN-04'

round(to_date ('22-AUG-03'),'Q') would return '01-OCT-03'


round(to_date ('22-AUG-03'),'MONTH') would return '01-SEP-03'

round(to_date ('22-AUG-03'),'DDD') would return '22-AUG-03'

round(to_date ('22-AUG-03'),'DAY') would return '24-AUG-03'

To_Char Function

In Oracle/PLSQL, the to_char function converts a number or date to a string.

Syntax

The syntax for the to_char function is:

to_char( value, [ format_mask ], [ nls_language ] )

value can either be a number or date that will be converted to a string.

format_mask is optional. This is the format that will be used to convert value to a string.

nls_language is optional. This is the nls language used to convert value to a string.

Examples - Numbers

The following are number examples for the to_char function.

to_char(1210.73, '9999.9') would return '1210.7'

to_char(1210.73, '9,999.99') would return '1,210.73'

to_char(1210.73, '$9,999.00') would return '$1,210.73'

to_char(21, '000099') would return '000021'

The following are date examples for the to_char function.

to_char(sysdate, 'yyyy/mm/dd'); would return '2003/07/09'

to_char(sysdate, 'Month DD, YYYY'); would return 'July 09, 2003'

to_char(sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003'

to_char(sysdate, 'MON DDth, YYYY'); would return 'JUL 09TH, 2003'

to_char(sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003'


to_char(sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'

to_char(sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003'

to_char(sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003'

to_char(sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'

The zeros have been suppressed so that the day component shows as "9" as opposed to "09".

Conversion Functions

Bin_To_Num Function

bin_to_num function converts a bit vector to a number.

Syntax

The syntax for the bin_to_num function is:

bin_to_num( expr1, expr2, ... expr_n)

expr1, expr2, ... expr_n must be either 0 or 1 values. They represent bits in a bit vector.

For Example
would
bin_to_num(1)
return 1

would
bin_to_num(1,0)
return 2

would
bin_to_num(1,1)
return 3

would
bin_to_num(1,1,1,0)
return 14

would
bin_to_num(1,1,1,1)
return 15

To_Number Function

the to_number function converts a string to a number.

Syntax
The syntax for the to_number function is:

to_number( string1, [ format_mask ], [ nls_language ] )

string1 is the string that will be converted to a number.

format_mask is optional. This is the format that will be used to convert string1 to a
number.

nls_language is optional. This is the nls language used to convert string1 to a


number.

For Example
to_number('1210.73', '9999.99') would return the number 1210.73

to_number('546', '999') would return the number 546

to_number('23', '99') would return the number 23

Coalesce Function

coalesce function returns the first non-null expression in the list. If all expressions evaluate
to null, then the coalesce function will return null.

Syntax

The syntax for the coalesce function is:

coalesce( expr1, expr2, ... expr_n )

expr1 to expr_n are the expressions to test for non-null values.

The coalesce function will compare each value, one by one.

COALESCE will return the first not null value.

It requires at least two arguments

If the arguments entered are of different datatype then it will throw an error

SQL> select coalesce(1,3,null,'jeet') from dual;


select coalesce(1,3,null,'jeet') from dual
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
SQL> select coalesce('a','as',null) from dual;
C
-
a
SQL> select COALESCE(null,null) from dual;
C
-

SQL> select COALESCE (null) from dual;


select COALESCE (null) from dual
*
ERROR at line 1:
ORA-00938: not enough arguments for function
SQL> select COALESCE('a') from dual;
select COALESCE('a') from dual
*
ERROR at line 1:
ORA-00938: not enough arguments for function

NVL Function

NVL function lets you substitute a value when a null value is encountered.

Syntax

The syntax for the NVL function is:

NVL( string1, replace_with )

string1 is the string to test for a null value.

replace_with is the value returned if string1 is null.

NVL :-function evaluates whether a column or expression of any datatype is NULL or NOT NULL .
It takes two mandatory parameters. NVL(original,ifnull). Where original refers the term being tested
& ifnull is the result for orignal.

Syntax:- NVL(original,ifnull)

Ex:-

SELECT NVL ( NULL,123 ) FROM DUAL;

Result returned by this query will be '123' which is ifnull result for original NULL value.
----------------------------------
SELECT NVL ( 125,123 ) FROM DUAL;

Result returned by this query will be '125' which is original NOT NULL value.
----------------------------------

SELECT NVL ( NULL,' value not found' ) FROM DUAL;

Result returned by this query will be 'value not found' which is ifnull result for original NULL value.
-----------------------------------

SELECT NVL ( 'not null value','null value' ) FROM DUAL;

Result returned by this query will be 'not null value' which is original NOT NULL value.
-----------------------------------

SELECT NVL ( NULL,' 01-oct-1988' ) FROM DUAL;

Result returned by this query will be '01-oct-1988' which is ifnull result for original NULL value.
------------------------------------

SELECT NVL ( '23-mar-1984',' 01-oct-1988' ) FROM DUAL;

Result returned by this query will be '23-mar-1984' which is original NOT NULL value.

Nullif function:

NULLIF Function
NULLIF function compares expr1 and expr2. If expr1 and expr2 are equal, the NULLIF function returns
NULL. Otherwise, it returns expr1.

Syntax

The syntax for the NULLIF function is:

NULLIF( expr1, expr2 )

expr1 and expr2 must be either numeric values or values that are of the same datatype.

Note

expr1 can be an expression that evaluates to NULL, but it can not be the literal NULL.
For Example
NULLIF(12, 12) would return NULL

NULLIF(12, 13) would return 12

NULLIF('apples', 'apples') would return NULL

NULLIF('apples', 'oranges') would return 'apples'

NULLIF(NULL, 12) would return an ORA-00932 error


because
expr1 can not be the literal NULL

GROUP FUNCTIONS
SQL COUNT (): This function returns the number of rows in the table that satisfies the condition specified in
the WHERE condition. If the WHERE condition is not specified, then the query returns the total number of
rows in the table.

DISTINCT(): This function is used to select the distinct rows.

MAX(): This function is used to get the maximum value from a column.

MIN(): This function is used to get the minimum value from a column.

AVG(): This function is used to get the average value of a numeric column.

SUM(): This function is used to get the sum of a numeric column

STDDEV():stddev function returns the standard deviation of a set of numbers.

You might also like