Restrict Sort
Restrict Sort
1
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
Lesson Aim
2
Limiting Rows Using a Selection
SELECT *
FROM emp;
SELECT *
FROM emp
WHERE deptno=10;
3
Limiting Rows Selected
FROM table
WHERE condition ;
You can restrict the rows returned from the query by using the WHERE clause.
A WHERE clause contains a condition that must be met, and it directly follows
the FROM clause.
In the syntax:
The WHERE clause can compare values in columns, literal values, arithmetic
expressions, or functions. The WHERE clause consists of three elements:
•Column name
•Comparison operator
4
Using the WHERE Clause
In the example, the SELECT statement retrieves the name, job title,
CLERK.
Note that the job title CLERK has been specified in uppercase to
ensure that the match is made with the job column in the EMP
5
Character Strings and Dates
•Character strings and date values are
enclosed in single quotation marks.
Character strings and dates in the WHERE clause must be enclosed in single quotation marks (' ').
All character searches are case sensitive. In the following example, no rows are returned because the
Oracle stores dates in an internal numeric format, representing the century, year, month, day, hours,
Note: Changing default date format will be covered in Lesson 3. Number values are not enclosed
6
Comparison Operators
Operator Meaning
= Equal to
Comparison Operators
Comparison operators are used in conditions that compare one expression to another.
Syntax
Examples
7
Using the Comparison Operators
SQL> SELECT ename, sal, comm
FROM emp
WHERE sal<=comm;
commission from the EMP table, where the employee salary is less than or
supplied to the WHERE clause. The two values being compared are taken
8
Other Comparison Operators
Operator Meaning
The SELECT statement on the slide returns rows from the EMP
table for any employee whose salary is between $1000 and $1500.
The IN Operator
The slide example displays employee number, name, salary, and manager's employee
number of all the employees whose manager's employee number is 7902. 7566. or
7788,
The IN operator can be used with any datatype. The following example returns a row
from the EMP table for any employee whose name is included in the list of names in
the WHERE clause:
SQL> SELECT empno, ename, mgr, deptno
2FROM emp
3WHERE ename IN ( 'FORD' , 'ALLEN' ) ;
If characters or dates are used in the list, they must be enclosed in single quotation
marks (' ')-
Using the LIKE Operator
Use the LIKE operator to perform wildcard searches of valid
search string values.
Search conditions can contain either literal characters or
numbers.
1. % denotes zero or many characters, denotes one character.
2. _ denotes one character.
You may not always know the exact value to search for. You can select rows that match a
character pattern by using the LIKE operator. The character pattern-matching operation is
referred to as a wildcard search. Two svmbols can be used to construct the search string.
Symbol Description
The SELECT statement above returns the employee name from the EMP table for any
employee whose name begins with an "S”. Note the uppercase "S" . Names beginning with
an "s” will not be returned.
The LIKE operator can be used as a shortcut for some BETWEEN comparisons. The
following example displays names and hire dates of all employees who joined between
January 1981 and December 1981:
ENAME
MARTIN
JAMES
WARD
You can use the ESCAPE identifier to search for "%" or "_”.
The ESCAPE option identifies the backslash (\) as the escape character. In the pattern,
the escape character precedes the underscore ( _ ). This causes the Oracle Server to
interpret the underscore literally.
Using the IS NULL Operator
ENAME
MGR
KING
The IS NULL operator tests for values that are null. A null value means the value is unavailable,
unassigned, unknown, or inapplicable. Therefore, you cannot test with (=) because a null value cannot be
equal or unequal to any value. The slide example retrieves the name and manager of all employees who do
not have a manager.
For example, to display name Job title, and commission for all employees who are not entitled to get a
commission, use the following SQL statement.
SELECT ename, job
FROM emp
WHERE comm IS NULL;
ENAME JOB
SMITH CLERK
JONES MANAGER
BLAKE MANAGER
CLARK MANAGER
SCOTT ANALYST
KING PRESIDENT
ADAMS CLERK
JAMES CLERK
FORD ANALYST
MILLER CLERK
10 rows selected.
Logical Operators
Operator Meaning
AND Returns TRUE if both component conditions are TRUE
OR Returns TRUE if either component condition is TRUE
NOT Returns TRUE if the following condition is FALSE
Logical Operators
•AND
•OR
•NOT
All the examples so far have specified only one condition in the WHERE
clause. You can use several conditions in one WHERE clause using the AND
and OR operators.
Using the AND Operator
AND requires both conditions to be TRUE
The OR Operator
In the example, either condition can be true for any record to be selected. Therefore,
an employee who has a job title of CLERK or earns more than $1100 will be
selected.
The OR Truth Table
The following table shows the results of combining two expressions with OR:
ENAME JOB
ALLEN SALESMAN
WARD SALESMAN
MARTIN SALESMAN
KING PRESIDENT
TURNER SALESMAN
The slide example displays name and job title of all the employees whose job title is not
CLERK. MANAGER, or ANALYST.
Note: The NOT operator can also be used with other SQL operators, such as BETWEEN,
LIKE, and
NULL.
2 NOT
3 AND
4 OR
FROM emp
The first condition is that job is PRESIDENT and salary is greater than 1500.
"Select the row if an employee is a PRESIDENT and earns more than $1500
or if the employee is a SALESMAN"
Rules of Precedence
FROM emp
Using Parentheses
In the example, there are two conditions: • The first condition is that job is
PRESIDENT or SALESMAN.
The second condition is that salay is greater than 1500. Therefore, the
SELECT statement reads as follows:
Syntax
SELECT expr
FROM table
[WHERE condition(s) ]
where:
ORDER BY specifies the order in which the retrieved rows are displayed
ASC orders the rows in ascending order (this is the default order)
• Null values are displayed last for ascending sequences and first for
descending sequences.
To reverse the order in which rows are displayed, specify the keyword DESC
after the column name in the ORDER BY clause. The slide example sorts
the result by the most recently hired employee.
Sorting by Column Alias
SQL> SELECT empno, ename, sal*12 "annsal"
FROM emp
ORDER BY "annsal";
You can use a column alias in the ORDER BY clause. The slide example sorts
FROM table
[WHERE condition(s) ]
Summary
In this lesson, you have learned about restricting and sorting rows returned by the
SELECT statement. You have also learned how to implement various operators.
Practice Overview
Practice Overview
This practice gives you a variety of exercises using the WHERE clause and the
ORDER BY clause.
Practice 2
1. Create a query to display the name and salary of employees earning more
than $2850. Save your SQL statement to a file named p2ql.sql. Run your
query.
ENAME SAL
JONES 2975
SCOTT 3000
KING 5000
FORD 3000
2. Create a query to display the employee name and department number for
employee number 7566.
ENAME DEPTNO
JONES 20
3. Modify p2ql.sql to display the name and salary for all employees whose
salary is not in the range of $1500 and $2850. Resave your SQL statement
to a file named p2q3.sql. Rerun your query.
ENAME SAL
SMITH 800
WARD 1250
JONES 2975
MARTIN 1250
SCOTT 3000
KING 5000
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
10 rows selected.
Practice 2 (continued)
4. Display the employee name. job. and start date of employees hired between
February 20. 1981, and May 1. 1981. Order the query in ascending order by
start date.
ENAME DEPTNO
ALLEN 30
BLAKE 30
CLARK 10
JAMES 30
KING 10
MARTIN 30
MILLER 10
TURNER 30
WARD 30
9 rows selected
6. Modify p2q3.scj/to list the name and salary of employees who earn more than
$1500 and are in department 10 or 30. Label the columns Employee and Monthly
Salary, respectively. Resave your SQL statement to a file named p2q6.sql Rerun
your query.
8. Display the name and job title of all employees who do not have a manager.
ENAME JOB
KING PRESIDENT
9. Display the name, salary, and commission for all employees who earn commissions. Sort
10. Display the names of all employees where the third letter of their name is an A.
ENAME
BLAKE
CLARK
ADAMS
11. Display the name of all employees who have two Ls in their name and are in department 30
or their manager is 7782.
ENAME
ALLEN
MILLER
Practice 2 (continued)
12. Display the name, job, and salary for all employees whose job is Clerk or
Analyst and their salary is not equal to $1000, $3000. or $5000.
13. Modify p2q6.sql to display the name, salary, and commission for all employees
whose commission amount is greater than their salary increased by 10%. Rerun
your query. Resave your query as p2q!3.sql.
Employee Monthly Salary COMM
MARTIN 1250 1400