chapter

menu8. Using Wildcard Filtering

websql

8 Using Wildcard Filtering

Learning Objectives

:

  1. Create complex filters using wildcard characters and the keyword LIKE.
  2. Use the wildcard character % in place of multiple characters.
  3. Use the wildcard character _ in place of a single character.
  4. Use SIMILAR TO with a bracket expression to match one of a set of characters.

8.1 Using the LIKE Operator

In each of the filtering examples you have examined in this resource, you compared values in a table to one or more known values. The expression WHERE birth = 1861 compares the values in the birth column to the known value 1861 and returns those rows that meet this condition. Similarly, the statement WHERE birth BETWEEN 1861 AND 1865 compares the values in the birth column to both 1861 and 1865 (known values) and returns those rows where the birth is greater than or equal to 1861 and less than or equal to 1865.

There are times when the values you wish to compare are not completely known. When this happens you can use a wildcard to replace missing characters in a filter to create comparisons based on patterns rather than exact values. A pattern contains literal text and one or more wildcard characters that stand in the place of unknown characters.

Wildcard — A special character used to replace one or more missing characters in a filter.

Pattern — A filter value that contains literal text and one or more wildcard characters that stand in the place of unknown characters.

For example, you may want to list all of the artists whose style includes the word 'classic'. The word classic could appear anywhere in the style. You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write a SQL statement to list the artists and their style whose style includes the word "classic".send

Sure! Here's the SQL statement to list the artists and their style whose style includes the word "classic":

SELECT full_name, style
FROM artist
WHERE style like '%classic%';

This query selects the full_name and style columns from the artist table. The WHERE clause uses the LIKE operator to search for the word "classic" somewhere within the style column. The % symbol is a wildcard that represents any number of characters before or after the word "classic". This query will return the names of artists and their corresponding styles where the style includes the word "classic" in any position.

Try the SQL statement.

SELECT full_name, style
FROM artist
WHERE style LIKE '%classic%';

Notice the syntax of the WHERE clause. A wildcard filter uses the keyword LIKE to signal to the DBMS that a wildcard filter has been specified. It also contains a pattern to be used for comparison. The pattern in this filter contains two wildcard characters '%' and the literal text classic to return all artists with the word classic somewhere in the style.

Wildcards and Data Types. Wildcard filters can only be used with columns that have a text data type. They cannot be used with columns of any other data type.

SQL supports more than one wildcard character. Each of them is discussed in the next three sections of this lesson.

8.2 The Percent Sign (%) Wildcard

The most commonly used wildcard character is the % sign. The % sign substitutes for any character or set of characters as you set a pattern for the text you'd like to see in the query results. In the previous example, you used the % sign both before and after the word "classic". This set a pattern for the text to be selected in the WHERE clause. The % character before the word classic means that any character or set of characters could appear before the word classic in the results. Likewise, the % after the word classic means that any character or set of characters could appear after the word classic. This means that the word classic would need to appear anywhere in the style for an artist to appear in the query result.

The % Wildcard. The % character substitutes for any character or set of characters including no characters at all. This means that the pattern %classic% in the last example would include those styles with the word classic as the first or last word of the style.

It is helpful to examine a few more examples. Suppose you wish to list the artists whose style starts with the word "art". You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write an SQL statement to list the artists and their style whose style starts with the word "art".send

Certainly! Here's the SQL statement to list the artists and their style whose style starts with the word "art":

SELECT full_name, style
FROM artist
WHERE style like 'art%';

This query selects the full_name and style columns from the artist table. The WHERE clause uses the LIKE operator to search for the word "art" at the beginning of the style column. The % symbol is a wildcard that represents any number of characters after the word "art". This query will return the names of artists and their corresponding styles where the style starts with the word "art".

Try the SQL statement.

SELECT full_name, style
FROM artist
WHERE style LIKE 'art%';

In this example, the pattern for the text to be selected is "art%". This means that the word "art" must be first in the style, but it can be followed by any character or set of characters. In this case, the style "Art Nouveau" is returned, because it starts with the word "art". Any style that starts with the word "art" would be selected.

Consider another example. Assume you wish to list the artists whose style ends with the word "art". You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write an SQL statement to list the artists and their style whose style ends with the word "art".send

Sure! Here's the SQL statement to list the artists and their style whose style ends with the word "art":

SELECT full_name, style
FROM artist
WHERE style like '%art';

This query selects the full_name and style columns from the artist table. The WHERE clause uses the LIKE operator to search for the word "art" at the end of the style column. The % symbol is a wildcard that represents any number of characters before the word "art". This query will return the names of artists and their corresponding styles where the style ends with the word "art".

Try the SQL statement.

SELECT full_name, style
FROM artist
WHERE style LIKE '%art';

In this case, the pattern is "%art". This will return all artists whose style has the word "art" at the end with any character or set of characters before the word art. This includes the styles of Landscape Art and Modern Art.

In this next example assume you wish to list the artists whose style contains the word "art". You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write an SQL statement to list the artists and their style whose style contains the word "art".send

Certainly! Here's the SQL statement to list the artists and their style whose style contains the word "art":

SELECT full_name, style
FROM artist
WHERE style like '%art%';

This query selects the full_name and style columns from the artist table. The WHERE clause uses the LIKE operator with % wildcard on both sides of the word "art". This allows the query to search for the word "art" anywhere within the style column. The query will return the names of artists and their corresponding styles where the style contains the word "art" in any position.

Try the SQL statement.

SELECT full_name, style
FROM artist
WHERE style LIKE '%art%';

This example uses a pattern you used before with the word classic. In this case, the pattern "%art%" will return artists whose style has the word art anywhere in the style.

Consider one final example. Assume you wish to list the artists whose style starts with the letter "a" and ends with the letter "t". You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write an SQL statement to list the artists and their style whose style starts with "a" and ends with "t".send

Sure! Here's the SQL statement to list the artists and their style whose style starts with 'a' and ends with 't':

SELECT full_name, style
FROM artist
WHERE style like 'a%t';

This query selects the full_name and style columns from the artist table. The WHERE clause uses the LIKE operator with 'a' followed by % wildcard at the beginning, and 't' at the end. This allows the query to search for styles that start with 'a' and end with 't'. The query will return the names of artists and their corresponding styles that meet this condition.

Try the SQL statement.

SELECT full_name, style
FROM artist
WHERE style LIKE 'a%t';

In this case, the pattern "a%t" retrieves the artists whose style starts with the letter "a" and ends with the letter "t". This includes American West and America West.

Wildcards and Unusual Situations. There are some interesting unusual cases to be aware of when using wildcard criteria.

  • Not all DBMS behave the same when it comes to case sensitivity. A DBMS may match 'art%' with Art Nouveau. Another may require the pattern 'Art%' to match Art Nouveau.
  • Some DBMS will pad the end of the values in a column with trailing spaces. For example, a column may be set to limit the values in the column to 25 characters. If this is the case the entry Modern Art (10 characters) would have 15 spaces at the end to fill the 25 characters allocated to entries in the column. As a consequence, the pattern '%art' would not return Modern Art. Most modern DBMS do not pad the end of values with trailing spaces.
  • Finally, the NULL value does not behave as you might expect. Wildcard searches do not return rows with NULL values. Specifically the clause WHERE style LIKE '%' will not return artists with a null value for their style (note that it will return artists with any non-NULL style).

8.3 The Underscore (_) Wildcard

The % wildcard character is powerful and useful. The underscore (_) character is also useful, particularly when using a more prescriptive pattern. The underscore character is used to substitute for any single character. It is used in place of only one character (no more and no fewer).

Suppose you wish to list the artists whose last name is similar to "monet". In particular, you want to retrieve those artists whose last name starts with the letters "mo", has any third character, and ends with the letters "et". You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write an SQL statement to list the artists whose last name starts with the letters 'mo', has any third character, and ends with the letters 'et'.send

To list the artists whose last name starts with the letters 'mo', has any third character, and ends with the letters 'et', you can use the following SQL statement:

SELECT full_name
FROM artist
WHERE last_name like 'mo_et';

In this query, the SELECT clause selects the full_name column from the artist table. The WHERE clause uses the LIKE operator with 'mo_et' pattern to search for artists whose last name meets the specified conditions. The underscore _ is a wildcard that matches any single character, so 'mo_et' will match any last name that starts with 'mo', has any third character, and ends with 'et'. The query will return the names of artists whose last names satisfy this pattern.

Try the SQL statement.

SELECT full_name
FROM artist
WHERE last_name LIKE 'mo_et';

Notice that the query returns two artists, Claude Monet and Henry Moret. The last name of each artist follows the same pattern. They both start with the letters 'mo', have any third character, and end with the letters 'et'.

You can also mix and match wildcard characters. Suppose you wish to list the artists whose last name has the letter 'n' as the third letter. You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write an SQL statement to list the artists whose last name contains the letter 'n' as the third letter.send

To list the artists whose last name contains the letter 'n' as the third letter, you can use the following SQL statement with the LIKE operator and underscore _ wildcard:

SELECT full_name
FROM artist
WHERE last_name like '__n%';

In this query, the SELECT clause selects the full_name column from the artist table. The WHERE clause uses the LIKE operator with '__n%' pattern to search for artists whose last name contains the letter 'n' as the third letter. The underscore _ is a wildcard that matches any single character, so '__n%' will match any last name with any two characters, followed by 'n' in the third position, and then any number of characters afterward. The query will return the names of artists whose last names satisfy this pattern.

Try the SQL statement.

SELECT full_name
FROM artist
WHERE last_name LIKE '__n%';

The pattern in this example uses both the "" and "%" wildcards. The pattern starts with two "" characters that allow the first two characters to be wildcards. The letter in the third position must be "n". The pattern then allows any character or set of characters after the third position. This pattern matches "Henri" and "Landseer". It would also match "Van Der Heist" and "Man".

8.4 Matching a Set of Characters with SIMILAR TO

Some DBMSs let you use square brackets — [] — directly inside a LIKE pattern to match a set of characters at a single position. PostgreSQL's LIKE does not support this: brackets in a LIKE pattern are treated as ordinary, literal characters, not as a wildcard. To get this behavior in PostgreSQL, you use a different operator: SIMILAR TO.

LIKE Does Not Support Brackets in PostgreSQL. If you write WHERE last_name LIKE '[ab]%' in PostgreSQL, you are asking for last names that literally begin with the characters [ab] — not last names that begin with either "a" or "b". To match a set of characters at a position, use SIMILAR TO instead of LIKE.

SIMILAR TO combines pattern matching with SQL regular expression syntax, including bracket expressions. Suppose you want to list the artists whose last name starts with either the letter A or B. You can use a chat with an AI assistant to create the SELECT statement that will do this:

Write a SQL statement to list the artists whose last name starts with the letter a or b using PostgreSQL's SIMILAR TO operator.send

Sure! Since PostgreSQL's LIKE doesn't support bracket expressions, you can use SIMILAR TO instead, which does. Here's the SQL statement:

SELECT full_name
FROM artist
WHERE last_name SIMILAR TO '[ab]%';

In this query, the SELECT clause selects the full_name column from the artist table. The WHERE clause uses the SIMILAR TO operator with the pattern '[ab]%' to search for artists whose last name starts with either 'a' or 'b'. Just like in LIKE, the % symbol is a wildcard that represents any number of characters after the specified letter, but SIMILAR TO additionally understands the bracket expression [ab] as "either a or b." The query will return the names of artists whose last names start with either 'a' or 'b'.

Try the SQL statement.

SELECT full_name
FROM artist
WHERE last_name SIMILAR TO '[ab]%';

The pattern in this example uses brackets to specify a set of characters (a or b) for the character in the first position of the last name. Any artist whose last name starts with a or b will be retrieved by the query. Notice that the % wildcard still works the same way inside a SIMILAR TO pattern as it does with LIKE.

SIMILAR TO Supports Full SQL Regular Expressions. Bracket expressions are just one small piece of what SIMILAR TO can do — it supports the full SQL standard regular expression syntax, including alternation with | (for example, '(mo|va)_et') and repetition with * and +. If you find yourself wanting more powerful pattern matching than SIMILAR TO offers, PostgreSQL also supports POSIX regular expressions directly through the ~ operator (for example, WHERE last_name ~ '^[AB]'), which is even more flexible still.

Using Wildcards. Queries that use wildcards or pattern matching in the criteria of the WHERE clause take longer to process than other queries. Keep these general principles in mind:

  • Whenever possible, write queries without using wildcards, because they will process more quickly.
  • Queries with a wildcard character at the beginning of the search pattern will take the longest to process.
  • Carefully design the patterns you wish to use in your search criteria. It is easy to misplace a character and not retrieve the results you need.

8.5 Summary

This lesson described how to use wildcard characters in the WHERE clause to create sophisticated filtering conditions. When using wildcards with LIKE, you will use a combination of the wildcard characters "%" and "_". PostgreSQL's LIKE does not support bracket expressions the way some other DBMSs do, so matching a set of characters at a position requires the SIMILAR TO operator (or, for full regular expressions, the ~ operator) instead. Wildcard and pattern-matching searches are powerful and you will likely use them often in your query writing.