In most applications we rarely want to retrieve all rows from a table. Instead, we usually need only records that satisfy specific conditions. In SQL, filtering rows is done using the WHERE clause together with the SELECT statement.
Note that all examples presented below are designed to work with the web shop database that you can query in the Practice tab. For practice purposes, you can copy the following queries, paste them into the Practice tab, and view the results. Do not hesitate to try your own modifications of the provided queries as well.
This query retrieves only customers whose id is less than 105.
Text columns can be filtered using standard comparison operators or pattern matching.
Common comparison operators include:
This query returns customers whose first name is exactly John.
The LIKE operator allows filtering text using wildcards. The most common wildcard is %, which represents any sequence of characters.
Starts with
Returns customers whose first name starts with A.
Ends with
Returns customers using Gmail addresses.
Contains text
Returns products whose name contains the word Bag anywhere in the text.
Many databases store temporal data such as dates or timestamps. In the webshop schema, the customer table contains dateofbirth, and several tables contain created timestamps.
Example filtering customers born after a specific date:
Example filtering recently created customers:
PostgreSQL also allows explicit casting when necessary:
Here the timestamp is converted to a date before comparison.
The BETWEEN operator filters values inside a specific range.
Syntax:
Example filtering customer birth dates:
This returns customers born in the 1980s.
Numeric Example
Returns articles with discounts between 10% and 30%.
In SQL, missing values are represented by NULL. They cannot be compared using =.
Example:
Returns customers whose email address is not stored.
Sometimes comparisons involve values with different data types, such as comparing numbers stored as integers with decimal values or text values.
Example: filtering articles whose tax rate is greater than 0.2.
PostgreSQL automatically converts compatible numeric types when possible.
If the types are incompatible, explicit casting may be required. PostgreSQL supports casting using ::type.
Example:
Here an integer value is cast to numeric so it can be compared with a decimal value.
Sometimes numeric values are stored as text. In such cases, conversion is required.
Example:
This converts the money or text-like value into a numeric value before comparison.
Queries often require more than one filtering condition.
AND requires that all conditions must be true.
Example:
This returns female customers born after 1995.
OR requires that at least one condition must be true.
Example:
This query retrieves products belonging to either category.
When multiple logical operators are used in a WHERE clause, SQL evaluates them according to a defined order of precedence. Understanding this order is important because it determines how conditions are interpreted and which rows are returned.
In SQL, the logical operators follow this precedence:
This means that AND conditions are evaluated before OR conditions unless parentheses explicitly change the order.
Consider the following query:
Because AND has higher precedence than OR, SQL interprets this query as if it were written like this:
This means the query will return:
Even if that person is not female or does not meet the date condition.
If we want a different logical grouping, we must use parentheses.
Example:
Now SQL evaluates the expression inside parentheses first. This query returns only:
The parentheses therefore change the meaning of the query.
Although SQL has defined operator precedence, relying on it can make queries harder to read and easier to misunderstand. For example, the following query technically works:
But many developers prefer to write:
because the logic becomes immediately clear. Using parentheses:
+48 790-430-860
analysislessons@gmail.com