WHERE clause is used to collect the records from the table based on some condition specified. 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 desire 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 same table to work with where clause.
Here is the table with few rows.
id
name
class
mark
sex
1
John Deo
Four
75
female
2
Max Ruin
Three
90
male
3
Arnold
Three
55
male
4
Krish Star
Four
60
female
5
John Mike
Four
60
female
6
Alex John
Four
55
male
Video Tutorial on SQL SELECT with WHERE
To get only Class Four records
SELECT * FROM student WHERE class='Four'
Here is the result
id
name
class
mark
sex
1
John Deo
Four
75
female
4
Krish Star
Four
60
female
5
John Mike
Four
60
female
6
Alex John
Four
55
male
10
Big John
Four
55
female
15
Tade Row
Four
93
male
16
Gimmy
Four
93
male
21
Babby John
Four
69
female
31
Marry Toeey
Four
93
male
This will return all the records from the table name=student of class=Four. This is what we require to get all the records of fourth standard students.
WHERE with AND
Now let us add little more requirement to this and go for all the records of students of fourth standard who have scored mark more than 70.
SELECT * FROM student WHERE class='Four' AND mark >70
id
name
class
mark
sex
1
John Deo
Four
75
female
15
Tade Row
Four
93
male
16
Gimmy
Four
93
male
31
Marry Toeey
Four
93
male
We have added one more condition in where clause with a AND combination. This query will return all student records of Fourth class who have scored more than 70. There are different logical combinations using which can work on different type of fields depending on the required conditions. We can see all the non numeric fields we have to use quotes and for numeric fields we need to use quotes. Now let us try some more commands and see what result we will get.
Read more on SQL Logical Operatore OR , AND →
WHERE with BETWEEN
SELECT * FROM `student` WHERE mark BETWEEN 60 and 70
Can we use Where condition linking more that one table?
wale
23-02-2014
I have 12 tables with the same number of fields and field names. I want to sum one of those fields that have numerical values in all tables. How can I accomplish the task.
Ivan
13-09-2014
Anyone can help me how to resolve this issue?? I am getting a type mismatch with the below syntax... I dont know how to resolve it..
adoCompName.RecordSource = "SELECT * FROM Tbl_Comp_Dtl WHERE CompName = ' * " & Text1.Text & " '"
adoCompName.Refresh