Skip to content
Advertisement

SQL select when one condition or another are met but not both

Given the following table:

id      column1   column2
-------------------------
1       3         8
2       4         7
3       4         10
4       4         14
5       14        17
6       10        27
7       14        21
8       16        14
9       21        4
10      30        3

what is the nicest way to query selecting rows when number 4 and 14 are selected in column1 or column2 BUT exclude when number 4 and number 14 are both in the row. Be aware that the order can be reversed.

expected output

id      column1   column2
-------------------------
2       4         7
3       4         10
5       14        17
7       14        21
8       16        14
9       21        4

Advertisement

Answer

SELECT * FROM table WHERE (column1=4 XOR column2=14) XOR (column1=14 XOR column2=4)  
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement