1

I am running my first SQL query and not sure where my error lies. The first line (for the date) seems to run correctly. None of the other lines seem to be working IE it returns a results with ratings of less than 3.5. I am not receiving an error message when I run the query, it runs successfully. If possible explain the 'concept' that i'm missing.

It seems like this should be a simple query, it pulls from one table 'app'. 'rating' 'downloads' and 'lastUpdate' are all columns.

SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 
downloads = '1 - 5' OR '10 - 50' OR '50 - 100' OR '100 - 500'
5
  • Formatting in SQL usually puts the AND on the new line, with the next statement. Not necessary or anything, just the way it's usually done. Commented Apr 23, 2012 at 20:57
  • downloads = '1 - 5' OR downloads ='10 - 50' OR downloads ='50 - 100' OR downloads ='100 - 500' => You have to specify the column again Commented Apr 23, 2012 at 20:57
  • @KDEx John is correct. Check out his comment. Commented Apr 23, 2012 at 20:58
  • When you say downloads = '1 - 5' do you mean that you want rows where the field downloads is literally '1-5' or are you trying to query where downloads is a value between 1 and 5? Commented Apr 23, 2012 at 20:59
  • @AshBurlaczenko according to me, obviously. Really, I thought that was the only way I had seen it done. I may be wrong, or may just have been looking in the wrong places, though. Commented Apr 23, 2012 at 21:04

3 Answers 3

4

Try this

SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 
(downloads IN ('1 - 5','10 - 50','50 - 100','100 - 500'))

I'm not sure your syntax was correct to string or values together, and if it is you need parenthesis or any of those or conditions will return true for the whole query.

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

1 Comment

You could also point out that rating >= '3.5' may not be want they want. '3.5' probably needs converting to a number.
3

You can't string together ORs like that. Try this alternate version:

SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 

(downloads = '1 - 5' OR 
 downloads ='10 - 50' OR
 downloads ='50 - 100' OR 
 downloads ='100 - 500')

You can shorten this syntax a bit using the IN clause as follows.

   downloads IN('1-5','10-50','90-100','100-500')

This assumes that downloads is a varchar or text type containing values like '1-5' and that it isn't a numeric and you are expecting this to return 1,2,3,4,5.

If you mean the latter you need to specify those parameters like this

(downloads between 1 and 5 OR 
 downloads between 10 and 50 OR
 downloads between 50 and 100 OR 
 downloads between 100 and 500)

Comments

0
SELECT * FROM app
WHERE lastUpdate >= '2011-08-01' AND 
rating >= '3.5' AND 
(downloads between 1 AND 5 OR downloads between 10 AND 50 OR downloads between 50 AND 100 OR downloads between 100 AND 500)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.