SQL Where Clause - How to use Where clause in SQL
Demo wikitechydatabase
- Below is a selection from the wikitechytable table used in the examples:
The SQL WHERE Clause
- The WHERE clause is used to filter records.
- It is used to extract only those records that fulfill a specified condition.
WHERE Syntax
SELECT column1, column2, ... FROM table_name WHERE condition;
Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, etc.!
WHERE Clause Example
- The following SQL statement selects all the customers from the country "Wikitechy", in the "wikitechytable" table:
SELECT * FROM wikitechytable WHERE name='Wikitechy';
Output
Text Fields vs. Numeric Fields
- SQL requires single quotes around text values (most database systems will also allow double quotes). However, numeric fields should not be enclosed in quotes:
SELECT * FROM wikitechytable WHERE id = 1;
Output
Operators in The WHERE Clause
- The following operators can be used in the WHERE clause:
Equal Operator( = )
SELECT * FROM wikitechytable WHERE id = 2;
Output 1
Greater than ( > )
SELECT * FROM wikitechytable WHERE id > 2;
Output 2
Less than ( < )
SELECT * FROM wikitechytable WHERE id < 4;
Output 3
Greater than or equal( >= )
SELECT * FROM wikitechytable WHERE id >= 2;
Output 4
Less than or equal( >= )
SELECT * FROM wikitechytable WHERE id <= 4;
Output 5
Not equal( )<>
Note: In some versions of SQL this operator may be written as !=
SELECT * FROM wikitechytable WHERE id <> 4;