SQL Where Clause - How to use Where clause in SQL



Demo wikitechydatabase

  • Below is a selection from the wikitechytable table used in the examples:
sql-where-clause-1

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

sql-where-clause-2

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

sql-where-clause-3

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

sql-where-clause-4

Greater than ( > )

SELECT * FROM wikitechytable WHERE id > 2;

Output 2

sql-where-clause-5

Less than ( < )

SELECT * FROM wikitechytable WHERE id < 4;

Output 3

sql-where-clause-6

Greater than or equal( >= )

SELECT * FROM wikitechytable WHERE id >= 2;

Output 4

sql-where-clause-7

Less than or equal( >= )

SELECT * FROM wikitechytable WHERE id <= 4;

Output 5

sql-where-clause-8

Not equal( )<>

Note: In some versions of SQL this operator may be written as !=

SELECT * FROM wikitechytable WHERE id <> 4;

Output 6

sql-where-clause-9

Related Searches to SQL Where Clause - How to use Where clause in SQL