WHERE Statement or WHERE Clause in SQL Statement Explained

WHERE Statement or WHERE Clause in SQL Statement Explained

The SQL WHERE statement is utilized to determine a condition while bringing the information from single table or joining with various tables. In the event that the given condition is fulfilled then just it returns particular records from the table. You would utilize WHERE statement to channel the records and bringing just vital records.

The WHERE condition is utilized as a part of SELECT statement, as well as utilized as a part of UPDATE, DELETE statement, and so on., which we would inspect and discuss in upcoming sections.

Syntax:

The basic and general syntax of SELECT statement with WHERE clause is as follows:

SELECT col_name_1, col_name_2, … , col_name_n

FROM tbl_name

WHERE col_name operator value;

In the above syntax, the purpose of SELECT and FROM clauses are same as explained in SELECT Statement whereas, the WHERE keyword refers to the definition of a criteria.

This criteria requires at least one column, based on which, the criteria will work, operator is either the logical or comparison operators like >, <, =, LIKE, NOT, etc. These operators are discussed in coming sections.

Example:

Currently the Customers table has following records:

ID C_Name Contact_No City Email Status
1 Tim Robbins +44 54 443-4434 London tim@myemail.uk Active
2 James Chris +44 54 498-3476 Birmingham james@myemail.uk Active
3 Kevin Sputnik +44 54 487-6987 Manchester kevin@myemail.uk Inactive
4 Richard Butler +44 54 422-2345 Birmingham richard@myemail.uk Active
5 David McGregor +44 54 413-0989 Cardiff david@myemail.uk Active


SELECT * FROM Customers

WHERE City = ‘Birmingham’;

The above command will produce following output:

ID C_Name Contact_No City Email Status
2 James Chris +44 54 498-3476 Birmingham james@myemail.uk Active
4 Richard Butler +44 54 422-2345 Birmingham richard@myemail.uk Active

In the above table, two records met the criteria where City column had a value ‘Birmingham’.

Now further narrowing this to single record level.

SELECT * FROM Customers

WHERE City = ‘Birmingham’ AND ID = 2;

Now this new command will return the following record as there is only one record in table that has City as Birmingham and ID = 2.

ID C_Name Contact_No City Email Status
2 James Chris +44 54 498-3476 Birmingham james@myemail.uk Active

Operators in the WHERE Clause

The following logical and comparison operators can be used in the WHERE clause:

Operator Description
= Equal to
<>  or != Not equal to.

Note: some SQL versions support != as non-equal to operator

> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
BETWEEN Between and including range
LIKE Search for a pattern
IN To specify multiple possible values for a column
% Wildcard … usually used with LIKE operator

More Related Articles For You

    DBMS Tutorial

    SQL and DBMS Interview Questions with Answers

    DBMS and SQL Practice Tests , Questions and Quizzes

Search

Shortcodes Ultimate

 
Follow Us
 
BGP