SQL WHERE Command
WHERE command is used to collect the records from the table based on some condition specified by the where clause. More than one condition can be added to the where clause by using various logical expressions like AND, OR, < ( less than ), >(greater than) etc. Logical expressions plays important role in returning the desired records. Let us start with some examples. We are interested in the marks of Fourth class students. You can just follow the table creation process and the select query section discussed here. We will use the table named as student to work with WHERE clause.
Here is the table.
| id | name | class | marks |
|---|---|---|---|
| 1 | javed | four | 76 |
| 2 | aamir | three | 34 |
| 3 | aijaz | two | 77 |
| 4 | sohail | four | 33 |
SELECT * from student where class=’Four’
Here is the result.
| id | name | class | marks |
|---|---|---|---|
| 1 | javed | four | 76 |
| 4 | sohail | four | 33 |
This will return all the records from the table of class Four. This is what we require to get all the records of fourth standard students.
Other possible WHERE queries can be like these:
SELECT * FROM student where class = ‘Four’ and mark >77
SELECT * FROM `student` WHERE mark between 33 and 76
SELECT * FROM `student` WHERE name like ‘%sohail%’
Like is special treatment. It can be used as following.
SELECT * FROM `student` WHERE name like ‘%sohail’
SELECT * FROM `student` WHERE name like ’sohail%’
SELECT * FROM `student` WHERE name like ‘%sohail%’
%sohail will also fetch record like Najam Sohail
sohail% will also fetch record like Sohail Nadeem
%sohail% will also fetch record like abcd Sohail xyz
While = will see for exact matching value.



John Says:
Detailed SQL WHERE Command post. Thank you.