Skip to content
Advertisement

How to filter the record based on two columns in sql

i have a table with 2 columns and i wanted to filter the records where column1 in (Hierarchy,Single) and column2 in (‘Relate’, ‘NoRelate’). but not return the record when Column1 = Single and Column2 = NoRelate.

|Column1  |  |Column2 |
|:--------|  |-------:|
|Hierarchy|  |Relate  |
|Hierarchy|  |Norelate|
|Single   |  |relate  |
|Single   |  |Norelate|

Expected Output

|Column1  |  |Column2 |
|:--------|  |-------:|
|Hierarchy|  |Relate  |
|Hierarchy|  |NoRelate|
|Single   |  |Relate  |

Advertisement

Answer

I would just write out your conditions here:

SELECT *
FROM yourTable
WHERE Column1 IN ('Hierarchy', 'Single') AND Column2 IN ('Relate', 'NoRelate') AND
      NOT (Column1 = 'Single' AND Column2 = 'NoRelate');

screen capture from demo link below

Demo

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement