PostgreSQL - NOT LIKE operator

Last Updated : 24 Jul, 2026

The NOT LIKE operator in PostgreSQL is used to filter rows whose values do not match a specified pattern. It is commonly used with wildcard characters such as % (zero or more characters) and _ (a single character) to exclude matching records.

  • Commonly used with the WHERE clause.
  • Can be combined with clauses such as ORDER BY and LIMIT.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE column_name NOT LIKE pattern;

Where:

  • column1, column2, ...: The columns to retrieve.
  • table_name: The name of the table.
  • column_name: The column to compare.
  • pattern: The pattern used for matching.

Examples

Firstly, create a Student table and insert some records into it.

Screenshot-2026-07-09-164714
Student Table

Example 1: Exclude Names Starting with 'J'

The following query retrieves students whose first names do not start with the letter J.

Query:

SELECT *
FROM Student
WHERE FirstName NOT LIKE 'J%';

Output:

Screenshot-2026-07-09-164844

Example 2: Exclude Names Ending with 'a'

The following query retrieves students whose first names do not end with the letter a.

Query:

SELECT *
FROM Student
WHERE FirstName NOT LIKE '%a';

Output:

Screenshot-2026-07-09-165024

Example 3: Exclude Names Containing 'li'

The following query retrieves students whose first names do not contain the substring li.

Query:

SELECT *
FROM Student
WHERE FirstName NOT LIKE '%li%';

Output:

Screenshot-2026-07-09-165130

Example 4: Exclude Courses Starting with 'C'

The following query retrieves students whose course names do not start with the letter C.

Query:

SELECT *
FROM Student
WHERE Course NOT LIKE 'C%';

Output:

Screenshot-2026-07-10-130027

Example 5: Find Students Whose First Name Starts with 'J' and Third Character is 'h'

The following query retrieves the names of students whose first name starts with 'J', has 'h' as the third character, and is followed by any number of characters.

Query:

SELECT FirstName
FROM Student
WHERE FirstName LIKE 'J_h%';

Output:

Screenshot-2026-07-17-152915

Example 6: Use NOT LIKE with ORDER BY

The following query retrieves students whose first names do not start with M and sorts the results alphabetically.

Query:

SELECT *
FROM Student
WHERE FirstName NOT LIKE 'M%'
ORDER BY FirstName;

Output:

Screenshot-2026-07-10-130307
Comment

Explore