WHERE clause

The WHERE clause defines the filters that will be applied to the data. This allows you to select only the part of the data you are interested in. Logically, the WHERE clause is applied immediately after the FROM clause.

Syntax

...
WHERE <predicate>
[ ... ]

Parameters

  • <predicate>
    Boolean expression. The expression can contain logical operators such as AND, OR and NOT.

Examples

  • Select rows from the table of capitals where the value of country is 'Italy':

    SELECT *
    FROM capitals
    WHERE country = 'Italy';
    +---------+---------+
    | country | capital |
    +---------+---------+
    | Italy   | Rome    |
    +---------+---------+
  • Let’s select from the table of days of the week the rows that match the given case-sensitive LIKE clause:

    SELECT *
    FROM weekdays
    WHERE name LIKE '%S%';
    +--------+----------+
    | number |   name   |
    +--------+----------+
    | 6      | Saturday |
    +--------+----------+
    | 7      | Sunday   |
    +--------+----------+
  • Let’s select from the table of days of the week the rows that match the given ILIKE clause, which is case insensitive:

    SELECT *
    FROM weekdays
    WHERE name ILIKE '%S%';
    +--------+-----------+
    | number |    name   |
    +--------+-----------+
    | 2      | Tuesday   |
    +--------+-----------+
    | 3      | Wednesday |
    +--------+-----------+
    | 4      | Thursday  |
    +--------+-----------+
    | 6      | Saturday  |
    +--------+-----------+
    | 7      | Sunday    |
    +--------+-----------+
  • Let’s select all rows corresponding to the given compound expression:

    SELECT *
    FROM weekdays
    WHERE number > 5 OR number = 3;
    +--------+-----------+
    | number | name |
    +--------+-----------+
    | 3 | Wednesday |
    +--------+-----------+
    | 6 | Saturday |
    +--------+-----------+
    | 7 | Sunday |
    +--------+-----------+