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

Rdbms Unit 3

Uploaded by

shaikhsubuktgin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Rdbms Unit 3

Uploaded by

shaikhsubuktgin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

BCA SY Operators & SQL Functions & Views

Unit III
Operators & SQL Functions & Views

What are Operators ?


•An operator manipulates individual data items and returns a result. The data items are called
operands or arguments.
•Operators are represented by special characters or by keywords.

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

Using Arithmetic Operators


•We can use an arithmetic operator with one or two arguments to negate, add, subtract,
multiply, & divide numeric values.

Operator Purpose

+ Addition: Use to add two data items or expressions.

- Subtraction: Use to find the difference


between two data items or expressions.
/ Multiplication: Use to multiply two data items or
expressions.
* Division: Use to divide a data item or expression with another.

SELECT EMPNO, ENAME, SAL + 1000 FROM EMP;


SELECT EMPNO, ENAME, SAL - 1000 FROM EMP;

By:ChimleM.D.
BCA SY Operators & SQL Functions & Views

SELECT EMPNO, ENAME, SAL * 10 FROM EMP; SELECT


EMPNO, ENAME, SAL /2 FROM EMP;

SQL Relational Operators


Operator Description Example
= Equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

<> Not equal to

SELECT EMPNO, ENAME, SAL FROM EMP Where SAL=1000;


SELECT EMPNO, ENAME, SAL FROM EMP Where SAL>1000;
SELECT EMPNO, ENAME, SAL FROM EMP Where SAL<1000;
SELECT EMPNO, ENAME, SAL FROM EMP Where SAL>=1000;
SELECT EMPNO, ENAME, SAL FROM EMP Where SAL<=1000;
SELECT EMPNO, ENAME, SAL FROM EMP Where SAL<>1000; *
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

By:ChimleM.D.
BCA SY Operators & SQL Functions & Views

Example –

SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SUMIT NANDED
4 AMARJEET PUNE

1. The AND Operator –


The AND operator allows creating an SQL statement based on two or more conditions being met.
The AND Operator is used in any valid SQL statement such as SELECT, UPDATE, or DELETE statement.
The AND operator requires that each condition must be met for the record to be included in
the result set.

Syntax –
WHERE condition1 AND condition2 ... AND condition_n;

Example – Display the students, whose name is SUMIT and city is NANDED.

SELECT * FROM student WHERE NAME = ‘SUMIT’ AND CITY =’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;

Example – Display the students, whose name is AMAR or city is NANDED.

SELECT * FROM student WHERE NAME = ‘AMAR’ OR CITY =’NANDED’

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.

3. The NOT Operator –


The NOT operator display only those records that do not satisfy the condition specified. The NOT
operator is used to negate a condition in a SELECT, UPDATE, or DELETE statement.

Syntax –
NOT condition

Example – List the student details that are not in NANDED city.

SELECT * FROM student WHERE NOT CITY = ‘NANDED’;

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.

Syntax – column_name LIKE


pattern;

Example –
1) List the students, whose names begin with the letter ‘A’.

SELECT * FROM student WHERE NAME LIKE ‘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.

By:ChimleM.D.
BCA SY Operators & SQL Functions & Views

SELECT * FROM student WHERE RN BETWEEN 2 AND 4;

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:

2) List all the students, whose roll number is not 2 to 4.

SELECT * FROM student WHERE RN NOT BETWEEN 2 AND 4;

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

2) SELECT * FROM student WHERE NAME IS NOT NULL;

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.

Syntax – column_name IN (value1, value2, ...


value_n);

Example –
1) List the student details of the students named AMOL, AMAR, and SUMIT.

SELECT * FROM student WHERE NAME IN (‘AMOL’, ‘AMAR’, ‘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.

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

* Single Row Functions –


Single row or scalar functions return a value for every row that is processed in a query. Single
row functions can be,
a) Character Functions
b) Numeric Functions
c) Date Functions
d) Conversion Functions

a) CHARACTER or STRING or TEXT FUNCTIONS –


Character or string 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.

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.

1) ABS (Absolute) Function –


The ABS function returns the absolute value of a number.

Syntax –
ABS (number)

Example –
SELECT ABS (-23) AS "ABS" FROM DUAL;

ABS

By:ChimleM.D.
BCA SY Operators & SQL Functions & Views

----------
23

2) EXP (Exponential) Function –


The EXP function returns e raised to the nth power (en), where e = 2.71828183.

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

b) SELECT GREATEST ('A', 'B', 'C') AS “LARGER” FROM DUAL;


L
-
C

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

ADD_MONTHS (date1, number_months)

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 –

a) SELECT EXTRACT (YEAR FROM DATE '1985-09-11') AS "YEAR",


EXTRACT (MONTH FROM DATE '1985-09-11') AS "MONTH",
EXTRACT (DAY FROM DATE '1985-09-11') AS "DAY"
FROM DUAL;

YEAR MONTH DAY


---------- ---------- ----------
1985 9 11

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

The CAST function converts one datatype to another.

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

* Multiple Row functions –


Multiple row functions work upon group of rows and return one result for the complete set of
rows. They are also known as group functions or aggregate functions.

Example –

SELECT * FROM STUDENT;

RN NAME CITY FEES


1 AMOL LATUR 10000
2 ATUL 15000
3 RAHUL LATUR 20000

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.

SELECT SUM (FEES) AS "TOTAL FEES" FROM STUDENT;

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.

SELECT AVG (FEES) AS "AVERAGE FEES" FROM STUDENT;


AVERAGE FEES
------------
17500

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.

SELECT MIN (FEES) AS "MINIMUM FEES" FROM STUDENT;

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.

SELECT MAX (FEES) AS "MAXIMUM FEES" FROM STUDENT;

MAXIMUM FEES
------------
25000

* Working with Views –


A VIEW is a virtual table that does not physically exist. Rather, it is created by a query joining one
or more tables. When you update record(s) in a VIEW, it updates the records in the underlying tables
that make up the View and vice-versa.

SELECT * FROM student;

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

1) 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:

SELECT * FROM vw_stud_1;

RN NAME
1 AMOL
2 ATUL
3 RAHUL

2) CREATE VIEW vw_stud_2 (ROLLL_NO, STUD_NAME,


LOCATION) AS SELECT RN, NAME, CITY FROM student; View
created.

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:

SELECT * FROM vw_stud_2;

ROLL_NO STUD_NAME LOCATION


1 AMOL LATUR
2 ATUL LATUR
3 RAHUL NANDED

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.

SELECT * FROM vw_stud_3;

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.

This example would drop/delete the VIEW called vw_stud_1.

2) DROP VIEW vw_stud_2;


View dropped.

This example would drop/delete the VIEW called vw_stud_2.

By:ChimleM.D.
BCA SY Operators & SQL Functions & Views

By:ChimleM.D.

You might also like