Experiment-2: Quires Using Operators in SQL
Experiment-2: Quires Using Operators in SQL
EXPERIMENT-2
Quires using Operators in SQL
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:
WWW.KVRSOFTWARES.BLOGSPOT.COM
10
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:
WWW.KVRSOFTWARES.BLOGSPOT.COM