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 | Status | |
1 | Tim Robbins | +44 54 443-4434 | London | [email protected] | Active |
2 | James Chris | +44 54 498-3476 | Birmingham | [email protected] | Active |
3 | Kevin Sputnik | +44 54 487-6987 | Manchester | [email protected] | Inactive |
4 | Richard Butler | +44 54 422-2345 | Birmingham | [email protected] | Active |
5 | David McGregor | +44 54 413-0989 | Cardiff | [email protected] | Active |
SELECT * FROM Customers
WHERE City = ‘Birmingham’;
The above command will produce following output:
ID | C_Name | Contact_No | City | Status | |
2 | James Chris | +44 54 498-3476 | Birmingham | [email protected] | Active |
4 | Richard Butler | +44 54 422-2345 | Birmingham | [email protected] | 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 | Status | |
2 | James Chris | +44 54 498-3476 | Birmingham | [email protected] | 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
- Architecture of DBMS
- CODDS RULE DBMS
- Database Models in DBMS
- Relational DBMS Concepts
- Keys and Types of keys in Database
- Database Normalization
- Generalization, Specialization and Aggregation Concepts in DBMS
- ERD Diagram Tutorial with Examples in DBMS
- Introduction to SQL
- How to Create Query in SQL, Create Table, Delete Table and User Insert Info Statements
- Alter Query Command in SQL, Add, Modify and Drop Column in Table
- TCL Commands in SQL
- Truncate Query, Drop and Rename Query in SQL
- All DML Statement in SQL, Select Statement, Delete, Insert and Update Statement
- Data Control Language DCL Revoke and Grant Command in SQL
- Select Statement or Select Query in SQL Explained with Examples
- Distinct Keyword Explained in SQL
- AND & OR Operators in SQL
- LIKE Operator in SQL
- ORDER BY Clause Sorting Explained in SQL
- Group By Clause in SQL
- Having Clause Explained in SQL
- SQL Constraints
- SQL Aggregate Functions
- SQL Aliases Use and Purpose in SQL
- SQL Joins Types, Use and Purpose
- SQL Sequence Syntax and Use with Examples
- SQL View
- SET Operation in SQL, UNION, UNION ALL, Intersect and Minus