![]() |
VOOZH | about |
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.
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.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:
👁 ImageExplanation:
SELECTstatement to specify the columns first_name and last_name we want to retrieve from the customer table.WHERE clause filters the results to exclude any records where the first_name begins with 'K', indicated by the NOT LIKE 'K%' condition. Example 2:
Let's write an query is to retrieve the first and last names of customers from the customertable 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:
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.