Smaller than but larger than

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Smaller than but larger than

Post by JellyFish »

Hello wonderful community! I haven't been on in awhile so I thought I'd might ask a quick SQL question.

Is there a way, in SQL, to accomplish a comparison that says x is larger y than but also smaller than z, like:

Code: Select all

 
... WHERE COLUMN < 11 AND COLUMN > 0; #column is x, 11 is y, and 0 is z.
 
Is there some operator that allows me to do this differently, or is the way I have just done it the best? I guess what I'm asking is, is there a better way to check to see if a value is within a specific range from y-z than what I've just wrote?

Thanks for reading.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Smaller than but larger than

Post by jackpf »

You can use BETWEEN...AND.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Smaller than but larger than

Post by JellyFish »

Oh thanks, just what I was looking for! You're awesome!

EDIT: Also, a note to readers: I would have to change my y and z variables to 10 and 1 respectively. This is because BETWEEN ... AND matches true when the expression is equal to either the min or max.
Post Reply