Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Rdbms unit 3 notes

The document provides an overview of SQL operators, functions, and views, detailing various types of operators including arithmetic, relational, logical, and comparison operators, along with examples of their usage. It also covers SQL functions, categorizing them into single row functions such as character, numeric, date, and conversion functions, and provides syntax and examples for each. Prepared by Mr. V. D. Patil from COCSIT, Latur, the document serves as a guide for understanding SQL operations.

Uploaded by

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

Rdbms unit 3 notes

The document provides an overview of SQL operators, functions, and views, detailing various types of operators including arithmetic, relational, logical, and comparison operators, along with examples of their usage. It also covers SQL functions, categorizing them into single row functions such as character, numeric, date, and conversion functions, and provides syntax and examples for each. Prepared by Mr. V. D. Patil from COCSIT, Latur, the document serves as a guide for understanding SQL operations.

Uploaded by

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

B Sc CS 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.

SQL> SELECT EMPNO, ENAME, SAL + 1000 FROM EMP;


SQL> SELECT EMPNO, ENAME, SAL - 1000 FROM EMP;
SQL> SELECT EMPNO, ENAME, SAL * 10 FROM EMP;
SQL> SELECT EMPNO, ENAME, SAL /2 FROM EMP;

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

SQL Relational Operators


Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to

SQL> SELECT * FROM EMP WHERE SAL=10000;


SQL> SELECT * FROM EMP WHERE SAL>10000;
SQL> SELECT * FROM EMP WHERE SAL<10000;
SQL> SELECT * FROM EMP WHERE SAL>=10000;
SQL> SELECT * FROM EMP WHERE SAL<=10000;
SQL> SELECT * FROM EMP WHERE SAL<>10000;

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

* 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 –

SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
3 SAMEER 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.

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

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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;

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

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

RN NAME CITY
2 AMAR LATUR
3 SAMEER NANDED

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.

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

RN NAME CITY
1 AMOL LATUR
2 AMAR LATUR
4 AMARJEET PUNE

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS 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’.

SQL> 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;

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

Example –
1) List all the students, whose roll number is 2 to 4.

SQL> SELECT * FROM student WHERE RN BETWEEN 2 AND 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:

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

SQL> 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 –
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.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

SQL> SELECT * FROM student WHERE NAME IS NOT NULL;

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.

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

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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 –
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)

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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.

1) ABS (Absolute) Function –


The ABS function returns the absolute value of a number.

Syntax –
ABS (number)

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

ABS
----------
23

2) EXP (Exponential) Function –


The EXP function returns e raised to the nth power (en), where e = 2.71828183.
Syntax –
exp (number)
Example –
SQL> SELECT EXP (2) AS “EXP” FROM DUAL;

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 –

SQL> SELECT EXTRACT (YEAR FROM DATE '2024-07-11') AS "YEAR",


EXTRACT (MONTH FROM DATE '2024-07-11') AS "MONTH",
EXTRACT (DAY FROM DATE '2024-07-11') AS "DAY"
FROM DUAL;

YEAR MONTH DAY


---------- ---------- ----------
2024 7 11

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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)

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

* 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 –

SQL> SELECT * FROM STUDENT;

RN NAME CITY FEES


1 AMOL LATUR 10000
2 ATUL PUNE 15000
3 RAHUL LATUR 20000
4 AMIR MUMBAI 25000

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)];

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

Example –
Calculate the total fee, which was collected from students.

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

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.

SQL> 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 –
Find out the minimum fee of all students.

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

MINIMUM FEES
------------
10000

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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.

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

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

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

* 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.

SQL> SELECT * FROM student;

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:

SQL> SELECT * FROM vw_stud_1;

RN NAME
1 AMOL
2 AMIR
3 RAHUL

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

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.

SELECT * FROM vw_stud_3;

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.

This example would drop/delete the VIEW called vw_stud_2.

SQL> DROP VIEW vw_stud_2;


View dropped.
Prepared by, Mr. V. D. Patil
COCSIT, Latur
B Sc CS SY Operators & SQL Functions & Views

Prepared by, Mr. V. D. Patil


COCSIT, Latur

You might also like