Rdbms unit 3 notes
Rdbms unit 3 notes
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
+ Addition: Use to add two data items or expressions.
* Logical Operators –
A logical operator combines the results of two component conditions to produce a single result
based on them or to invert the result of a single condition. Oracle has three logical operators:
1. AND
2. OR
3. NOT
Example –
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SAMEER NANDED
4 AMARJEET PUNE
Syntax –
WHERE condition1 AND condition2 ... AND condition_n;
Example – Display the students, whose name is SUMIT and city is NANDED.
SQL> SELECT * FROM student WHERE NAME = ‘SAMEER’ AND CITY =’NANDED’;
RN NAME CITY
3 SAMEER NANDED
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.
Syntax –
WHERE condition1 OR condition2 ... OR condition_n;
RN NAME CITY
2 AMAR LATUR
3 SAMEER NANDED
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
* 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.
Syntax –
column_name LIKE pattern;
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.
Syntax –
column_name BETWEEN value1 AND value2;
Example –
1) List all the students, whose roll number is 2 to 4.
RN NAME CITY
2 AMAR LATUR
3 SAMEER 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 –
SQL> 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.
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SAMEER 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.
Syntax –
column_name IN (value1, value2, ... value_n);
Example –
1) List the student details of the students named AMOL, AMAR, and SUMIT.
RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SAMEER NANDED
2) List the student details of the students other than AMOL, AMAR, and SUMIT.
SQL> SELECT * FROM student WHERE NAME NOT IN (‘AMOL’, ‘AMAR’, ‘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 –
SQL> SELECT ASCII ('A') AS “ASCII” FROM DUAL;
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 –
SQL> 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 –
SQL> 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 –
LOWER (string)
Example –
SQL> 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 –
SQL> 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 –
SQL> SELECT ABS (-23) AS "ABS" FROM DUAL;
ABS
----------
23
EXP
----------
7.3890561
Prepared by, Mr. V. D. Patil
COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views
3) SQRT Function –
The SQRT function returns the square root of n.
Syntax –
SQRT (n)
Example –
SQL> 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 –
SQL> SELECT GREATEST (2, 5, 12) AS “LARGER” FROM DUAL;
LARGER
----------
12
5) LEAST Function –
The LEAST function returns the smallest value in a list of expressions.
Syntax –
LEAST (expr1 [, expr2, ... expr_n])
Example –
SQL> SELECT LEAST (2, 5, 12) AS “SMALLER” FROM DUAL;
SMALLER
----------
2
Prepared by, Mr. V. D. Patil
COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views
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 –
ADD_MONTHS (date1, number_months)
Example –
SQL> SELECT ADD_MONTHS ('04-AUG-24', 3) FROM DUAL;
ADD_MONTH
---------
04-NOV-24
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)
Example –
SQL> SELECT LAST_DAY ('03-JAN-25') AS "LAST DAY" FROM DUAL;
LAST DAY
---------
31-JAN-25
4) MONTHS_BETWEEN Function –
The MONTHS_BETWEEN function returns the number of months between date1 and date2.
Syntax –
MONTHS_BETWEEN (date1, date2)
Example –
SQL> SELECT MONTHS_BETWEEN ('03-AUG-24', '03-MAY-24') 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 –
SQL> SELECT SYSDATE AS “NOW” FROM DUAL;
NOW
---------
07-FEB-2025
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 –
The CAST function converts one datatype to another.
Syntax –
CAST (expr AS type_name)
Example –
SQL> SELECT CAST ('07-FEB-2025' AS VARCHAR2 (15)) AS "CONVERSION" FROM DUAL;
CONVERSION
---------------
07-FEB-2025
This would convert the date (i.e. 07-FEB-2025) into a varchar2 (15) value.
2) TO_CHAR Function –
The TO_CHAR function converts a number or date to a string.
Syntax –
TO_CHAR (value, format_string)
Example –
SQL> SELECT TO_CHAR (123.456, '000.00') AS "CONVERT" FROM DUAL;
CONVERT
----------
123.46
3) TO_DATE Function –
The TO_DATE function converts a string to a date.
Syntax –
TO_DATE (string, format_mask)
Example –
SQL> SELECT TO_DATE ('2025/02/07', 'yyyy/mm/dd') AS "CONVERT" FROM DUAL;
CONVERT
---------
07-FEB-25
4) TO_NUMBER Function –
The TO_NUMBER function converts a string to a number.
Syntax –
TO_NUMBER (string)
Example –
SQL> SELECT TO_NUMBER ('123') AS "CONVERT" FROM DUAL;
CONVERT
----------
123
Example –
1) COUNT Function –
The COUNT function returns the count of an expression.
Syntax –
SELECT COUNT (expression) FROM table [WHERE condition(s)];
Example –
SQL> 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 –
Calculate the total fee, which was collected from students.
TOTAL FEES
----------
70000
3) AVG Function –
The AVG function returns the average value of an expression.
Syntax –
SELECT AVG (expression) FROM table [WHERE condition(s)];
Example –
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 –
Find out the minimum fee of all students.
MINIMUM FEES
------------
10000
5) MAX Function –
The MAX function returns the maximum value of an expression.
Syntax –
SELECT MAX (expression) FROM table [WHERE condition(s)];
Example –
Find out the maximum fee of all students.
MAXIMUM FEES
------------
25000
RN NAME CITY
1 AMOL LATUR
2 AMIR 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 –
SQL> CREATE VIEW vw_stud_1 AS SELECT RN, NAME FROM student;
View created.
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 AMIR
3 RAHUL
b) Update VIEW –
You can modify the definition of a VIEW without dropping it by using the CREATE OR REPLACE
VIEW Statement.
Syntax –
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, … FROM tbl_name
[WHERE condition(s)];
Example –
SQL> CREATE or REPLACE VIEW vw_stud_2
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 AMIR 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 –
SQL> DROP VIEW vw_stud_1;
View dropped.