Databases Reference
In-Depth Information
Table 5-5. The BETWEEN and IN Operators
Operator
Description
Example
BETWEEN
WHERE extension BETWEEN 400 AND 500 returns
the rows where Extension is between 400 and
500, inclusive.
True if a value is within a range.
IN
WHERE city IN ('Seattle', 'London') returns
True if a value is in a list. The list
can be the result of a subquery.
the rows where City is either Seattle or London.
Combining Predicates
Quite often you'll need to use more than one predicate to filter your data. You can use the
logical operators shown in Table 5-6.
Table 5-6. SQL Logical Operators
Operator
Description
Example
AND
HERE (title LIKE 'Sales%' AND lastname
Combines two expressions,
='Peacock')
evaluating the complete
expression as true only if both
are true
NOT
WHERE NOT (title LIKE 'Sales%' AND lastname
='Peacock')
Negates a Boolean value
OR
WHERE (title = 'Peacock' OR title = 'King')
Combines two expressions,
evaluating the complete
expression as true if either
is true
When you use these operators, it's often a good idea to use parentheses to clarify the
conditions. In complex queries, this may be absolutely necessary.
Sorting Data
After you've filtered the data you want, you can sort the data by one or more columns and
in a certain direction. Since tables are by definition unsorted, the order in which rows are
retrieved by a query is unpredictable. To impose an ordering, you use the ORDER BY clause.
ORDER BY <column> [ASC | DESC] {, n}
Search WWH ::




Custom Search