SQL Conditions

Common conditional logic statements for SQL

 =Equal to. Tests for equality
<>
Not equal to. Tests for inequality
<
Less than. Tests that the value on the left is less than the value on the right
<=
Less than or equal to. Tests that the value on the left is less than or equal to the value on the right.
>
Greater than. Tests that the value on the left is greater than the value on the right.
>=
Greater than or equal to. Tests that the value on the right is greater than or equal to the value on the left
BETWEEN
Tests that a value is in the range between two values; the range is inclusive
EXISTS
Tests for the existence of rows returned by a subquery
IN
Tests to see whether a value is contained within a list of values.
IS NULL
Tests to see whetehr a column contains a NULL value.
IS NOT NULL
Tests to see whether a column contains a non-NULL value.
LIKE
Tests to see whether a value matches a specified pattern
NOT
Negates any test

Extract taken from 'ColdFusion Web Application Contruction Kit' book

Comments
Ben Nadel's Gravatar I wish that BETWEEN wasn't inclusive. Or rather I wish it was from-inclusive, to-exclusive. That's what stops me from using it. I think the single-sided inclusive is the most general use-case.
# Posted By Ben Nadel | 31/10/08 16:06
Will Wilson's Gravatar Don't you think it makes more sense for it to be inclusive? Say for instance you're searching between date ranges, a typical user would expect results for the dates they selected + anything in between
# Posted By Will Wilson | 31/10/08 21:44
Ben Nadel's Gravatar @Will,

That's funny you mention that because dates was exactly the use-case that I was thinking about when considering the non-inclusive one-end :) I guess its just how someone comes down to using it. I find that I most often write my queries like this:

date >= X
AND
date < Y

For example, find "All XYZ that ocurred in a single week". This is inclusive on one end only:

>= Sunday 12:00 AM
< Sunday (week later) 12:00 AM

This is the majority of the date ranges that I use.
# Posted By Ben Nadel | 31/10/08 21:54
Will Wilson's Gravatar @Ben,

Nabbed this off the w3c site :
----------
<i>Note: The BETWEEN operator is treated differently in different databases.

In some databases a person with the LastName of "Hansen" or "Pettersen" will not be listed (BETWEEN only selects fields that are between and excluding the test values).

In other databases a person with the last name of "Hansen" or "Pettersen" will be listed (BETWEEN selects fields that are between and including the test values).

And in other databases a person with the last name of "Hansen" will be listed, but "Pettersen" will not be listed (BETWEEN selects fields between the test values, including the first test value and excluding the last test value).

Therefore: Check how your database treats the BETWEEN operator.</i>
-----------

According to their site, MS SQL uses inclusive, but mySQL only states that the function "Check[s] whether a value is <strong>within</strong> a range of values"

I guess its all down to user preference, and as you stated there is the < and > alternatives :) (I dont think i'll ever use BETWEEN either due to the variation between databases)
# Posted By Will Wilson | 01/11/08 00:40
Will Wilson's Gravatar Not to mislead anyone - mySQL actually supports BETWEEN in the same way MSSQL does - as greater/less than or equal to (should have read on a bit on the documentation).
# Posted By Will Wilson | 01/11/08 00:56
Ben Nadel's Gravatar That's good to know. I did not realize that this might even vary from engine to engine.
# Posted By Ben Nadel | 02/11/08 20:11
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner