The AND & OR & NOT operators(logical operators) are used to filter records based on more than one condition. The AND operator displays a record if both the first condition and the second condition is true. The OR operator displays a record if either the first condition or the second condition is true. The NOT operator displays the records that are false to the specified value. In the previous tutorials, we have learn that the WHERE keyword can be used to conditionally select data from a table.This are simple conditions. With And & OR & NOT we will learn how to make compound conditions. Compound conditions are simple conditions combined connected using And & OR & NOT. We can use as many simple conditions as we like into out query.
Sintax for a compound condition is :
SELECT (column name) FROM table_name
WHERE (simple condition) {[AND|OR|NOT] (simple condition)}+;
Script:
--first lets drop the table Test (if she exists, if not skip the drop line).
Drop table test;
create table test (id int,name varchar(20),email varchar(20),salary int ,de char(20));
--insert values into table
insert into test values (1,'Eve','[email protected]',1500,'HR');
insert into test values (2,'Jon','[email protected]',2500,'AD');
insert into test values (3,'Mike','[email protected]',3000,'AD');
insert into test values (4,'Paul','[email protected]',3200,'HR');
insert into test values (5,'Mary','[email protected]',1800,'IT');
insert into test values (6,'Jane','[email protected]',2200,'IT');
SQLselect * from test ;
ID NAME EMAIL SALARY DE
---------- -------------------- -------------------- ---------- -- --------
1 Eve [email protected] 1500 HR
2 Jon [email protected] 2500 AD
3 Mike [email protected] 3000 AD
4 Paul [email protected] 3200 HR
5 Mary [email protected] 1800 IT
6 Jane [email protected] 2200 IT
The AND condition
If you want to select rows that must satisfy all the given conditions, you can use the logical operator, AND.Example
-we want to see the persons that have the id =1 and name= Eve.SQLselect * from test where id=1 and name='Eve';
ID NAME EMAIL SALARY DE
---------- -------------- --------------- ---------- -------
1 Eve [email protected] 1500 HR
SQLselect * from test where id=1 and name='Evan';
-no lines found.
Example 2
-an example with more than 2 AND conditionsSQLselect * from test where id=1 and name='Eve' and email='[email protected]';
ID NAME EMAIL SALARY DE
---------- -------------------- -------------------- ---------- ---------
1 Eve [email protected] 1500 HR
The OR condition
If you want to select rows that satisfy at least one of the given conditions, you can use the logical operator, OR.SQLselect * from test where id=1 or name='Evan';
ID NAME EMAIL SALARY DE
---------- -------------- -------------- -------- -------
1 Eve [email protected] 1500 HR
The NOT condition
If you want to find rows that do not satisfy a condition, you can use the logical operator, NOT. NOT results in the reverse of a condition. That is, if a condition is satisfied, then the row is not returned. Using our test table we will want to find the name of the persons who are not working in the AD department:mysqlselect * from test where not de= 'AD';
+------+------+----------------+--------+------+
| id | name | email | salary | de |
+------+------+----------------+--------+------+
| 1 | Eve | [email protected] | 1500 | HR |
| 4 | Paul | [email protected] | 3200 | HR |
| 5 | Mary | [email protected] | 1800 | IT |
| 6 | Jane | [email protected] | 2200 | IT |
+------+------+----------------+--------+------+
mysqlselect * from test where de not in ('AD');
+------+------+----------------+--------+------+
| id | name | email | salary | de |
+------+------+----------------+--------+------+
| 1 | Eve | [email protected] | 1500 | HR |
| 4 | Paul | [email protected] | 3200 | HR |
| 5 | Mary | [email protected] | 1800 | IT |
| 6 | Jane | [email protected] | 2200 | IT |
+------+------+----------------+--------+------+