The LIKE Operator
The LIKE operator is used to find values that match a pattern. Patterns are always entered in quotes.
A per cent symbol is used to represent zero or more unknown characters; an underscore represents any single character.
Note: The LIKE operator is added to the WHERE clause, as shown in the examples below.
Filters rows where the “user” column starts with “Abd” followed by any characters.
|
|
It’s looking for values in the “user” column that start with “De” and end with “ni,” with any characters in between.
|
|
It searches for rows where the “user” column starts with ‘F,’ followed by three characters, ends with ’m,’ or has additional characters afterwards.
|
|
Fetch accounts are registered in August of any year.
|
|
Key Points:
- LIKE finds values that match a pattern
- Percent (%) represents zero or more characters
- Underscore (_) represents one character
Questions:
- Find all accounts registered in September 2023.
- Fetch all clusters in angaza_users that start with “O”
The AND Operator
The AND operator is used to combine two comparisons, creating a compound comparison. The keyword AND is placed between the two comparisons. Both comparisons must evaluate to true for the compound comparison to be true.
|
|
|
|
Key Points:
- Syntax
|
|
- AND combines two comparisons
- AND is placed between two comparisons
- Both comparisons must evaluate to true
Questions:
- Select all accounts from Abeokuta, Nigeria.
- Find all enabled accounts in Kenya. Just get the accounts registered in August 2023.
The BETWEEN Operator
Some compound comparisons using the AND operator can be more conveniently expressed using the BETWEEN operator. BETWEEN compares each column value with a range of values. The range always includes the endpoints.
The following SQL query
|
|
translates to this
|
|
Key Points:
- BETWEEN compares each column value to a range
- The range includes both endpoints
- The second value must be greater than the first
The OR Operator
The OR operator is used to combine two comparisons, creating a compound comparison. The keyword OR is placed between the two comparisons. Either or both comparisons must evaluate to true for the compound comparison to be true.
|
|
Key Points:
- OR combines two comparisons
- OR is placed between two comparisons
- Either or both comparisons must evaluate to true
Question:
- Display all columns and records in Eastern Africa.
The IN Operator
Some compound comparisons using the OR operator can be more conveniently expressed using the IN operator. IN compares each column value with a list of values. The list is enclosed in parentheses; the values are separated with commas.
|
|
The IS NULL Operator
A null value is a missing entry in a column. Nul means ‘unknown’ or ‘does not apply.’ Nulls are neither blanks nor zeros (two nulls are not necessarily equal, and you cannot do arithmetic with nulls). The IS NULL operator locates rows with null values.
There are no columns with null values in the three tables we are working with. Here is the syntax.
|
|
Key Points:
- Nulls are missing values
- Nulls are not the same as blanks
- Nulls are not the same as zeros (Not entirely)
- IS NULL locates null values
Precedence and Negation
Parentheses are used to indicate precedence - the order in which comparisons are evaluated. SQL evaluates comparisons enclosed in parentheses first. The keyword NOT is used to negate or reverse the result of a comparison.
|
|
|
|
Key Points:
- Comparisons in parentheses are evaluated first
- NOT is placed in front of a comparison
- NOT reverses the result of a comparison