Home > Software design >  Filtering on two conditions in SQL
Filtering on two conditions in SQL

Time:12-09

I am using Oracle SQL and have the following table, which I would like to filter to exclude the records in which ID = 2 and GRP = X, and ID = 3 and GRP = X, as these were entered in error.

ID    GRP
1     X
2     B
2     X
3     C
3     X

What is the correct syntax to do so? My desired end result table is:

ID    GRP
1     X
2     B
3     C

CodePudding user response:

Using row value constructor:

SELECT *
FROM  tab
WHERE (ID, GRP) NOT IN ((2,'X'),(3,'X'))

CodePudding user response:

SELECT * 
FROM tab
WHERE ID NOT IN (2,3) AND GRP <> 'X'

or

SELECT * 
FROM tab
WHERE (ID <> 2 OR ID <> 3)  AND GRP <> 'X'
  • Related