Open In App
Related Articles

PostgreSQL – LIKE operator

Improve Article
Improve
Save Article
Save
Like Article
Like

The PostgreSQL LIKE operator is used query data using pattern matching techniques. Its result include strings that are case-sensitive and follow the mentioned pattern.
It is important to know that PostgreSQL provides with 2 special wildcard characters for the purpose of patterns matching as below:

  • Percent ( %) for matching any sequence of characters.
  • Underscore ( _) for matching any single character.
Syntax: string LIKE pattern;

For the sake of this article we will be using the sample DVD rental database, which is explained here and can be downloaded by clicking on this link in our examples.

Now, let’s look into a few examples.

Example 1:
Here we will make a query to find the customer in the “customer” table by looking at the “first_name” column to see if there is any value that begins with “K” using the LIKE operator in our sample database.

SELECT
    first_name,
        last_name
FROM
    customer
WHERE
    first_name LIKE 'K%';

Output:

Notice few things in the above example, the WHERE clause contains a special expression: the first_name, the LIKE operator, and a string that contains a percent (%) character, which is referred to as a pattern.

Example 2:
Here we will query for customers whose first name begins with any single character, is followed by the literal string “her”, and ends with any number of characters using the LIKE operator in our sample database.

SELECT
    first_name,
    last_name
FROM
    customer
WHERE
    first_name LIKE '_her%';

Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Aug, 2020
Like Article
Save Article
Previous
Next
Similar Reads