You are here

Logical Operator

logical operators

LOGICAL OPERATORS

In this post we will discuss logical operators use in predicates like OR, AND, NOT.

  • The operators used in the predicates are Logical operators AND, OR, NOT are used for combining predicates in the where clause.

AND operator:

  • The AND operator allows SQL statement to filter data based on one or more conditions combined using AND operator.
  • AND operator allows creating an SQL statement based on two or more conditions begin met.
  • AND can be used in any SQL statement such as insert, select, delete, update. The AND operator requires each condition must be true.

OR operator:

  • The OR operator allows creating a SQL statement when any one of the specified condition is true.
  • The OR operator can be used with insert, select, delete, update.
  • We can also combine AND and OR operators in one condition.

NOT operator:

  • The NOT operator process all rows and returns all rows that do not satisfy the specified condition.
  • The NOT operator is used with IN, BETWEEN, LIKE etc.

Example:

Table: tblemp

eid

Ename City joindate
1 Riya V.V.Nagar

2016-01-01

2

Priya Anand 2017-08-12
3 Diya Nadiad

2016-12-31

4

Jiya Bakrol

2017-09-28

Retrieve the employee names that have eid of 1,4 and a city begin with ‘Ba’
select ename from tblemp where eid IN(1,4) AND city LIKE ‘Ba%’;
output:
Jiya
Retrieve the name of employee that have eid 1 or city is V.V.Nagar
select ename from tblemp where eid=1 OR city=’V.V.Nagar’;
output:
Riya
Retrieve the name of employee whose eid is between 1 and 3
select * from tblemp where eid BETWEEN 1 AND 3;
output:

eid

ename
1

Riya

2

Priya
3

Diya

Retrieve the eid, ename, city from tblemp table whose employee id is not 1,2;
select eid, ename, city from tblemp  where eid not in(1,2);
output:

eid

ename city
3 Diya

Nadiad

4

Jiya

Bakrol

Keep Visiting TechwithR.com for more upcoming top technical articles and also see my new blog Mag4info. and for kotlin tutorial also see my blog kotlin category.
You can also by SQL 70-761 book write by me from Amazon.
You can also by Kotlin Book write by me from Amazon.

Leave a Reply

Top