PostgreSQL - NOT LIKE operator
Last Updated :
11 Oct, 2024
The PostgreSQL NOT LIKE operator is a powerful tool used in SQL queries to filter out rows that do not match specific patterns. By utilizing this operator, users can eliminate undesired string patterns from their results, enabling more precise data retrieval.
It is particularly beneficial in scenarios where specific naming conventions or formats need to be excluded. In this article, We will learn about the PostgreSQL NOT LIKEOperator by understanding various examples and so on.
PostgreSQL NOT LIKE Operator
- The NOT LIKE operator is used in SQL queries to filter out rows that do not match a specific pattern. It is especially useful when we need to eliminate certain string patterns from our results.
- This operator is part of PostgreSQLs string comparison toolkit and can work with other PostgreSQL window functions or conditional operators.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column_name NOT LIKE pattern;
Explanation:
column_name
: The column to filter.pattern
: The string pattern, with wildcards (%
for multiple characters and _
for a single character) to match against.
Examples of NOT LIKE operator
Example 1:
Let's write an query is to retrieve the first and last names of customers from the customer
table whose first names do not start with the letter 'K'.
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name NOT LIKE 'K%';
Output:

Explanation:
- In this SQL query, we use the
SELECT
statement to specify the columns first_name
and last_name
we want to retrieve from the customer
table.
- The
WHERE
clause filters the results to exclude any records where the first_name
begins with 'K', indicated by the NOT LIKE 'K%'
condition.
- The percentage symbol (%) is a wildcard in SQL that represents zero or more characters, meaning any first name starting with 'K' will not be included in the result set.
Example 2:
Let's write an query is to retrieve the first and last names of customers from the customer
table whose first names do not contain the substring "her" at the second position.
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name NOT LIKE '_her%';
Output:
Explanation:
- This SQL query retrieves the `first_name` and `last_name` of customers from the `customer` table whose first names do not have the substring "her" starting from the second position.
- The `NOT LIKE '_her%'` condition uses the underscore (`_`) wildcard to represent any single character before "her," ensuring that only names that do not fit this pattern are returned.
Conclusion
In summary, the NOT LIKE operator in PostgreSQL provides an effective means to refine query results by excluding unwanted patterns. Whether you need to filter out names based on their initial letters or exclude specific substrings, this operator simplifies the process.
Similar Reads
PostgreSQL - NOT IN operator PostgreSQL NOT IN condition is a powerful tool for data retrieval in PostgreSQL by allowing users to filter out specific values from their query results. This condition is particularly useful when we want to exclude a defined set of values from a dataset by making our queries more efficient and targ
4 min read
PostgreSQL - LIKE operator In PostgreSQL, the LIKE operator is an essential tool for pattern matching in SQL queries. Whether we're dealing with large datasets or searching for specific string patterns, this operator provides a powerful way to filter and retrieve data based on partial matches. By Using wildcard search techniq
5 min read
PostgreSQL - IN operator The IN operator in PostgreSQL is a powerful and efficient tool used to filter records based on a predefined set of values. When used with the WHERE clause, it simplifies SQL queries and enhances readability, making it a key component of SQL query optimization for data retrieval and database manipula
4 min read
PostgreSQL - NOT BETWEEN operator PostgreSQL NOT BETWEEN operator is used to match all values against a range of values excluding the values in the mentioned range itself. Syntax: value NOT BETWEEN low AND high; Or, Syntax: value high; The NOT BETWEEN operator is used generally with WHERE clause with association with SELECT, INSERT,
1 min read
PostgreSQL - IS NULL operator The PostgreSQL IS NULL operator is used to check whether a value is NULL. In the context of databases, NULL indicates that data is either missing or not applicable. Since NULL cannot be compared directly with any integer or string (as such comparisons result in NULL, meaning an unknown result), the
2 min read