Code Tech
Code Tech

Reputation: 61

Sql query for multiple ANDs use in a single condition

I need an SQL query , with multiple AND's. Let me explain with an example,

For example I want to search in a database for a property , who's price is greater than 1000 and less than 2000 (price is between 1000-2000), and its area is greater than 1000 sqft. and less than 2000 sq ft. (area is between 1000-2000). So i was guessing that the query could be,

SELECT *
FROM table_name
WHERE (price>1000 AND price<2000) AND (area>1000 AND area<2000)

this is something i need ! Thank you

Upvotes: 0

Views: 1782

Answers (3)

Rising Punjab
Rising Punjab

Reputation: 21

Use this, i think it will solve your problem. It works for me:

SELECT * FROM table_name WHERE price and area BETWEEN 1001 and 1999.

If we have same values of the parameters, then we can add the condition with and all the parameters.

Upvotes: 0

Mahesh Madushanka
Mahesh Madushanka

Reputation: 2988

Use between instead of and

SELECT * FROM table_name WHERE (price between 1001 AND 1999) AND (area between 1001 AND 1999) 

Upvotes: 0

lloiacono
lloiacono

Reputation: 5030

Your original query looks fine to me, but you can also use BETWEEN if you like, try this:

SELECT * FROM table_name WHERE (price BETWEEN 1001 AND 2000) AND (area BETWEEN 1001 AND 2000); 

expr BETWEEN min AND max

If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <= expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 12.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments

Upvotes: 1

Related Questions