How to use SQL WHERE Clause

In previous SQL post we had discussed about Data Definition Language  Statements such as  CREATE Commandand ALTER Command and USE Command and DROP DATABASE Command and SQL SELECT Statement andSQL INSERT Statement and SQL UPDATE Statement.

Now in this SQL Post we will learn WHERE Clause.

SQL WHERE Clause

The SQL WHERE Clause is used to specify the condition while make action like Select, Update and Delete operation on Database Table.
The WHERE Clause is used to tack action on only those records that fulfill a specified condition.

 

The Syntax of WHERE Clause

SELECT col1, col2 FROM Table_Name WHERE [Condition];
SELECT col1, col2 FROM Table_Name WHERE column Operator value
SELECT col1, col2 FROM Table_Name WHERE column = “value”;
Example :

SELECT * FROM StudentMst WHERE city=”Mumbai”;

Above statement display all the records of students who belongs to city “Mumbai”.

Operators Allowed in the WHERE Clause

1. =            Equal
2. <>          Not equal
3. >            Grater than
4. <            Less than
5. >=          Grater than or equal
6. <=          Less than or equal
7. Between  Between an inclusive range
8. Like         Search for a pattern
9. IN           Specify the value to return form at least one of the column.

 

SELECT * FROM StudentMst;

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555
2 Rahul Surat 352200 7874444444
3 Jayesh Ahmedabad 352200 7874333333
4 Dhvanish Bombay 380022 7874111111
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

 

SELECT * FROM StudentMst WHERE Name=”Meera”;

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555

 

SELECT * FROM StudentMst WHERE Name <> “Meera”;

ID Name City Pincode Mobile
2 Rahul Surat 352200 7874444444
3 Jayesh Ahmedabad 352200 7874333333
4 Dhvanish Bombay 380022 7874111111
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

 

SELECT * FROM StudentMst WHERE ID > 3;

ID Name City Pincode Mobile
4 Dhvanish Bombay 380022 7874111111
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

 

SELECT * FROM StudentMst WHERE <= 3

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555
2 Rahul Surat 352200 7874444444
3 Jayesh Ahmedabad 352200 7874333333

 

SELECT * FROM StudentMst WHERE ID BETWEEN 2 and 5;

ID Name City Pincode Mobile
2 Rahul Surat 352200 7874444444
3 Jayesh Ahmedabad 352200 7874333333
4 Dhvanish Bombay 380022 7874111111
5 Reena Baroda 352222 7874666666

 

SELECT * FROM StudentMst WHERE CITY LIKE “bo%”;

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555
4 Dhvanish Bombay 380022 7874111111

 

SELECT * FROM StudentMst WHERE CITY LIKE “%o%”;

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555
4 Dhvanish Bombay 380022 7874111111
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

 

SELECT * FROM StudentMst WHERE ID IN (1,3,5,6);

ID Name City Pincode Mobile
1 Meera Bombay 380022 7874555555
3 Jayesh Ahmedabad 352200 7874333333
5 Reena Baroda 352222 7874666666
6 Veera Baroda 352222 7874121212

Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 4 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">