In this section we cover the basic yet most common scenarios of filtering table rows by column values.
We wish to filter by a logical column to return the rows where the logical column is TRUE
.
SELECT *
FROM refcon.dataset.table_1
WHERE col_4;
Here is how this works:
TRUE
and/or FALSE
values) can be used in
a WHERE
clause.col_4
) takes a value of TRUE
, are retained.We wish to obtain (filter) the rows of a table where the values of a particular column meet a condition that involves comparison to a scaler value.
In this example, we wish to obtain the rows of the table table_1
where the numerical
column col_6
takes a value
greater than 0.
SELECT *
FROM refcon.dataset.table_1
WHERE col_6 > 0;
Here is how this works:
col_6
to a scaler
value 0
via col_6 > 0
.WHERE
clause which will only keep rows that match this condition.>
. In general, we can use any comparison
operation on numerical
columns including smaller than <
, equal ==
, greater than or equal >=
, or smaller than or
equal <=
.
See Numerical Operations for more details.We wish to obtain rows where a comparison involving two of the table’s columns evaluates to TRUE
.
In this example, we wish to return rows where the value of one column col_6
is larger than the
value of another
column col_3
.
SELECT *
FROM refcon.dataset.table_1
WHERE col_6 > col_3;
Here is how this works:
We wish to obtain rows where a condition is not satisfied.
In this example, we wish to obtain rows where the value of a string column col_10
is neither ‘S’
nor ‘XS’
.
SELECT *
FROM refcon.dataset.table_1
WHERE NOT col_10 IN ('S', 'XS');
Here is how this works:
IN
operator to check whether each value of col_10
is either ‘S’
or ‘XS’
. We
cover IN
in detail
in General Operations.col_10 IN ('S', 'XS')
returns TRUE
for rows where the value of col_10
is
either ‘S’
or ‘XS’
.col_10
is neither ‘S’
nor ‘XS’
. In other words, we are
looking for the *
complement* of the condition col_10 IN ('S', 'XS')
. In SQL, we obtain that by using the
complement operator NOT
which we add right before the condition.