We have separated LIKE clause from other logical / comparison operators because it has slightly more examples that we wish to share.
We can use these wildcard operators in different combinations and the basic syntax for this clause is:
SELECT first_name, last_name
WHERE first_name LIKE “XXX%”;
XXX can be used to represent any numeric or character value.
This is extremely useful for large database where we want to search for certain records.
For example – we can use this clause for the following scenarios:
- To find customer records with this first name.
WHERE first_name LIKE “Jason%”;
- To find customer records whose age starts with a 3.
WHERE age LIKE “3%”;
- To find customer records that have an ‘A’ in the second position in their first_name.
WHERE first_name LIKE “_a%”;
- To find customer records that have an “ON” in the second and third positions in their last_name.
WHERE last_name LIKE “_on%”;
- To find customer records that ends with “N” in their first_name.
WHERE first_name LIKE “%n”;
- To find customer records that have an “J” in their first_name or age over 34 years old.
WHERE first_name LIKE “J%” OR age > 34;
We can always use this clause and combine with other logical / comparison operators whenever required to query specific observations in the database.
Categories: SQL Learning