September 5th, 2008

ORDER BY (sorting the Results), Part 2 of 2

Posted by admin in D. WHERE Clause

The sort sequence for numeric data, dates, and times is easily obvious.
When you look at the sort sequence for alphanumeric data with special characters, the order is sometimes not so obvious. See Collating Sequence.
If the ORDER BY columns do not identify each row uniquely, rows with duplicate values in the ORDER BY clause will be listed in arbitrary order.
You can use expressions in place of the ORDER BY columns.Nulls complicate the situation.
Even though nulls mean that there is no value, nulls are considered equal when sorted.
The SQL standard leaves it up to the RDBMS system to decide if nulls are greater or less than non-null values.

SQL allows you to use a relative column number in place of the column name in the ORDER BY clause.
This relative column number points to the column name in the SELECT clause.
This practice is a bad idea because the order of the SELECTed columns could be changed, and this would mean that the relative column numbers now point to different column names and is confusing.

You can reference column alias names in the ORDER BY clause. See: AS (Column Aliases).
You can intermingle, column names, alias column names, expressions, and relative column numbers.

> Access, MySQL, and SQL Server treat nulls as the lowest possible value.
> DB2, Oracle, and PostgreSQL treat nulls as the highest value.
> Access won’t let you sort expressions.
> Oracle won’t let you sort BLOB, CLOB, or NCLOB.
> SQL Server won’t let you sort IMAGE, TEXT, or NTEXT.

September 4th, 2008

ORDER BY (Sorting the Results), Part 1 of 2

Posted by admin in D. WHERE Clause

The SQL standard says that row order is irrelevant. The query results are therefore unordered and arbitrary.
You can use the ORDER BY clause to sort rows in the result table by a column or columns in ascending (ASC) order (lowest to highest) or descending (DESC) order (highest to lowest). See Collating Sequence.

     SELECT   [ALL | DISTINCT]
              [* | column1[, column2, ...]]
       FROM   [table1 correlation, ...
             | view1 correlation, ...]
      WHERE    condition1[, condition2, ...]
      GROUP BY column1[, column2, ...]
     HAVING    condition1[, condition2, ...]
      ORDER BY column1 [ASC | DESC][, column2 [ASC | DESC], ...];

> The ORDER BY clause is always the last clause in a SELECT statement when used.
> The columns in ORDER BY do not have to be listed in the SELECT clause.
> Specify ASC for ascending, or DESC for descending. If nothing is specified, it defaults to ASC.
> The GROUP BY and HAVING clauses will be discussed later. See GROUP BY and HAVING.

September 3rd, 2008

IN (Matching to a List)

Posted by admin in D. WHERE Clause

The IN operator is used to find if a given value matches a value in a specified list.

SELECT column1, column2, ...
  FROM table1, table2, ...
 WHERE testcolumn [NOT] IN (value1, value2, ...;

> IN works with character strings, numbers, and datetimes. The testcolumn can be an expression.
The list value must be the same comparable data type as the testcolumn.
> The IN list is contained in parentheses and contains one or more values separated by commas.
The values in the list can be in any order.
> You can combine IN conditions with other conditions using AND and OR.
> NOT IN is used to find values that are not in the list values.
> String comparisons may or may not be case sensitive depending on your RDBMS.
> The IN operator is shorter than using many OR conditions and runs faster. For example:

WHERE testcolumn IN            or  WHERE (testcolumn = value1)
      (value1, value2, value3)        OR (testcolumn = value2)
                                      OR (testcolumn = value3)

For speed, list the values most likely to find a match first.

Next Page »