You can filter the displayed data based on conditions
SELECT <column-name> FROM <table-name> WHERE <conditions>;'
You can perform conditional filtering of the selected fields with
the WHERE
command
You can filter based on one column or many columns as well as any number of conditions in the order that they are defined
- You can group conditions using parenthesis
(...)
- You can't use aggregator function like
MIN()
insideWHERE
AND
: Both group of conditions have to be satisfiedBETWEEN
: Select the records that lie inside the specified rangeIN
: Check if the record equal to a set of valuesLIKE
: Match against a pattern using wild cardsREGEXP
: Match against a pattern using regexOR
: One or both group of condition have to be satisfiedNOT
: If value does not match the condition<=
: Less or equal<>
: Not equal=
: Equal to>=
: Greater or equal
If you want to filter based on whether the values of a field are equal
to an specified value use the =
sign
SELECT * FROM person WHERE gender = "Female";
When you want to match every value that does not satisfy the test
condition you can do so with the NOT
command
SELECT * FROM customers
WHERE NOT city = 'Berlin';
You can chain conditions with the logic operators
SELECT * FROM person WHERE
gender = "Male" AND
(contry_of_birth = "Poland" OR country_of_birth = "China") AND
last_name = "Pietersma";
If you want to select only those records that have null/empty values in the
specified field you can use the IS NULL
command
SELECT * FROM customers
WHERE postal_code IS NULL;
If you want only those records for which the specified field is not
null/empty then use the IS NOT NULL
command
SELECT * FROM customers
WHERE postal_code IS NOT NULL;
When you want to filter based on several values a field record has to match you
could chain <field> = <value> OR
statements or you could use the IN
operator
The following SQL query:
SELECT * FROM person WHERE
country_of_birth = "China" OR
country_of_birth = "Brazil" OR
country_of_birth = "France";
Would be the same as:
SELECT * FROM person WHERE
country_of_birth IN ("China", "Brazil", "France");
If you want to only return the records for which the specified field lies
inside a range of values you can use the BETWEEN
command
SELECT * FROM person
WHERE <column-name>
BETWEEN <lower-value> AND <upper-value>;
You can match based on a general pattern defined by wild cards and the LIKE
command
The wild cards are as follow:
%
: Any character any number of times
SELECT * FROM person
WHERE email
LIKE "%.com";
_
: Match any single character
SELECT * FROM person
WHERE email
LIKE "___.com";
[]
: Match one of the characters
SELECT * FROM customers
WHERE city LIKE '[acs]%'
By default, the matching is case sensitive. If you want to do a case
insensitive patter matching then use the ILIKE
command
You can also filter based on a pattern that is not matched
SELECT * FROM person
WHERE email
NOT LIKE "%.com";
For more complex string matching you can use the REGEXP
command to match
against a regex pattern
SELECT city FROM station
WHERE city REGEXP '^[aeiou].*'