DBMS Lab 4
DBMS Lab 4
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
a + b willgive 30
+ Addition - Adds values on either side of the operator
a - b willgive -10
- Subtraction - Subtracts right hand operand from left hand operand
a * b willgive 200
* Multiplication - Multiplies values on either side of the operator
b / a will give 2
/ Division - Divides left hand operand by right hand operand
b % a will give 0
% Modulus - Divides left hand operand by right hand operand and returns remainder
Here are simple examples showing usage of SQL Arithmetic Operators:
SQL> select 10+ 20;
+ +
| 10+ 20 |
+ +
| 30 |
+ +
1 row in set (0.00 sec)
SQL> select 10 / 5;
+ +
| 10 / 5 |
+ +
| 2.0000 |
+ +
1 row in set (0.03 sec)
SQL> select 12 % 5;
+ +
| 12 % 5 |
+ +
| 2 |
+ +
1 row in set (0.00 sec)
(a = b) isnot
= Checks if the values of two operands are equal or not, if yes then condition becomes true.
true.
Checks if the values of two operands are equal or not, if values are not equal then (a != b)is true.
!=
condition becomes true.
Checks if the values of two operands are equal or not, if values are not equal then (a <> b)is true.
<>
condition becomes true.
Checks if the value of left operand is greater than the value of right operand, if yes then (a > b) isnot
>
condition becomes true. true.
Checks if the value of left operand is less than the value of right operand, if yes then (a < b) istrue.
<
condition becomes true.
Checks if the value of left operand is not less than the value of right operand, if yes then (a !< b)
!<
condition becomes true. is false.
Checks if the value of left operand is not greater than the value of right operand, if yes (a !> b)
!>
then condition becomes true. is true.
Q.1 Select all customers details from CUSTOMERS table where salary is greater than 5000
Q.2 Select customers details from CUSTOMERS table where salary =2000
Q.3 Select customers details from CUSTOMERS table where salary is not equal to 2000
Q.4 Select customers details from CUSTOMERS table where salary is less than 2000
ALL The ALL operator is used to compare a value to all values in another value set.
AND The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.
The ANY operator is used to compare a value to any applicable value in the list according to the
ANY
condition.
The BETWEEN operator is used to search for values that are within a set of values, given the
BETWEEN
minimum value and the maximum value.
The EXISTS operator is used to search for the presence of a row in a specified table that meets
EXISTS
certain criteria.
IN The IN operator is used to compare a value to a list of literal values that have been specified.
LIKE The LIKE operator is used to compare a value to similar values using wildcard operators.
The NOT operator reverses the meaning of the logical operator with which it is used. Eg: NOT
NOT
EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
OR The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
IS NULL The NULL operator is used to compare a value with a NULL value.
UNIQUE The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).
+ + + + + +
7 rows in set (0.00 sec)
Q. 1 Select all customers details from CUSTOMERS table where SALARY is greater than
6500 AND AGE >=25.
Q.2 Select all customers details from CUSTOMERS table where SALARY is greater than
6500 OR AGE >=25.
Q.3 Select all customers FROM CUSTOMERS WHERE AGE IS NOT NULL.
Q.4 Select all customers FROM CUSTOMERS WHERE AGE IS LIKE ‘KO%’.
Q.5 Select all customers FROM CUSTOMERS WHERE AGE IN
You can use WHERE clause with UPDATE query to update selected rows, otherwise
all the rows would be affected.
Syntax:
The basic syntax of UPDATE query with WHERE clause is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2....,
columnN = valueN WHERE [condition];
You can combine N number of conditions using AND or OR operators.
Example:
Consider the CUSTOMERS table having the following records:
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +
SQL> UPDATE
CUSTOMERS SET
ADDRESS =
'Pune' WHERE ID
= 6;