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 LIKE
operator in PostgreSQL is used to search for a specified pattern in a column. It allows for simple pattern matching using wildcard characters:
%
: Represents zero, one, or multiple characters._
: Represents a single character.column_name LIKE pattern
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%';
Find names with 'an' anywhere in them: To find names that have 'an' anywhere:
SELECT name FROM employees WHERE name LIKE '%an%';
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%';
Find names with exact 5 characters: To get names that are exactly 5 characters long:
SELECT name FROM employees WHERE name LIKE '_____';
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.
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%';
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.
How to use LIKE operator in PostgreSQL:
LIKE
operator is used for pattern matching in PostgreSQL.SELECT * FROM employees WHERE employee_name LIKE 'John%';
Pattern matching with LIKE in PostgreSQL:
LIKE
to match patterns within strings.SELECT * FROM products WHERE product_name LIKE 'App%';
Using wildcards with LIKE in PostgreSQL:
%
and _
for flexible pattern matching.SELECT * FROM customers WHERE email LIKE 'user%@example.com';
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%';
Escape characters with LIKE in PostgreSQL:
SELECT * FROM data WHERE column_name LIKE '50\% off' ESCAPE '\';
Using NOT LIKE in PostgreSQL:
NOT LIKE
.SELECT * FROM orders WHERE order_description NOT LIKE '%urgent%';
Combining LIKE with other operators in PostgreSQL:
LIKE
with other operators for complex queries.SELECT * FROM products WHERE (product_name LIKE 'App%') AND (price > 100);
LIKE operator with multiple conditions in PostgreSQL:
LIKE
for intricate matching.SELECT * FROM customers WHERE (first_name LIKE 'John%' OR last_name LIKE 'John%') AND email LIKE '%example.com';
Using LIKE with regular expressions in PostgreSQL:
SELECT * FROM data WHERE column_name ~ 'pattern[0-9]';
Matching numeric and date values with LIKE in PostgreSQL:
LIKE
to match patterns in numeric or date columns.SELECT * FROM transactions WHERE transaction_id::TEXT LIKE '2022%';
Handling NULL values with LIKE in PostgreSQL:
LIKE
.SELECT * FROM employees WHERE department_name LIKE 'Sales%' OR department_name IS NULL;
Using LIKE in SELECT statements in PostgreSQL:
LIKE
in SELECT
statements to filter results.SELECT product_name, price FROM products WHERE product_name LIKE 'Laptop%';
Optimizing queries with the LIKE operator in PostgreSQL:
CREATE INDEX idx_product_name ON products (product_name);
LIKE operator in conjunction with JOIN in PostgreSQL:
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%';
Using LIKE with index scans in PostgreSQL:
LIKE
for improved performance.CREATE INDEX idx_email ON users (email);
Using LIKE with aggregate functions in PostgreSQL:
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;