Rdbms Unit 3
Rdbms Unit 3
Unit III
Operators & SQL Functions & Views
Types of Operators
•As in any other programming language, SQL & PL/SQL has a set of operators. The operators can
be classified as follows:
Arithmetic operators
Relational / Comparison operators
Logical operators
Operator Purpose
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
Example –
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SUMIT NANDED
4 AMARJEET PUNE
Syntax –
WHERE condition1 AND condition2 ... AND condition_n;
Example – Display the students, whose name is SUMIT and city is NANDED.
RN NAME CITY
3 SUMIT NANDED
This example would return all students that have a name SUMIT and reside in the
NANDED city. Because the * is used in the SELECT statement, all fields from the STUDENT
table would appear in the result set.
2. The OR Operator –
The OR operator allows creating an SQL statement where records are returned when any of the
conditions are met. The OR operator is used in any valid SQL statement such as SELECT, UPDATE, or
DELETE statement.
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
The OR operator requires that any of the conditions must be met for the record to be included
in the result set.
Syntax –
WHERE condition1 OR condition2 ... OR condition_n;
RN NAME CITY
2 AMAR LATUR
3 SUMIT NANDED
This example would return all students that have a name AMAR or reside in the NANDED
city. Because the * is used in the SELECT statement, all fields from the STUDENT table
would appear in the result set.
Syntax –
NOT condition
Example – List the student details that are not in NANDED city.
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
4 AMARJEET PUNE
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
* Comparison Operators –
Comparison operators are used in the WHERE clause to determine which records to select.
Oracle has several comparison operators, some of them are listed below: a) LIKE
b) BETWEEN
c) IS NULL
d) IN
a) LIKE Operator –
The LIKE operator allows wildcards (% and _) to be used in the WHERE clause of a SELECT,
UPDATE, or DELETE statement. This allows you to perform pattern matching.
% (Percentage) – allows matching any string of any length.
_ (Underscore) – allows matching on a single character.
Example –
1) List the students, whose names begin with the letter ‘A’.
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
4 AMARJEET PUNE
b) BETWEEN Operator –
The BETWEEN operator is used to retrieve values within a range in a SELECT, UPDATE, or DELETE
statement.
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
RN NAME CITY
2 AMAR LATUR
3 SUMIT NANDED
4 AMARJEET PUNE
This example would return all rows from the STUDENT table where the RN is between 2
and 4 (inclusive). It is equivalent to the following SELECT statement:
RN NAME CITY
1 AMOL LATUR
This example would return all rows from the STUDENT table where the RN is NOT
between 2 and 4 (inclusive).
c) IS NULL Operator –
The IS NULL operator is used to test for a NULL value.
Syntax – column_name IS
NULL
Example –
1) SELECT * FROM student WHERE NAME IS NULL; no rows selected.
It will return all records from the STUDENT table where the NAME contains a null value.
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SUMIT NANDED
4 AMARJEET PUNE
It will return all records from the STUDENT table where the NAME doesn’t contain a null value.
d) IN Operator –
The IN operator is used to help reduce the need to use multiple OR conditions in a SELECT,
UPDATE, or DELETE statement.
Example –
1) List the student details of the students named AMOL, AMAR, and SUMIT.
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SUMIT NANDED
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
2) List the student details of the students other than AMOL, AMAR, and SUMIT.
RN NAME CITY
4 AMARJEET PUNE
SQL Functions :-
Oracle SQL supplies a rich library of in-built functions which can be employed for various tasks.
There are two types of functions,
* Single Row Functions
* Multiple Row Functions
1) ASCII Function –
The ASCII function returns the NUMBER code that represents the specified character. It accepts
only single character at a time.
Syntax –
ASCII (single_character)
Example –
SELECT ASCII ('A') AS “ASCII” FROM DUAL;
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
ASCII
----------
65
2) CHR Function –
The CHR function is the opposite of the ASCII function. It returns the character based on the
NUMBER code.
Syntax –
CHR (number_code)
Example –
SELECT CHR (65) AS “CHARACTER” FROM DUAL;
C
-
A
3) LENGTH Function –
The LENGTH function returns the length of the specified string.
Syntax –
LENGTH (string)
Example –
SELECT LENGTH ('INDIA') AS LENGTH FROM DUAL;
LENGTH
-----------
5
4) LOWER Function –
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 –
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
LOWER (string)
Example –
SELECT LOWER ('INDIA') AS “SMALL” FROM DUAL;
SMALL
----- india
5) UPPER Function –
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 –
UPPER (string)
Example –
SELECT UPPER ('india') AS “UPPER” FROM DUAL;
UPPER
-----
INDIA
b) NUMERIC FUNCTIONS –
These are functions that accept numeric input and return numeric values.
Syntax –
ABS (number)
Example –
SELECT ABS (-23) AS "ABS" FROM DUAL;
ABS
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
----------
23
Syntax – exp
(number)
Example –
SELECT EXP (2) AS “EXP” FROM DUAL;
EXP
----------
7.3890561
3) SQRT Function –
The SQRT function returns the square root of n.
Syntax – SQRT
(n)
Example –
SELECT SQRT (100) AS “SQRT” FROM DUAL;
SQRT
----------
10
4) GREATEST Function –
The GREATEST function returns the greatest value in a list of expressions.
Syntax –
GREATEST (expr1 [, expr2, ... expr_n])
Example –
a) SELECT GREATEST (2, 5, 12) AS “LARGER” FROM DUAL;
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
LARGER
----------
12
5) LEAST Function –
The LEAST function returns the smallest value in a list of expressions.
Syntax –
LEAST (expr1 [, expr2, ... expr_n])
Example –
a) SELECT LEAST (2, 5, 12) AS “SMALLER” FROM DUAL;
SMALLER
----------
2
b) SELECT LEAST ('A', 'B', 'C') AS “SMALLER” FROM DUAL;
L
-
A
c) 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.
1) ADD_MONTHS Function –
The ADD_MONTHS function returns a date with a specified number of months added.
Syntax –
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
Example –
a) SELECT ADD_MONTHS ('04-AUG-17', 3) FROM DUAL;
ADD_MONTH
---------
04-NOV-17
b) SELECT ADD_MONTHS ('04-AUG-17', -3) FROM DUAL;
ADD_MONTH
---------
04-MAY-17
2) EXTRACT Function –
The EXTRACT function extracts a value from a date.
Syntax –
EXTRACT ({YEAR | MONTH | DAY | HOUR | MINUTE | SECOND} FROM DATE date_value)
Example –
3) LAST_DAY Function –
The LAST_DAY function returns the last day of the month based on a date value.
Syntax –
LAST_DAY (date)
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
Example –
SELECT LAST_DAY ('03-AUG-85') AS "LAST DAY" FROM DUAL;
LAST DAY
---------
31-AUG-85
4) MONTHS_BETWEEN Function –
The MONTHS_BETWEEN function returns the number of months between date1 and date2.
Syntax –
MONTHS_BETWEEN (date1, date2)
Example –
SELECT MONTHS_BETWEEN ('03-AUG-17', '03-MAY-17') AS "MONTHS BETWEEN" FROM DUAL;
MONTHS BETWEEN
--------------
3
5) SYSDATE Function –
The SYSDATE function returns the current system date and time on your local database.
Syntax –
SYSDATE
Example –
SELECT SYSDATE AS “NOW” FROM DUAL;
NOW
---------
07-AUG-17
d) 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.
1) CAST Function –
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
Syntax –
CAST (expr AS type_name)
Example –
SELECT CAST ('07-AUG-2017' AS VARCHAR2 (15)) AS "CONVERSION" FROM DUAL;
CONVERSION
---------------
07-AUG-2017
This would convert the date (i.e. 07-AUG-2017) into a varchar2 (15) value.
3) TO_CHAR Function –
The TO_CHAR function converts a number or date to a string.
Syntax –
TO_CHAR (value, format_string)
Example –
a) SELECT TO_CHAR (123.456, '000.00') AS "CONVERT" FROM DUAL;
CONVERT
----------
123.46
4) TO_DATE Function –
The TO_DATE function converts a string to a date.
Syntax –
TO_DATE (string, format_mask)
Example –
SELECT TO_DATE ('2017/08/07', 'yyyy/mm/dd') AS "CONVERT" FROM DUAL;
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
CONVERT
---------
07-AUG-17
5) TO_NUMBER Function –
The TO_NUMBER function converts a string to a number.
Syntax –
TO_NUMBER (string)
Example –
SELECT TO_NUMBER ('123') AS "CONVERT" FROM DUAL;
CONVERT
----------
123
Example –
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
4 AMARJEET 25000
1) COUNT Function –
The COUNT function returns the count of an expression.
Syntax –
SELECT COUNT (expression) FROM table [WHERE condition(s)];
The COUNT function will only include the records in the count where the value of expression in
COUNT (expression) is NOT NULL. When expression contains a NULL value, it is not included in the
COUNT calculations.
Example –
a) SELECT COUNT (NAME) AS "COUNT" FROM STUDENT;
COUNT
----------
4
This COUNT example will return 4 since all NAME values in the query's result set are NOT NULL.
2) SUM Function –
The SUM function returns the summed value of an expression.
Syntax –
SELECT SUM (expression) FROM table [WHERE condition(s)];
Example –
a) Calculate the total fee, which was collected from students.
TOTAL FEES
----------
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
70000
3) AVG Function –
The AVG function returns the average value of an expression.
Syntax –
SELECT AVG (expression) FROM table [WHERE condition(s)];
Example –
a) Calculate the average fee of all the students.
4) MIN Function –
The MIN function returns the minimum value of an expression.
Syntax –
SELECT MIN (expression) FROM table [WHERE condition(s)];
Example –
a) Find out the minimum fee of all students.
MINIMUM FEES
------------
10000
5) MAX Function –
The MAX function returns the maximum value of an expression.
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
Syntax –
SELECT MAX (expression) FROM table [WHERE condition(s)];
Example –
a) Find out the maximum fee of all students.
MAXIMUM FEES
------------
25000
RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 RAHUL NANDED
a) Create VIEW –
Use the CREATE VIEW command to create a view.
Syntax –
CREATE VIEW view_name AS
SELECT column1, column2, … FROM tbl_name
[WHERE condition(s)];
Example –
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
This example would create a VIEW (virtual table) vw_stud_1 with RN and NAME fields. You can
now display the result of VIEW as follows:
RN NAME
1 AMOL
2 ATUL
3 RAHUL
This example would create a VIEW (virtual table) vw_stud_2 with different column names
ROLL_NO, STUD_NAME and LOCATION for RN, NAME and CITY. You can now display the result
of VIEW as follows:
b) Update VIEW –
You can modify the definition of a VIEW without dropping it by using the CREATE OR REPLACE
VIEW Statement.
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
Syntax –
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, … FROM tbl_name
[WHERE condition(s)];
Example –
CREATE or REPLACE VIEW vw_stud_3 AS
SELECT * FROM student WHERE RN = 2;
View created.
This example would update the definition of the VIEW called vw_stud_3 without dropping it. If
the VIEW did not yet exist, the VIEW would merely be created for the first time.
RN NAME CITY
2 ATUL LATUR
c) Drop VIEW –
Once a VIEW has been created, you can drop it with the DROP VIEW Statement.
Syntax –
DROP VIEW view_name;
Example –
1) DROP VIEW vw_stud_1;
View dropped.
By:ChimleM.D.
BCA SY Operators & SQL Functions & Views
By:ChimleM.D.