0

I am trying to select a set of data including female and male in 50 states. However, I need to exclude 5 states with both female and male, and one state with female. I'm not sure how to have two conditions in one where statement.

Below is the simple version of the code. The last row of the code is clearly wrong.

I want to say 'I want to exclude data when state code is OR, CA, WA, OH and VA', also 'I want to exclude data when state code is DC and gender is female'.

I hope you can understand what I am trying to ask. Any advice would be greatly appreciated. Thank you!

SELECT * FROM dataset WHERE state_code NOT IN ('OR','CA','WA','OH','VA) AND (state_code <> DC AND gender <> female) 
1
  • Onluy add quote to you select literal SELECT * FROM dataset WHERE state_code NOT IN ('OR','CA','WA','OH','VA) AND (state_code <> 'DC' AND gender <> 'female') Commented Jun 6, 2016 at 17:08

3 Answers 3

2

This is the most literal translation of your conditions I can think of:

SELECT * FROM dataset WHERE state_code NOT IN ('OR','CA','WA','OH','VA') AND NOT (state_code = DC AND gender = 'female') ; 

It is logically equivalent to Remi's answer, as NOT (X AND Y) is the same as (NOT X OR NOT y).

Sign up to request clarification or add additional context in comments.

1 Comment

I just tried and it worked. Fantastic! Thanks a lot!
1

Here is the corrected query :

SELECT * FROM dataset WHERE state_code NOT IN ('OR','CA','WA','OH','VA') AND (state_code != 'DC' OR gender != 'female'); 

The WHERE state_code NOT IN ('OR','CA','WA','OH','VA')line excludes 5 state_code and the AND (state_code != 'DC' OR gender != 'female') line excludes women living in 'DC' state.

Comments

0

This shoult be correct:

SELECT * FROM dataset WHERE state_code NOT IN ('DC','OR','CA','WA','OH','VA') OR (state_code == 'DC' AND gender == male)

1 Comment

That would not filter out "DC female" as they are not in the "NOT IN" list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.