SQL WHERE Command

SELECT * FROM table_name WHERE condition
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.
idnameclassmarksex
1John DeoFour75female
2Max RuinThree90male
3ArnoldThree55male
4Krish StarFour60female
5John MikeFour60female
6Alex JohnFour55male
To get only Class Four records
SELECT * FROM student WHERE  class='Four'
Here is the result
idnameclassmarksex
1John DeoFour75female
4Krish StarFour60female
5John MikeFour60female
6Alex JohnFour55male
10Big JohnFour55female
15Tade RowFour93male
16GimmyFour93male
21Babby JohnFour69female
31Marry ToeeyFour93male
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
idnameclassmarksex
1John DeoFour75female
15Tade RowFour93male
16GimmyFour93male
31Marry ToeeyFour93male
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
idnameclassmarksex
4Krish StarFour60female
5John MikeFour60female
20JacklyNine65female
21Babby JohnFour69female
34Gain ToeSeven69male
This will return all the records of the student table that have scored marks between 60 and 70.

Read more on SQL BETWEEN to get range of records

Using LIKE with WHERE

SELECT * FROM `student` WHERE name LIKE '%John%'
idnameclassmarksex
1John DeoFour75female
5John MikeFour60female
6Alex JohnFour55male
7My John RobFifth78male
10Big JohnFour55female
21Babby JohnFour69female
This search will return us all the records for which inside the name field 'John' is used. John name can be a part of the field. This will return records having name = John Deo or John Mike and even names like Alex John.

Read more on SQL LIKE tutorial




Your Rating


Google+
Raju

11-04-2013

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

Post Comment This is for short comments only. Use the forum for more discussions.








HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer