The conditions in the optional WHERE clause are used to limit what data is displayed or used in a calculation.

For example, if your query was

FROM tickets
SELECT tickets.id

you’d get a table with every ticket’s ID - not very useful.

But with:

FROM tickets
SELECT tickets.id
WHERE tickets.status = 'awaiting_agent'

you’d get a table with the IDs of just the tickets with a status of Awaiting Agent.

Conditions can be joined by operators such as AND, OR, NOT, IN and parentheses (brackets) to make complex expressions.

IN is useful if you want to match any one of a number of different values. For example:

WHERE tickets.status IN ('awaiting_agent','awaiting_user')

will return all tickets with either Awaiting Agent or Awaiting User status.

tickets.labels.label != NULL

will return all tickets that have a label (i.e. the label value does not equal NULL).

Published: 23/03/2018

Last updated: 22/05/2018