AnalyzeThis
AnalyzeThis

Reputation: 49

What is Wrong with my Query?

I am working on a project for a data bootcamp that I am in, and I am working with three tables. Within these tables, I am trying to find a little more information about the users in this data set. Some of the questions I would like to answer are...

  1. How many users use more than one browser in their daily life?
  2. What are the most popular browsers used besides firefox?
  3. What is the most popular browser used amongst users who use multiple browsers?

Attached is an overview of my table (ignore the crap code) data set and tables This is the query I am having problems with:

select user_id, q2, q3, q4
from survey
where q2 = '0' or '1' and q3 = '0' or '1' or '2' or '3' and q4 = '0' or '1' or '2' or '3' or '4' or '5'

I want to pull up the named values for q2, q3, q4. How do I write this correctly so that it pulls the data in one set? I would like to pull it in one set so that I can export and import into excel and pivot the data.

Upvotes: 0

Views: 57

Answers (2)

Gilad Green
Gilad Green

Reputation: 37299

Two problems:

  1. Misunderstanding of operator precedence - If you have both and and or make sure you wrap properly with ()..
  2. Compile error - between each or you must specify again the comparison. q3 = '0' or '1' won't work -> change to q3 = '0' or q3 = '1'

So:

select user_id, q2, q3, q4
from survey
where (q2 = '0' or q2 = '1')
and (q3 = '0' or q3 = '1' or q3 = '2' or q3 ='3')
and (q4 = '0' or q4 = '1' or q4 = '2' or q4 = '3' or q4 = '4' or q4 = '5')

Now, a nicer way to do this is to use in

select user_id, q2, q3, q4
from survey
where q2 in ('0','1')
and q3 in ('0','1','2','3')
and q4 in ('0','1','2','3','4','5')

Upvotes: 4

trincot
trincot

Reputation: 350252

The thing that is wrong with your query is that you cannot write q2 = '1' or '2'. It is invalid syntax. Use in instead:

where q2 in ('0','1') and q3 in ('0','1','2','3') and q4 in ('0','1','2','3','4','5')

Upvotes: 3

Related Questions