Krisshan Sirohiya - 3 months ago 5x

SQL Question

I want fetch all the records from single Table except only when Column A has value X and Column B has value P. So for example

**Table:**

`Value Column A Column B`

1 X P

2 X Q

3 Y P

4 Y Q

5 Z R

So I want to fetch all records except First record where Column A has X and Column B has P value. Please help

Answer

Try this:

```
SELECT *
FROM yourtable
WHERE columnA <> 'X' AND columnB <> 'P'
```

*Edited:*

```
SELECT *
FROM yourtable t1
WHERE NOT EXISTS (
SELECT 1 FROM yourtable t2 WHERE t1.Value = t2.Value AND t2.columnA = 'X' AND t2.columnB = 'P'
);
SELECT *
FROM yourtable
WHERE NOT (columnA = 'X' AND columnB = 'P')
```

Source (Stackoverflow)

Comments