Lets take a breather from JOIN statements we been looking at and lets look at AND and OR operators to be used in conjunction with with the SELECT statement to query information from the database.
Here is a sample SELECT statement using WHERE clause
select emp_name as 'Name', emp_sex as 'Sex', emp_salary as 'Salary' from employee where emp_sex = 'Male';
This will return you with 3 rows , excluding the lone female we have in the sample database.
| Name |
Sex |
Salary |
| Michael Bay |
Male |
90000.00 |
| John Evan |
Male |
5000.00 |
| George Chan |
Male |
50000.00 |
Its all fine but what if you want to select those who are BOTH Male AND has salary of more than 10000.00? Of course , this mean only two out of three will qualify. SO how do we do this in SQL?
select emp_name , emp_sex, emp_salary from employee where emp_sex = 'Male' and emp_salary > 5000;
Here we are looking for employees who are both Male and must have salary of more than 5000. The important thing to note is that using “AND’ means both the conditions must be satisfied. The query result for this statement is of just 2 rows.
| Name |
Sex |
Salary |
| Michael Bay |
Male |
90000.00 |
| George Chan |
Male |
50000.00 |
So what about OR? Try it. Replace ‘and’ with ‘or’ in the previous statement , like this ..
select emp_name , emp_sex, emp_salary from employee where emp_sex = 'Male' or emp_salary > 5000;
Guess what is the result from this query? If you say all three rows are returned as the first query then you are right. Because using ‘OR’ operator , only one of the conditions must be right. Either the employee is Male or has salary more than 5000. Since John Evan is Male , it qualifies him even though his salary is not more than 5000.
Hope this helps if you are wondering how is it possible to apply two conditions in SQL Select statement. See ya!