0% found this document useful (0 votes)
73 views5 pages

DBMS Lab 4

The document discusses different types of operators in SQL including arithmetic, logical, comparison, and special operators. It provides examples of using each type of operator and sample queries on a CUSTOMERS table to showcase their usage.
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)
73 views5 pages

DBMS Lab 4

The document discusses different types of operators in SQL including arithmetic, logical, comparison, and special operators. It provides examples of using each type of operator and sample queries on a CUSTOMERS table to showcase their usage.
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/ 5

Assignment_4

Implementation of different types of operators in SQL


 Arithmetic Operators
 Logical Operators
 Comparison Operator
 Special Operator
 Set Operation

What is an Operator in SQL?

A n operator is a reserved word or a character used primarily in an SQL statement's

WHERE clause to perform operation(s), such as comparisons and arithmetic operations.

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

 Operators used to negate conditions

SQL Arithmetic Operators:


Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example

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 * 20;


+ +
| 10 * 20 |
+ +
| 200 |
+ +
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)

SQL Comparison Operators:


Assume variable a holds 10 and variable b holds 20, then:
Operator Description Example

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

(a >= b)is not


Checks if the value of left operand is greater than or equal to the value of right operand, if
>= true.
yes then condition becomes true.
Checks if the value of left operand is less than or equal to the value of right operand, if (a <= b)
<=
yes then condition becomes true. is 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.

Consider Following Table CUSTOMERS to solve below Queries .

SQL> SELECT * FROM CUSTOMERS;


+ + + + + +
| 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 |
+ + + + + +
7 rows in set (0.00 sec)

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

SQL Logical Operators:


Operator Description

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

Here is a list of all the logical operators available in SQL.

Consider the CUSTOMERS table having the following records:


| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi
| 1500.00 |
| 3 | kaushik | 23 | Kota
| 2000.00 |
| 4 | Chaitali | 25 | Mumbai
| 6500.00 |
| 8500.00 |
| 5 | Hardik | 27 | Bhopal | 4500.00 |
| 10000.00 |
| 6 | Komal | 22 | MP
| 7 | Muffy | 24 | Indore

+ + + + + +
7 rows in set (0.00 sec)

Perform following SQL queries using Comparison Operators on Customers table

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

SQL UPDATE Query

T he SQL UPDATE Query is used to modify the existing records in a table.

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 |
+ + + + + +

Following is an example, which would update ADDRESS for a customer


whose ID is 6:

SQL> UPDATE
CUSTOMERS SET
ADDRESS =
'Pune' WHERE ID
= 6;

Q. 1 Update CUSTOMERS table to modify all ADDRESS and SALARY


column values in
SET all ADDRESS to ‘Pune’ and SALARY to 1000

You might also like