Consider having the ff. table (#temp_table)
| id | column1 | column2 |
|---|---|---|
| 1 | value1 | mark |
| 2 | value2 | jim |
| 3 | value3 | taylor |
| 4 | value4 | tess |
| 5 | value5 | mark |
| 6 | value6 | jim |
How do I make a select query where first, I will exclude all rows where column2 = mark
then exclude all rows where column2 = jim and column1 = value2
The expected result set should be
| id | column1 | column2 |
|---|---|---|
| 3 | value3 | taylor |
| 4 | value4 | tess |
| 6 | value6 | jim |
The query I have tried so far is
select * from #temp_table
where column2 != 'mark'
and (column2 !='jim' and column1 != 'value2')
The result of my query
| id | column1 | column2 |
|---|---|---|
| 3 | value3 | taylor |
| 4 | value4 | tess |
>Solution :
You can "negate" a group of conditions using the operator NOT before the group
select * from #temp_table
where column2 != 'mark'
and NOT (column2 ='jim' and column1 = 'value2')
More at: https://www.w3schools.com/sql/sql_and_or.asp