0% found this document useful (0 votes)
63 views6 pages

Experiment-2: Quires Using Operators in SQL

This document discusses different types of operators used in SQL - arithmetic, comparison, and logical operators. It provides examples of each operator used to query data from sample tables including sailor, boat, and reserve tables. Arithmetic operators allow mathematical calculations on values. Comparison operators check for equality or other comparisons between values. Logical operators combine or reverse multiple conditions in a WHERE clause.
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)
63 views6 pages

Experiment-2: Quires Using Operators in SQL

This document discusses different types of operators used in SQL - arithmetic, comparison, and logical operators. It provides examples of each operator used to query data from sample tables including sailor, boat, and reserve tables. Arithmetic operators allow mathematical calculations on values. Comparison operators check for equality or other comparisons between values. Logical operators combine or reverse multiple conditions in a WHERE clause.
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/ 6

9

EXPERIMENT-2
Quires using Operators in SQL

Types of operators in SQL:


1.Arthematic Operators,
2.Comparison Operators
3.Logical Operators
1.Arthematic Operators:
a.Addtion Operator(+): Adds values on either side of the operator.
Eg consider the Sailor table

Write a query to display sum of rating and age from the above table
SQL> select rating+age from sailor;
Output:

b.Subraction Operator(-): Subtracts right hand operand from left hand operand.
Eg from the sailors table;
SQL>select age-rating from sailor;
Output:

c.Multiplication Operator(*):Multiplies values on either side of the operator.


Eg from sailors table
SQL>select SID* rating from sailor;
Output:

d.Division Operator(/): Divides left hand operand by right hand operand.

WWW.KVRSOFTWARES.BLOGSPOT.COM
10

Eg from the sailors table;


SQL>select rating/2 from sailor;
Output:

e.Modulus Operator(%): Divides left hand operand by right hand operand and returns
remainder.
Eg from the sailors table;
SQL>select rating%2 from sailor;
Output:

2.Comparison Operators:
a)Equal to Operator(=): Checks if the values of two operands are equal or not, if yes then
condition becomes true.
Eg from the above sailor table;
SQL>select SID from sailor where rating=5;
Output:

b)Not Equal to Operator(!=): Checks if the values of two operands are equal or not, if values
are not equal then condition becomes true.
Eg from above sailors table;
SQL> select SID from sailor where rating!=5;
Output:

WWW.KVRSOFTWARES.BLOGSPOT.COM
11

c) Grater than Operator(>): Checks if the value of left operand is greater than the value of
right operand, if yes then condition becomes true.
Eg from above sailors table;
SQL> select sname from sailor where rating>3;
Output:

d) Less than Operator(<) :Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
Eg from above sailors table;
SQL> select sname from sailor where rating<5;
Output:

e) Grater than or Equal to Operator(>=): Checks if the value of left operand is greater than
or equal to the value of right operand, if yes then condition becomes true.
Eg from above sailors table;
SQL> select sname from sailor where rating>=5;
Output:

f)Less than or Equal to Operator(<=): Checks if the value of left operand is less than or
equal to the value of right operand, if yes then condition becomes true.
Eg from above sailors table;
SQL> select sname from sailor where rating<=3;
Output:

g) Not Equal to Operator(<>): Checks if the values of two operands are equal or not, if
values are not equal then condition becomes true.
Eg from above sailors table;
SQL> select SID from sailor where rating<>5;
Output:

WWW.KVRSOFTWARES.BLOGSPOT.COM
12

3.Logical Operators:
Consider the sailor,boat ,reserve tables
Boat table

Reserve table

a)ALL Operator: The ALL operator is used to compare a value to all values in another value
set.
Eg to get not registered but reserved sailors from reserves table
SQL> select * from reserve where bid!=all(select bid from boat);
Output:

b)AND Operator: The ALL operator is used to compare a value to all values in another value
set.
Eg: to get registered sailors having 25 plus age and rating more than 4
SQL> select * from sailor where age>25 and rating>4;
Output:

c)ANY Operator: The ANY operator is used to compare a value to any applicable value in the
list as per the condition.
Eg:To get details of resrved sailors until now
SQL> select * from reserve where bid= any(select bid from boat);
Output:

WWW.KVRSOFTWARES.BLOGSPOT.COM
13

d)BETWEEN Operator: The BETWEEN operator is used to search for values that are within a
set of values, given the minimum value and the maximum value.
Eg: To get details of registered sailors having rating between 5 and 6
SQL> select * from sailor where rating between 5 and 6;
Output:

e) EXISTS Operator: The EXISTS operator is used to search for the presence of a row in a
specified table that meets a certain criterion.
Eg: To get the details of reserved sailors until now
SQL> select * from sailor where exists (select sid from reserve );
Output:

f) IN Operator: The IN operator is used to compare a value to a list of literal values that have
been specified.
Eg:To get the details of green and white colored boats
SQL> select * from boat where color in('green','white');
Output:

g) LIKE Operator: The LIKE operator is used to compare a value to similar values using
wildcard operators.
Eg:To get the details of sailors name starting with V
SQL> select * from sailor where sname like 'V%';
Output:

h) NOT Operator: The NOT operator reverses the meaning of the logical operator with which it
is used. Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc.
Eg: : To get details of registered sailors not having rating between 5 and 6
SQL> select * from sailor where rating not between 5 and 6;

WWW.KVRSOFTWARES.BLOGSPOT.COM
14

Output:

i) OR Operator: The OR operator is used to combine multiple conditions in an SQL statement's


WHERE clause.
Eg: : To get details of sailors whose sid>10 or rating>4
SQL> select * from sailor where sid>11 or rating >4;
Output:

WWW.KVRSOFTWARES.BLOGSPOT.COM

You might also like