SQL Operators

An 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.
Types of SQL Operators:

  1. Arithmetic operators
  2. Comparison operators
  3. Logical operators

Operators used to negate conditions

SQL Arithmetic Operators:[+ , – , * , /]

Example:
Assume A=10 AND B=20, then:

> select a+b

30

>select a-b

-10

> select a*b

200

SQL Logical Operators:[ALL, LIKE, AND, ANY, BETWEEN, EXIST, IN, NOT, OR, IS NULL,UNIQUE]

Example:

Lets make  an employee table

ID NAME AGE ADDRESS SALARY
1 Shankar 21 Alwar 56000
2 Raaj 22 Alwwar 23651
3 Faiyyaz 28 Alwar 30000
4 Alam 25 Alwar 35465
5 Ajay 23 Alwar 20000
6 Aakash 22 Alwar 56078
7 Shailesh 26 Alwar 45000

Here in this Employee Table we will perform all the Logical Operator conditions

> SELECT * FROM CUSTOMERS WHERE AGE >= 25 AND SALARY >= 35500;
> SELECT * FROM CUSTOMERS WHERE AGE >= 25 OR SALARY >= 10000;
> SELECT * FROM CUSTOMERS WHERE AGE IS NOT NULL;
> SELECT * FROM CUSTOMERS WHERE NAME LIKE 'A%';
> SELECT * FROM CUSTOMERS WHERE AGE IN ( 25, 28 );
> SELECT * FROM CUSTOMERS WHERE AGE BETWEEN 25 AND 30;
> SELECT AGE FROM CUSTOMERS 
  WHERE EXISTS (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
> SELECT * FROM CUSTOMERS 
  WHERE AGE > ALL (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);
> SELECT * FROM CUSTOMERS 
  WHERE AGE > ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);


SQL COMPARISION OPERATOR:[=, !=, <>, >, <, >=, <=, !<, !>]

= : VALUES ARE EQUAL.
!= : VALUES ARE NOT EQUAL.
<> : VALUES OF TWO OPERANDS ARE EQUAL OR NOT, IF VALUES ARE NOT EQUAL THEN       CONDITION BECOMES TRUE.
> : LEFT VALUE GREATER THAN RIGHT VALUE.
< : LEFT VALUE LESS THAN RIGHT VALUE.
>= : LEFT VALUE GREATER OR EQUAL TO THE RIGHT VALUE.
=< : LEFT VALUE IS SMALLER OR EQUAL TO RIGHT VALUE.
!< : CHECK LEFT VALUE IS NOT LESS THAN RIGHT VALUE, IF NOT LESS THEN CONDITION BECOMES TRUE.
!> : CHECK LEFT VALUE IS NOT LARGER THAN RIGHT VALUE, IF NOT LARGE THEN CONDITION BECOMES TRUE.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s