Jake Girouard Jake Girouard - 4 months ago 8
SQL Question

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.

Answer

Two problems:

  1. Logic error - 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')
Comments