Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I want to find an SQL query to find rows where field1 does not contain $x. How can I do this?

2 Answers

0 votes
by (40.7k points)

The IN operator cannot be used with a single field, but you can use this operator in subqueries or with predefined lists as follows:

SELECT a FROM x WHERE x.b NOT IN (SELECT b FROM y);

-- subquery is used here

SELECT a FROM x WHERE x.b NOT IN (1, 2, 3, 6);

-- predefined list is like this

But, if you are looking for the string, then go for the LIKE operator (but this will be slow) like this:

SELECT * FROM x WHERE x.a NOT LIKE '%text%';

-- It'll Find all the rows where a does not contain "text"

And if you don't want the string you are searching for then it has to start with the given string, it can also use indices (if there is an index on that field) and be reasonably fast:

SELECT * FROM x WHERE x.a NOT LIKE 'text%';

--This will find all the rows where a does not start with "text"

To master SQL statements, queries and become proficient in SQL queries, enroll in our industry-recognized SQL training

0 votes
by (3.1k points)

When wanting to filter records in SQL to exclude certain values from a specific field - for example, the value of $x - one can incorporate NOT LIKE in the SQL WHERE clause.

SELECT *

FROM your_table_name

WHERE your_column_name NOT LIKE '%$x%';

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 24, 2019 in SQL by Tech4ever (20.3k points)
0 votes
2 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...