How to use SQL 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

  • =            Equal
  • <>          Not equal
  • >            Grater than
  • <            Less than
  • >=          Grater than or equal
  • <=          Less than or equal
  • Between  Between an inclusive range
  • Like         Search for a pattern
  • 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 *