Jess
Jess

Reputation: 25069

How to filter out multiple values of a particular set

In this example, how do I filter out 10 year old males? I don't want to filter out all males nor all 10 year olds. Also I don't want to specify the list of attributes that I want to filter for, just attributes I want to filter out!

Sample data and table:

sqlite> create table person ( age int, gender char(1), name varchar(10));
sqlite> insert into person values (10,'f','Anoushka');
sqlite> insert into person values (10,'m','James');
sqlite> insert into person values (30,'f','Mei');
sqlite> insert into person values (30,'m','Ahmed');

I used this select which returns the data I want, but is there a better way?

sqlite> select *
   ...> from person
   ...> where age || gender != '10m';
10|f|Anoushka
30|f|Mei
30|m|Ahmed

Upvotes: 0

Views: 100

Answers (2)

Jess
Jess

Reputation: 25069

I did find another way. You just need to us or logic, which I seldom use, but it comes in handy.

sqlite> select *
   ...> from person
   ...> where (age != 10 or gender != 'm');
10|f|Anoushka
30|f|Mei
30|m|Ahmed

Another way to exclude 10 year old males is to simply not that filter. This option is easier to read for me.

sqlite> select *
   ...> from person
   ...> where not (age = 10 and gender = 'm');
10|f|Anoushka
30|f|Mei
30|m|Ahmed

See also De Morgan's Laws: "not (A and B)" is the same as "(not A) or (not B)"

Upvotes: 0

StevieG
StevieG

Reputation: 8709

select *
from person
where age != '10'
or gender != 'm';

Upvotes: 1

Related Questions