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 - LIKE operator

The LIKE operator in PostgreSQL is used to search for a specified pattern in a column. It allows for simple pattern matching using wildcard characters:

  1. %: Represents zero, one, or multiple characters.
  2. _: Represents a single character.

Syntax:

column_name LIKE pattern

Examples:

  1. Find all names starting with 'A': Suppose you have a table employees and you want to find all employees whose names start with the letter 'A'.

    SELECT name FROM employees
    WHERE name LIKE 'A%';
    
  2. Find names with 'an' anywhere in them: To find names that have 'an' anywhere:

    SELECT name FROM employees
    WHERE name LIKE '%an%';
    
  3. Find names that have a second character 'a': If you want names where 'a' is the second character:

    SELECT name FROM employees
    WHERE name LIKE '_a%';
    
  4. Find names with exact 5 characters: To get names that are exactly 5 characters long:

    SELECT name FROM employees
    WHERE name LIKE '_____';
    

ILIKE for Case-Insensitive Search:

PostgreSQL also offers the ILIKE operator, which performs case-insensitive pattern matching. This can be very useful when you don't know (or don't want to specify) the exact casing of the pattern.

For example, to find names with 'an' anywhere in them, irrespective of case:

SELECT name FROM employees
WHERE name ILIKE '%an%';

This would match 'Alan', 'Anita', 'Johannes', and 'JANET' among others.

NOT LIKE and NOT ILIKE:

If you want to select rows that don't match a pattern, you can use the NOT LIKE or NOT ILIKE operators.

For example, to select employees whose names don't start with 'A':

SELECT name FROM employees
WHERE name NOT LIKE 'A%';

Important Points:

  • Be cautious when using the LIKE operator with patterns starting with the % wildcard on large datasets. It can be performance-intensive since it prevents the use of indexes.

  • For pattern matching on more complex patterns or regular expressions, PostgreSQL also offers the ~ (regular expression match) operator and its related operators.

In summary, the LIKE operator in PostgreSQL provides a powerful way to perform simple pattern matching in text columns. For more complex needs, consider diving into PostgreSQL's regular expression capabilities.

  1. How to use LIKE operator in PostgreSQL:

    • The LIKE operator is used for pattern matching in PostgreSQL.
    SELECT * FROM employees
    WHERE employee_name LIKE 'John%';
    
  2. Pattern matching with LIKE in PostgreSQL:

    • Use LIKE to match patterns within strings.
    SELECT * FROM products
    WHERE product_name LIKE 'App%';
    
  3. Using wildcards with LIKE in PostgreSQL:

    • Employ wildcards % and _ for flexible pattern matching.
    SELECT * FROM customers
    WHERE email LIKE 'user%@example.com';
    
  4. CASE-sensitive and CASE-insensitive matching with LIKE in PostgreSQL:

    • LIKE is case-sensitive by default. Use ILIKE for case-insensitive matching.
    SELECT * FROM users
    WHERE username LIKE 'admin%';
    
    SELECT * FROM users
    WHERE username ILIKE 'admin%';
    
  5. Escape characters with LIKE in PostgreSQL:

    • Escape special characters using the escape clause.
    SELECT * FROM data
    WHERE column_name LIKE '50\% off' ESCAPE '\';
    
  6. Using NOT LIKE in PostgreSQL:

    • Negate conditions with NOT LIKE.
    SELECT * FROM orders
    WHERE order_description NOT LIKE '%urgent%';
    
  7. Combining LIKE with other operators in PostgreSQL:

    • Combine LIKE with other operators for complex queries.
    SELECT * FROM products
    WHERE (product_name LIKE 'App%') AND (price > 100);
    
  8. LIKE operator with multiple conditions in PostgreSQL:

    • Utilize multiple conditions with LIKE for intricate matching.
    SELECT * FROM customers
    WHERE (first_name LIKE 'John%' OR last_name LIKE 'John%') AND email LIKE '%example.com';
    
  9. Using LIKE with regular expressions in PostgreSQL:

    • Leverage regular expressions for advanced pattern matching.
    SELECT * FROM data
    WHERE column_name ~ 'pattern[0-9]';
    
  10. Matching numeric and date values with LIKE in PostgreSQL:

    • Apply LIKE to match patterns in numeric or date columns.
    SELECT * FROM transactions
    WHERE transaction_id::TEXT LIKE '2022%';
    
  11. Handling NULL values with LIKE in PostgreSQL:

    • Consider NULL values when using LIKE.
    SELECT * FROM employees
    WHERE department_name LIKE 'Sales%' OR department_name IS NULL;
    
  12. Using LIKE in SELECT statements in PostgreSQL:

    • Include LIKE in SELECT statements to filter results.
    SELECT product_name, price FROM products
    WHERE product_name LIKE 'Laptop%';
    
  13. Optimizing queries with the LIKE operator in PostgreSQL:

    • Optimize queries using indexing and other techniques for better performance.
    CREATE INDEX idx_product_name ON products (product_name);
    
  14. LIKE operator in conjunction with JOIN in PostgreSQL:

    • Combine LIKE with JOIN for querying related tables.
    SELECT orders.order_id, customers.customer_name
    FROM orders
    JOIN customers ON orders.customer_id = customers.customer_id
    WHERE customers.customer_name LIKE 'John%';
    
  15. Using LIKE with index scans in PostgreSQL:

    • Leverage index scans when using LIKE for improved performance.
    CREATE INDEX idx_email ON users (email);
    
  16. Using LIKE with aggregate functions in PostgreSQL:

    • Apply LIKE in conjunction with aggregate functions for summarizing data.
    SELECT department_name, COUNT(employee_id) AS employee_count
    FROM employees
    WHERE department_name LIKE 'Sales%'
    GROUP BY department_name;