PostgreSQL
NOT LIKE Operator
In PostgreSQL, DQL (Data Query Language) is used to retrieve data from the database, and one of the commonly used operators in this context is the NOT LIKE operator. The NOT LIKE operator is utilized to filter records that do not match a specific pattern. It is often used with wildcard characters such as%(which represents zero or more characters) and _ (which represents a single character). This can be especially useful when you want to exclude certain values from your query results.
For example, in a banking database with tables likecustomers, accounts, and transactions, you might use the NOT LIKE operator to find customers whose names do not contain a particular substring.
Example Code:
SELECT customer_name
FROM customers
WHERE customer_name NOT LIKE '%John%';
This query will return the names of customers that do not contain "John" anywhere in their name.
Key Points on theNOT LIKE Operator:
- Basic Syntax:
- The
NOT LIKEoperator is used in theWHEREclause. - It can be combined with wildcard characters for pattern matching.
Example:
SELECT * FROM accounts WHERE account_type NOT LIKE 'Savings%';This will return accounts whose type does not start with "Savings".
- The
- Using Wildcards:
%represents zero or more characters._represents exactly one character.
Example:
SELECT * FROM transactions WHERE transaction_id NOT LIKE 'T%1';This will return transactions where the
transaction_iddoes not start with "T" and end with "1". - Combining with Other Conditions:
- You can combine the
NOT LIKEoperator with other conditions usingAND,OR, etc.
Example:
SELECT * FROM customers WHERE customer_name NOT LIKE 'M%' AND city = 'New York';This query will return customers from New York whose names do not start with "M".
- You can combine the
- Case Sensitivity:
- In PostgreSQL, the
LIKEandNOT LIKEoperators are case-sensitive. - To perform a case-insensitive search, you can use the
ILIKEoperator instead.
Example:
SELECT * FROM customers WHERE customer_name NOT LIKE 'john%'; -- Case-sensitive SELECT * FROM customers WHERE customer_name NOT ILIKE 'john%'; -- Case-insensitive - In PostgreSQL, the
- Practical Usage in Banking Systems:
- Exclude certain account types from reports.
- Filter out customers or transactions that match unwanted patterns.
- Find records that do not contain certain strings, useful in cleanup operations.
To gain complete access, login with gmail or outlook, no need of signup. click here
TEST CODE
In PostgreSQL, to fetch records for clients whose last names do not start with 'M%', where '%' indicates the potential presence of zero or more characters following 'M', you can use the following query:
SELECT *
FROM org_client
WHERE last_name NOT LIKE 'M%';In PostgreSQL, to retrieve entries for clients whose first names do not conclude with 'e', you can use the following query:
SELECT *
FROM org_client
WHERE first_name NOT LIKE '%e';Example 1:
Explore the procedure of retrieving data from a designated table using the SQL NOT LIKE operator. Fetch records for clients whose last names do not start with 'M%', where '%' indicates the potential presence of zero or more characters following 'M'.
Example 1 - Raw data from client table

Example 1 - Query
SELECT *
FROM org_client
WHERE last_name NOT LIKE 'M%';
Example 1 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
Example 1 - Query Output

Example 2:
Retrieve entries for clients whose first names do not conclude with '%e'.
Example 2 - Query
SELECT *
FROM org_client
WHERE first_name NOT LIKE '%e';Example 2 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
Example 2 - Query Output



Comments Not Found