SQL Boolean Expressions for the Unwary


A Boolean expression is a way of expressing a condition or criterion which can be either true or false. Applying this to database tables we find that we often want to see "all of table x where column 1 is equal to this value, and column 3 is one of these four values". This page is intended to help you construct boolean expressions for choosing data out of tables offered on this webserver.

This interface permits you to simplify the normal syntax of a SQL expression, so as to make it easier for the non-SQL-literate user to formulate selection expressions.

After each brief explanation of a SQL Boolean operator comes a list of examples that you might actually enter into a blank on one of the query forms. If you use one of these syntactic examples and get an error, please send mail to webmaster (see below) right away!

Equals =
The syntax for equality is ColName = Value. Technically speaking, SQL syntax requires quotes around the value for string and date fields. However, you can ignore this because the interface will interpolate quotes for you where needed. You can enter = Fred with impunity! For "not equals" just put an exclamation mark in front of the equals sign: ColName != Value
Greater and Less >, <
Again, the syntax is simple: Colname > Value. Also, negation is done with the exclamation mark as above. Quoting rules are always the same, but comparison is alphabetic for strings and numeric for numbers.
Between between
If you want records where ColName falls between Value1 and Value2, the syntax is still pretty simple: ColName between Value1 and Value2 In point of fact you can leave out the syntactically necessary and because the interface will correct this lapse for you. Also, if you enter your values in the wrong order the (greater to lesser) the interface will fix that. Negation of between is done with not: ColName not between Value1 and Value2. Quoting rules never change.
Among in
If you want records where ColName is one of a discrete list of values, the syntax is ColName in (Val1, Val2, Val3...). The interface will accept the equivalent (not SQL, though!) syntax one of Val1 Val2 Val3 Val4... You can negate this with not as with between
Like like
You can match partial strings using the like operator along with a percent sign for wildcarding. like AAA% will match all strings starting with AAA. In a correct SQL expression, AAA% should be in quotes, but the interface is smart enough to provide them for you.
Sounds Like sounds_like
When searching certain types of data, a fuzzy soundalike match is useful. This interface provides a pseudo-operator sounds_like to perform SQL soundex matching.
If you are SQL-literate already, you will have noted that some rather bizarre syntactical variants are supported here (for the benefit of the non-SQL speaking user. For example, > 5 < 7 will be accepted by the interface and turned into (ColName > 5 and ColName < 7). Do note however, that if you construct your own where-clause in the text box provided, you must use canonical SQL syntax -- there will be no help from the software in this case! The advantage of using the text box, for the more advanced user, is that you can construct more complex clauses involving or as well as and, and you can control evaluation order by means of parentheses.

webmaster@ucolick.org
De Clarke
UCO/Lick Observatory
University of California
Santa Cruz, CA 95064
Tel: +1 408 459 2630
Fax: +1 408 454 9863