03 Restricting and Sorting Data
03 Restricting and Sorting Data
Sorting Data
Lesson Objectives
After completing this lesson, you should be able to:
• Applying Rules of precedence for operators in an expression
• Limiting Rows Returned in a SQL Statement
• Sorting Data
• Using Substitution Variables
• Using the DEFINE and VERIFY commands
Limiting Rows Using a WHERE clause
Limiting Rows Using a Selection
• Restrict the rows that are returned by using the WHERE clause
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
• Where clause is optional, condition can include column names, character strings,
expressions, constants and comparison operator
• Character strings and date values are enclosed with single quotation marks.
• Character values are case-sensitive and date values are format-sensitive.
• The default date display format is DD-MON-RR
Limiting Rows Using a Selection
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90;
SELECT last_name
FROM employees
WHERE hire_date = '17-FEB-96';
Limiting Rows Using a Selection – Comparison
Operator
Operator Function
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> or != Not equal to
Between … AND … Between two values inclusive
In (Set) Match any of the list of values
Like Match a character pattern
Is Null Is a null value
Limiting Rows Using a Selection – Comparison
Operator
• Display employees whose salary is less than or equal to 3000;
SELECT last_name, salary
FROM employees
WHERE salary <= 3000 ;
• You can combine the two wildcard characters (%, _) with literal characters for pattern
matching
• The Like operator can also be used without wildcard characters, but it is nonsense
because it will be equal to ( = ) operator and Like is slower than ( = ) operator
Limiting Rows Using a Selection – Comparison
Operator (Wildcard character)
• Search conditions can contain either literal characters or numbers:
• % denotes zero or many characters
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
Limiting Rows Using a Selection – Comparison
Operator (Wildcard character)
• Search for employees whose job_id contain the character sequence “SA_”:
Operator Function
AND Returns True is both component conditions are true
OR Returns True if either component conditions are true
NOT Return True if the following condition is false
Conditions Using the Logical Operators - AND
• Both the component conditions must be true for any record to be selected
• Truth Table AND TRUE FALSE NULL
TRUE TRUE FALSE NULL
FALSE FALSE FALSE FALSE
NULL NULL FALSE NULL
UNDEFINE employee_num
Using the VERIFY Command
• Use the VERIFY command to toggle the display of the substitution variable, both
before and after SQL Developer replaces substitution variables with values:
SET VERIFY ON
SELECT employee_id, last_name, salary
FROM employees
WHERE employee_id = &employee_num;
Substitution variables –
ACCEPT/PROMPT
Using the ACCEPT/PROMPT Command
• Most reliable and robust method for getting input from the user
• The ACCEPT command takes the input from the user and stores it in a user variable
• While the PROMPT command is used to display a message to the user for supplying
a brief explanation of what your script is going to accomplish
• To undefine the value, you are require to issue the UNDEFINE command
Using the ACCEPT/PROMPT Command
ACCEPT min_salary PROMPT 'Please enter the MINIMUM salary';
ACCEPT max_salary PROMPT 'Please enter the MAXIMUM salary';
Select employee_ID, first_name, last_name, salary
from employees
where salary BETWEEN &min_salary and &max_salary;
Exercise
1. Which two statements are true regarding the ORDER BY clause? (Choose two.)
A. It is executed first in the query executionSELECT name, address FROM customers;
B. It must be the last clause in the SELECT statement
C. It cannot be used in a SELECT statement containing a HAVING clause
D. You cannot specify a column name followed by an expression in this clause
E. You can specify a combination of numeric positions and column names in this
clause
Exercise
2. The STUDENT_GRADES table has these columns:
STUDENT_ID NUMBER(12)
SEMESTER_END DATE
GPA NUMBER(4, 3)
The registrar requested a report listing the students' grade point averages (GPA) sorted from highest grade point
average to lowest. Which statement produces a report that displays the student ID and GPA in the sorted order
requested by the registrar?
PRODUCT_ID NUMBER(4)
PRODUCT_NAME VARCHAR2(45)
PRICE NUMBER(8, 2)
SELECT *
FROM PRODUCTS
ORDER BY price, product_name;
A promotional sale is being advertised to the customers in France. Which WHERE clause identifies customers that are located in
France?
A. WHERE lower(country_address) = "france"
B. WHERE lower(country_address) = 'france'
C. WHERE lower(country_address) IS 'france'
D. WHERE lower(country_address) = '%france%'
E. WHERE lower(country_address) LIKE %france%
Exercise
6. Which substitution variable would you use if you want to reuse the variable without
prompting the user each time?
A. &
B. ACCEPT
C. PROMPT
D. &&
Summary
• In this lesson, 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 operators
• Apply the logical AND, OR, and NOT operators
• Use the ORDER BY clause to sort rows of output:
• Use ampersand substitution and ACCEPT to restrict and sort output at
run time
Assignment – HR Schema
1. Display the job and salary for all employees where the salary is less than 5,000
2. Display the names and salaries of people working in department 80 who earn
a salary of more than 10000
3. Display a list of employees who were hired during or after the year 2002 order
by employee name descending
4. Display a list of departments that do not start with the letter R
5. Display a list of employees whose commission is greater than 0.5
Assignment – HR Schema
6. Display a list of employees in sequence of their hire date, with the most
recently hired employee showing at the top of the list
7. Display the names of all employees whose salary is between 2500 and 3850
inclusive.
8. Display the name and job all employees who haven’t been assigned a
department
9. Display the name and salary of employees earning more than 5000
Assignment – HR Schema
10. Display the employee name and department number for the employee number
151
11. Display the employee name and salary for all employees whose salary is not in the
range 3000 to 11000
12. Display the employee name and department number of all employees in
department 20 or 50 in alphabetical order by name
13. List the name and salary of employees who earn between 1250 and 3000 and are
in department 20 or 10. Label the columns Employees and Monthly Salary
respectively
Assignment – HR Schema
14. Display name and hiredate of all employees who was hired in 1981
15. Display the name and job title of all employees who do not have a manager
16. Display the name, salary and commission for all employees who earn
commissions. Sort data in descending order of salary and commissions