2 Restricting and Sorting Data
2 Restricting and Sorting Data
http://www.tutorialspoint.com/sql_certificate/restricting_and_sorting_data.htm
Copyright tutorialspoint.com
The essential capabilities of SELECT statement are Selection, Projection and Joining. Displaying
specific columns from a table is known as a project operation. We will now focus on displaying
specific rows of output. This is known as a select operation. Specific rows can be selected by
adding a WHERE clause to a SELECT query. As a matter of fact, the WHERE clause appears just
after the FROM clause in SELECT query hierarchy. The sequence has to be maintained in all
scenarios. If violated, Oracle raises an exception.
Syntax:
SELECT *|{[DISTINCT] column| expression [alias],..}
FROM table
[WHERE condition(s)]
In the syntax,
WHERE clause is the keyword
[condition] contains column names, expressions, constants, literals and a comparison
operator.
Suppose that your manager is working on the quarterly budget for your organization. As part of
this activity, it is necessary to produce a listing of each employee's essential details, but only for
employees that are paid at least $25,000 annually. The SQL query below accomplishes this task.
Note the use of the WHERE clause shown in bold text.
SELECT Employee_ID, Last_Name, First_Name, Salary
FROM employees
WHERE Salary >= 25000;
EMPLOYEE_ID
---------88303
88404
88505
LAST_NAME
FIRST_NAME
SALARY
--------------- --------------- ----------Jones
Quincey
$30,550.00
Barlow
William
$27,500.00
Smith
Susan
$32,500.00
3 rows selected
Points to be noted A SELECT clause can contain only one WHERE clause. However, multiple filter conditions can
be appended to WHERE clause using AND or OR operator.
The columns, literals or expressions in a predicate clause must be of similar or
interconvertible data types.
Column alias cannot be used in the WHERE clause.
Character literals must be enclosed within single quotation marks and are case sensitive.
Date literals must be enclosed within single quotation marks and are format sensitive.
Default format is DD-MON-RR.
Comparison Operators
Comparison operators are used in predicates to compare one term or operand with another term.
SQL offers comprehensive set of equality, inequality and miscellaneous operators. They can be
used depending on the data and filter condition logic in the SELECT query. When you use
comparison operators in a WHERE clause, the arguments objectsorvaluesyouarecomparing on both sides
of the operator must be either a column name, or a specific value. If a specific value is used, then
the value must be either a numeric value or a literal string. If the value is a character string or
date, you must enter the value within single quotation marks .
Oracle has nine comparison operators to be used in equality or inequality conditions.
Operator
=
<
>
>=
<=
!=
<>
!>
!<
Meaning
equal to
less than
greater than
greater than or equal to
less than or equal to
not equal to
not equal to
not greater than
not less than
The IN Operator
The IN operator is used to test a column value in a given set of value. If the column can be equated
to any of the values from the given set, the condition is validated. The condition defined using the
IN operator is also known as the membership condition.
For example, the WHERE condition SALARY IN 1500, 3000, 2500 in a SELECT query will restrict the
rows where salary is either of 1500, 3000 or 2500.
Logical Operators
Multiple filter conditions can be added to the WHERE clause predicate. More than one condition
can be combined together using logical operators AND, OR and NOT.
AND: joins two or more conditions, and returns results only when all of the conditions are
true.
OR: joins two or more conditions, and it returns results when any of the conditions are true.
NOT: negates the expression that follows it.
The AND operator links two or more conditions in a WHERE clause and returns TRUE only if all the
conditions are true. Suppose that a manager needs a list of female employees. Further, the list
should only include employees with last names that begin with the letter "E" or that come later in
the alphabet. Additionally, the result table should be sorted by employee last name. There are two
simple conditions to be met. The WHERE clause may be written as: WHERE Gender = 'F' AND
last_name > 'E'.
SELECT last_name "Last Name", first_name "First Name", Gender "Gender"
FROM employees
WHERE Gender = 'F' AND last_name > 'E'
ORDER BY last_name;
The OR operator links more than one condition in a WHERE clause and returns TRUE if either of
the condition returns true. Suppose that your organizational manager's requirements change a bit.
Another employee listing is needed, but in this listing the employees should: 1 be female or, 2 have
a last name that begins with the letter "T" or a letter that comes later in the alphabet. The result
table should be sorted by employee last name. In this situation either of the two conditions can be
met in order to satisfy the query. Female employees should be listed along with employees having
a name that satisfies the second condition.
The NOT operator is used to negate an expression or conition.
Sorting can be based on numeric and date values also. Sorting can also be done based on multiple
columns.
By default, the ORDER BY clause will sort output rows in the result table in ascending order. We
can use the keyword DESC shortfordescending to enable descending sort. The alternative default is
ASC which sorts in ascending order, but the ASC keyword is rarely used since it is the default.
When the ASC or DESC optional keyword is used, it must follow the column name on which you are
sorting in the WHERE clause.
Positional Sorting - Numeric position of the column in the selected column list can be given in
ORDER BY clause, instead of column name. It is mainly used in UNION queries discussedlater. The
Query orders the result set by salary since it appears 2nd in the column list.
Substitution Variables
When a SQL query has to be executed more than once for the different set of inputs, substitution
variables can be used. Substitution variables can be used to prompt for user inputs before the
query execution. They are widely used in query based report generation which takes data range
from the users as input for the conditional filtering and data display. Substitution variables are
prefixed by a single-ampersand & symbol to temporarily store values. For example,
SELECT EMPLOYEE_ID, LAST_NAME, SALARY
FROM employees
WHERE LAST_NAME = &last_name
OR EMPLOYEE_ID = &EMPNO;
When the above SELECT query is executed, oracle identifies the '&' as substitution variable. It
prompts user to enter value for 'last_name' and 'EMPNO' as below.
Enter value for last_name:
Enter value for empno:
Once the user provides inputs to both the variables, values are substituted, query is verified and
executed.
Points to be noted If the variable is meant to substitute a character or date value, the literal needs to be
enclosed in single quotes. A useful technique is to enclose the ampersand substitution
variable in single quotes when dealing with character and date values.
Both SQL Developer and SQL* Plus support the substitution variables and the
DEFINE/UNDEFINE commands. Though SQL Developer or SQL* Plus does not support
validation checks exceptfordatatype on user input.
You can use the substitution variables not only in the WHERE clause of a SQL statement, but
also as substitution for column names, expressions, or text.
Note that the same value of &DT is substituted twice in the above query. So, its value once given
by the user will be substituted at two places.
SALARY
------5000