SQL Where Clause

Filed Under: SQL

When we have to UPDATE, DELETE or SELECT a particular set of data we use SQL WHERE clause. The WHERE clause is an integral part of SQL as it provides the feature to add conditions to a query.

SQL Where Clause

SQL where clause, SQL Query where statement example
SQL where clause is one of the most widely used SQL keyword because it’s part of most of the queries. Let’s see the general syntax of where clause in sql query.


SELECT column(s) FROM table_name WHERE condition;

In the syntax above the column data is retrieved based on the condition that is specified as part of WHERE condition.

Similarly we can use it with Update and Delete query as follows:


update Employee set name = 'David' where id=1;

Delete from Employee where name = 'John';

SQL Query Where Example

Let’s try to understand the SQL WHERE command through some example. We have a sample table with below structure and data.

CustomerIdCustomerNameCustomerAgeCustomerGender
1John31M
2Amit25M
3Annie35F

Now we want to select all the customer names who are Female. Below is the SQL query with where clause to get this data.


SELECT CutomerName FROM Customer WHERE CustomerGender = 'F';

SQL Where Clause Operators

Since where clause is to add conditions to the query, there are many operators that we can use with it. Let’s look into commonly used operators with sql query where clause.

OperatorDescriptionExample
=Equal toSELECT CustomerNameFROM Customer WHERE CustomerGender = ‘F’;
!= , <>Not equal toSELECT CustomerName FROM Customer WHERECustomerGender != ‘M’;
>Greater thanSELECT CustomerNameFROM Customer WHERE CustomerAge > 32;
<Less thanSELECT CustomerName FROM Customer WHERECustomerAge < 30;
>=Greater than or equaltoSELECT CustomerNameFROM Customer WHERE CustomerAge >= 35;
<=Less than or equal toSELECT CustomerName FROM Customer WHERECustomerAge <= 30;
INValue is from thespecified list of valuesSELECT CustomerNameFROM Customer WHERE CustomerGender IN (‘M’, ‘F’);
BETWEENBetween the specified rangeSELECT CustomerName FROM Customer WHERECustomerAge BETWEEN (25,30);
LIKESearch for patternSELECT CustomerNameFROM Customer WHERE CustomerName like ‘Ann%’;

Numeric and text are used as part of the WHERE clause values in different ways. To use Numeric no quotes should be used. For using a text value single quotes should be used.

That’s all for SQL where clause. Where clause in SQL query is used a lot, so you should understand it properly.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages