SQL Expressions, Conditions, and Operators
In Lesson 2, “Introducing the Query,” you used SELECT and FROM to manipulate data in interesting (and useful) ways. In this lesson, you learn more about SELECT and FROM. You will expand the basic query with some new terms, a new clause, and a group of handy items called operators. By the end of this lesson, you will
- Know what an expression is and how to use it.
- Know what a condition is and how to use it.
- Be familiar with the basic uses of the WHERE clause.
- Be able to use arithmetic, comparison, character, logical, and set operators.
- Have a working knowledge of some miscellaneous operators.
Working with Query Expressions
The definition of an expression is simple: An expression returns a value. Expression types are very broad, covering different data types such as String, Numeric, and Boolean. In fact, pretty much anything following a clause (SELECT or FROM, for example) is an expression. In the following example, AMOUNT is an expression that returns the value contained in the AMOUNT column:
SELECT AMOUNT FROM CHECKS;
Of course, the following is also considered a numerical expression. Remember that the key to an expression is that it returns a value.
SELECT AMOUNT*10 FROM CHECKS;
In the following statement, NAME, ADDRESS, PHONE, and ADDRESSBOOK are expressions:
SELECT NAME, ADDRESS, PHONE FROM ADDRESSBOOK;
Now, examine the following WHERE clause:
WHERE NAME = 'BROWN'
It contains a condition, NAME = 'BROWN', which is an example of a Boolean expression. NAME = 'BROWN' will be either TRUE or FALSE, depending on the condition =.