Saturday, September 22, 2007

Restricting and Sorting Data

Objectives
After completing this lesson, you should be able to do the following:
• Limit the rows retrieved by a query
• Sort the rows retrieved by a query

Limiting Rows Using a Selection


Limiting the Rows Selected
• Restrict the rows returned by using the WHERE clause.
• The WHERE clause follows the FROM clause.

Using the WHERE Clause

Character Strings and Dates
• Character strings and date values are enclosed in single quotation marks.
• Character values are case sensitive, and date values are format sensitive.
• The default date format is DD-MON-RR.

Comparison Conditions

Using Comparison Conditions
Other Comparison Conditions

Using the BETWEEN Condition

Using the IN Condition


Using the LIKE Condition
• Use the LIKE condition to perform wild card searches of valid search string values.
• Search conditions can contain either literal characters or numbers:
– % denotes zero or many characters.
– _ denotes one character.
• You can combine pattern-matching characters.
• You can use the ESCAPE identifier to search for the actual % and _ symbols.


Using the NULL Conditions
Test for nulls with the IS NULL operator.

Logical Conditions

Using the AND Operator
AND requires both conditions to be true.

Using the OR Operator
OR requires either condition to be true.

Using the NOT Operator


Rules of Precedence
Override rules of precedence by using parentheses.
Use parentheses to force priority.

ORDER BY Clause
• Sort rows with the ORDER BY clause
– ASC: ascending order (the default order)
– DESC: descending order
• The ORDER BY clause comes last in the SELECT statement.

Sorting in Descending Order

Sorting by Column Alias

Sorting by Multiple Columns
• The order of ORDER BY list is the order of sort.
• You can sort by a column that is not in the SELECT list.

Summary
In this, you should have learned how to:

• Use the WHERE clause to restrict rows of output
– Use the comparison conditions
– Use the BETWEEN, IN, LIKE, and NULL conditions
– Apply the logical AND, OR, and NOT operators

• Use the ORDER BY clause to sort rows of output


2 comments:

Lucifer_is_back said...

nice tutorials
basics are explained in detail

Bangalore School of Business said...

examples are basic and explained beautifully.
try to include little complex examples