PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
The NOT LIKE
operator in PostgreSQL is used to filter rows based on pattern matching. Specifically, it returns rows where the column value does not match the specified pattern. This operator is commonly used with string data types.
column NOT LIKE pattern [ ESCAPE 'escape_character' ]
column
: The column or expression to be checked.pattern
: The pattern to match against. You can use %
to represent zero, one, or multiple characters, and _
to represent a single character.ESCAPE 'escape_character'
: An optional clause to specify the escape character if you need to search for the actual %
or _
characters.Basic usage:
To select employees whose names don't start with 'J':
SELECT name FROM employees WHERE name NOT LIKE 'J%';
Using both %
and _
:
To select employees whose names don't start with 'J' and have 'o' as the third character:
SELECT name FROM employees WHERE name NOT LIKE 'J_o%';
This would exclude names like "John", "Joan", etc., but include names like "Jane" or "Jacob".
Using the ESCAPE clause:
If you have data that includes the actual %
and _
characters and you want to filter based on them, you can use the ESCAPE
clause.
Suppose you want to find products whose descriptions do not contain the string '50% off':
SELECT description FROM products WHERE description NOT LIKE '50\% off' ESCAPE '\';
In this example, the backslash \
is used as an escape character to search for the actual %
character in the string.
Using NOT LIKE
(as well as the LIKE
operator) can sometimes be less performant, especially on large datasets, because they might not always make efficient use of indexes. If performance is a concern, it's crucial to analyze the query execution and consider other ways to optimize or restructure the query or data.
In summary, the NOT LIKE
operator in PostgreSQL allows you to filter rows based on the absence of specific patterns in string values. By using wildcards and the optional ESCAPE clause, you can construct a variety of pattern-matching conditions.
How to use NOT LIKE operator in PostgreSQL:
NOT LIKE
operator in PostgreSQL is used to exclude rows that match a specified pattern.SELECT * FROM your_table WHERE column_name NOT LIKE 'pattern';
Excluding patterns with NOT LIKE in PostgreSQL:
NOT LIKE
to exclude rows that match a specific pattern.SELECT * FROM products WHERE product_name NOT LIKE 'Apple%';
Using wildcards with NOT LIKE in PostgreSQL:
NOT LIKE
for more flexible pattern matching.SELECT * FROM your_table WHERE column_name NOT LIKE 'prefix%' AND column_name NOT LIKE '%suffix';
CASE-sensitive and CASE-insensitive matching with NOT LIKE in PostgreSQL:
SELECT * FROM your_table WHERE column_name NOT LIKE 'pattern' COLLATE "C";
Escape characters with NOT LIKE in PostgreSQL:
NOT LIKE
for special character matching.SELECT * FROM your_table WHERE column_name NOT LIKE '10\% off' ESCAPE '\';
Using NOT LIKE in conjunction with other operators in PostgreSQL:
NOT LIKE
with other operators for complex conditions.SELECT * FROM your_table WHERE column1 NOT LIKE 'pattern1' AND column2 = 'value2';
NOT LIKE operator with multiple conditions in PostgreSQL:
NOT LIKE
with multiple conditions for refined exclusion.SELECT * FROM your_table WHERE column1 NOT LIKE 'pattern1' AND column2 NOT LIKE 'pattern2';
Using NOT LIKE with regular expressions in PostgreSQL:
NOT LIKE
for advanced pattern matching.SELECT * FROM your_table WHERE column_name !~ 'pattern';
Matching numeric and date values with NOT LIKE in PostgreSQL:
NOT LIKE
to exclude rows based on numeric or date patterns.SELECT * FROM your_table WHERE numeric_column NOT LIKE '9%' OR date_column NOT LIKE '2023%';
Handling NULL values with NOT LIKE in PostgreSQL:
NOT LIKE
.SELECT * FROM your_table WHERE column_name NOT LIKE 'pattern' OR column_name IS NULL;
Using NOT LIKE in SELECT statements in PostgreSQL:
NOT LIKE
directly in SELECT statements for data retrieval.SELECT column1, column2 FROM your_table WHERE column1 NOT LIKE 'exclude%';
NOT LIKE operator in conjunction with JOIN in PostgreSQL:
NOT LIKE
with JOIN operations for complex data retrieval.SELECT * FROM table1 JOIN table2 ON table1.column = table2.column AND table2.column2 NOT LIKE 'pattern';
NOT LIKE operator with aggregate functions in PostgreSQL:
NOT LIKE
with aggregate functions for summary analysis.SELECT COUNT(*) AS total_rows FROM your_table WHERE column_name NOT LIKE 'pattern';
NOT LIKE with OR and AND conditions in PostgreSQL:
NOT LIKE
with OR and AND conditions for more complex filtering.SELECT * FROM your_table WHERE (column1 NOT LIKE 'pattern1' OR column2 NOT LIKE 'pattern2') AND column3 = 'value';