How to Say “Does Not Contain” in SQL: A Comprehensive Guide

SQL is a powerful programming language used for managing relational databases. When working with SQL, there may be instances where you need to query data that does not contain a certain value or string. In this guide, we will explore various ways to express “does not contain” in SQL, with both formal and informal alternatives. Let’s dive in!

1. Using the NOT LIKE Operator

The most common and straightforward way to express “does not contain” in SQL is by using the NOT LIKE operator. The LIKE operator is used to match patterns within strings, and by applying the NOT qualifier, you can negate the result. Here’s the basic syntax:

SQL Query: SELECT * FROM table_name WHERE column_name NOT LIKE ‘%value%’;

The % symbol is a wildcard character used to represent any number of characters before or after the specified value. This allows you to search for patterns within strings. In the example above, the query will fetch all rows from the specified table where the column value does not contain the specified string, ‘value’ in this case. Let’s look at some examples to better understand how to use the NOT LIKE operator:

Example 1:

SQL Query: SELECT * FROM employees WHERE department NOT LIKE ‘%HR%’;

This query will retrieve all employee records from the “employees” table where the “department” column does not contain the string ‘HR’. It could retrieve departments like ‘Finance’, ‘IT’, or ‘Marketing’.

Example 2:

SQL Query: SELECT * FROM products WHERE description NOT LIKE ‘%red%’;

In this example, the query will fetch all products from the “products” table that do not have the string ‘red’ included in their description. This could include items like ‘Blue T-Shirt’, ‘Green Umbrella’, or ‘Yellow Dress’.

2. Utilizing the NOT IN Operator

Another efficient way to express “does not contain” in SQL is by using the NOT IN operator. This operator is used to match a column value against a list of predefined values and exclude the matching ones. Here’s an example:

SQL Query: SELECT * FROM table_name WHERE column_name NOT IN (‘value1’, ‘value2’, ‘value3’);

The query above will retrieve all rows from the specified table where the column value does not match any of the given values. Let’s see it in action:

Example:

SQL Query: SELECT * FROM customers WHERE country NOT IN (‘USA’, ‘Canada’);

This query will fetch all customer records from the “customers” table where the “country” column does not contain values ‘USA’ or ‘Canada’. This could include countries like ‘Germany’, ‘France’, or ‘Australia’.

3. Using the NOT EXISTS Operator

If you need to check the absence of specific rows in a table, you can employ the NOT EXISTS operator. This operator compares the result of a subquery and returns rows where the subquery returns no results. Consider the following syntax:

SQL Query: SELECT * FROM table_name WHERE NOT EXISTS (subquery);

You can customize the subquery to match your specific requirements. Here’s an example:

Example:

SQL Query: SELECT * FROM orders WHERE NOT EXISTS (SELECT * FROM products WHERE products.order_id = orders.id);

In this case, the query will return all orders from the “orders” table that do not have corresponding records in the “products” table. This means the orders without any associated products will be retrieved.

4. Applying the LEFT JOIN Operator

If you are dealing with multiple tables and need to retrieve records from one table that do not have matching values in another table, you can use the LEFT JOIN operator. The LEFT JOIN returns all rows from the left table and the matching rows from the right table, while excluding the non-matching rows from the right table. Take a look at the syntax:

SQL Query: SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name WHERE table2.column_name IS NULL;

In this query, table1 is the left table, table2 is the right table, and column_name is the common column used for joining the two tables. The WHERE clause with IS NULL will filter the results to return only the rows where the right table’s column is NULL, indicating the absence of a match. Let’s explore an example:

Example:

SQL Query: SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id WHERE orders.order_id IS NULL;

This query fetches all user records from the “users” table that do not have a corresponding order in the “orders” table. These users are yet to place an order.

Conclusion

In conclusion, SQL offers several approaches to express “does not contain” depending on your specific requirements and the database schema. The NOT LIKE operator, NOT IN operator, NOT EXISTS operator, and the LEFT JOIN operator provide flexible ways to achieve your desired outcomes. By utilizing these techniques, you can effectively filter data that does not contain specific values or strings. Happy coding!

⭐Share⭐ to appreciate human effort 🙏
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top