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

PostgreSQL - NOT LIKE operator

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.

Syntax:

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.

Examples:

  1. Basic usage:

    To select employees whose names don't start with 'J':

    SELECT name FROM employees WHERE name NOT LIKE 'J%';
    
  2. 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".

  3. 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.

Note:

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.

  1. How to use NOT LIKE operator in PostgreSQL:

    • The 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';
    
  2. Excluding patterns with NOT LIKE in PostgreSQL:

    • Use NOT LIKE to exclude rows that match a specific pattern.
    SELECT *
    FROM products
    WHERE product_name NOT LIKE 'Apple%';
    
  3. Using wildcards with NOT LIKE in PostgreSQL:

    • Apply wildcards with NOT LIKE for more flexible pattern matching.
    SELECT *
    FROM your_table
    WHERE column_name NOT LIKE 'prefix%' AND column_name NOT LIKE '%suffix';
    
  4. CASE-sensitive and CASE-insensitive matching with NOT LIKE in PostgreSQL:

    • Achieve case-sensitive or case-insensitive matching based on collation settings.
    SELECT *
    FROM your_table
    WHERE column_name NOT LIKE 'pattern' COLLATE "C";
    
  5. Escape characters with NOT LIKE in PostgreSQL:

    • Use escape characters with NOT LIKE for special character matching.
    SELECT *
    FROM your_table
    WHERE column_name NOT LIKE '10\% off' ESCAPE '\';
    
  6. Using NOT LIKE in conjunction with other operators in PostgreSQL:

    • Combine NOT LIKE with other operators for complex conditions.
    SELECT *
    FROM your_table
    WHERE column1 NOT LIKE 'pattern1' AND column2 = 'value2';
    
  7. NOT LIKE operator with multiple conditions in PostgreSQL:

    • Apply NOT LIKE with multiple conditions for refined exclusion.
    SELECT *
    FROM your_table
    WHERE column1 NOT LIKE 'pattern1' AND column2 NOT LIKE 'pattern2';
    
  8. Using NOT LIKE with regular expressions in PostgreSQL:

    • Leverage regular expressions with NOT LIKE for advanced pattern matching.
    SELECT *
    FROM your_table
    WHERE column_name !~ 'pattern';
    
  9. Matching numeric and date values with NOT LIKE in PostgreSQL:

    • Use 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%';
    
  10. Handling NULL values with NOT LIKE in PostgreSQL:

    • Account for NULL values in exclusion patterns with NOT LIKE.
    SELECT *
    FROM your_table
    WHERE column_name NOT LIKE 'pattern' OR column_name IS NULL;
    
  11. Using NOT LIKE in SELECT statements in PostgreSQL:

    • Apply NOT LIKE directly in SELECT statements for data retrieval.
    SELECT column1, column2
    FROM your_table
    WHERE column1 NOT LIKE 'exclude%';
    
  12. NOT LIKE operator in conjunction with JOIN in PostgreSQL:

    • Combine 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';
    
  13. NOT LIKE operator with aggregate functions in PostgreSQL:

    • Apply NOT LIKE with aggregate functions for summary analysis.
    SELECT COUNT(*) AS total_rows
    FROM your_table
    WHERE column_name NOT LIKE 'pattern';
    
  14. NOT LIKE with OR and AND conditions in PostgreSQL:

    • Combine 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';