Upload
ali-hasan
View
264
Download
11
Embed Size (px)
Citation preview
7/29/2019 Oracle Database 11g: SQL Lesson 02
1/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.
Restricting and Sorting Data
7/29/2019 Oracle Database 11g: SQL Lesson 02
2/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 2
Objectives
After completing this lesson, you should be able to do thefollowing:
Limit the rows that are retrieved by a query
Sort the rows that are retrieved by a query
Use ampersand substitution to restrict and sort output atrun time
7/29/2019 Oracle Database 11g: SQL Lesson 02
3/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 3
Lesson Agenda
Limiting rows with: TheWHERE clause
The comparison conditions using =,
7/29/2019 Oracle Database 11g: SQL Lesson 02
4/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 4
Limiting Rows Using a Selection
retrieve allemployees in
department 90
EMPLOYEES
7/29/2019 Oracle Database 11g: SQL Lesson 02
5/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 5
Limiting the Rows That Are Selected
Restrict the rows that are returned by using the WHEREclause:
The WHERE clause follows the FROM clause.
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table[WHERE condition(s)];
7/29/2019 Oracle Database 11g: SQL Lesson 02
6/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 6
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;
Using theWHERE Clause
7/29/2019 Oracle Database 11g: SQL Lesson 02
7/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 7
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen' ;
Character Strings and Dates
Character strings and date values are enclosed with singlequotation marks.
Character values are case-sensitive and date values are
format-sensitive.
The default date display format is DD-MON-RR.
SELECT last_name
FROM employees
WHERE hire_date = '17-FEB-96' ;
7/29/2019 Oracle Database 11g: SQL Lesson 02
8/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 8
Comparison Operators
Not equal to
Between two values (inclusive)BETWEEN
...AND...
Match any of a list of valuesIN(set)
Match a character patternLIKE
Is a null valueIS NULL
Less than
Equal to=
MeaningOperator
7/29/2019 Oracle Database 11g: SQL Lesson 02
9/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 9
SELECT last_name, salary
FROM employees
WHERE salary
7/29/2019 Oracle Database 11g: SQL Lesson 02
10/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 10
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500 ;
Range Conditions Using the BETWEEN Operator
Use the BETWEEN operator to display rows based on a range ofvalues:
Lower limit Upper limit
7/29/2019 Oracle Database 11g: SQL Lesson 02
11/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 11
SELECT employee_id, last_name, salary, manager_id
FROM employeesWHERE manager_id IN (100, 101, 201) ;
Membership Condition Using the IN Operator
Use the IN operator to test for values in a list:
7/29/2019 Oracle Database 11g: SQL Lesson 02
12/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 12
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%' ;
Pattern Matching Using the LIKE Operator
Use the LIKE operator to perform wildcard searches ofvalid search string values.
Search conditions can contain either literal characters or
numbers:
%
denotes zero or many characters.
_ denotes one character.
7/29/2019 Oracle Database 11g: SQL Lesson 02
13/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 13
Combining Wildcard Characters
You can combine the two wildcard characters (%, _) withliteral characters for pattern matching:
You can use the ESCAPE identifier to search for the actual% and _ symbols.
SELECT last_name
FROM employeesWHERE last_name LIKE '_o%' ;
7/29/2019 Oracle Database 11g: SQL Lesson 02
14/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 14
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL ;
Using theNULL Conditions
Test for nulls with the ISNULL operator.
7/29/2019 Oracle Database 11g: SQL Lesson 02
15/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 15
Defining Conditions Using the Logical Operators
Returns TRUE if the condition is falseNOT
Returns TRUE ifeithercomponent condition
is true
OR
Returns TRUE ifboth component conditions
are true
AND
MeaningOperator
7/29/2019 Oracle Database 11g: SQL Lesson 02
16/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 16
SELECT employee_id, last_name, job_id, salaryFROM employees
WHERE salary >= 10000
AND job_id LIKE '%MAN%' ;
Using theAND Operator
AND requires both the component conditions to be true:
7/29/2019 Oracle Database 11g: SQL Lesson 02
17/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 17
SELECT employee_id, last_name, job_id, salaryFROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%' ;
Using the OROperator
OR requires either component condition to be true:
7/29/2019 Oracle Database 11g: SQL Lesson 02
18/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 18
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
Using theNOT Operator
7/29/2019 Oracle Database 11g: SQL Lesson 02
19/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 19
Lesson Agenda
Limiting rows with: The WHERE clause
The comparison conditions using =,
7/29/2019 Oracle Database 11g: SQL Lesson 02
20/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 20
Rules of Precedence
You can use parentheses to override rules of precedence.
Not equal to6
NOT logical condition7
AND logical condition8
OR logical condition9
IS[NOT]NULL, LIKE, [NOT]IN4
[NOT]BETWEEN5
Comparison conditions3
Concatenation operator2
Arithmetic operators1
MeaningOperator
7/29/2019 Oracle Database 11g: SQL Lesson 02
21/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 21
SELECT last_name, job_id, salaryFROM employees
WHERE job_id = 'SA_REP'
OR job_id = 'AD_PRES'
AND salary > 15000;
Rules of Precedence
SELECT last_name, job_id, salary
FROM employees
WHERE (job_id = 'SA_REP'
OR job_id = 'AD_PRES')
AND salary > 15000;
1
2
7/29/2019 Oracle Database 11g: SQL Lesson 02
22/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 22
Lesson Agenda
Limiting rows with: The WHERE clause
The comparison conditions using =,
7/29/2019 Oracle Database 11g: SQL Lesson 02
23/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 23
Using the ORDERBY Clause
Sort the retrieved rows with the ORDERBY clause: ASC: Ascending order, default
DESC: Descending order
The ORDERBY clause comes last in the SELECT
statement:SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date ;
7/29/2019 Oracle Database 11g: SQL Lesson 02
24/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 24
Sorting
Sorting in descending order:
Sorting by column alias:
SELECT last_name, job_id, department_id, hire_dateFROM employees
ORDER BY hire_date DESC ; 1
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal ;2
7/29/2019 Oracle Database 11g: SQL Lesson 02
25/39Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 25
Sorting
Sorting by using the columns numeric position:
Sorting by multiple columns:
SELECT last_name, job_id, department_id, hire_dateFROM employees
ORDER BY 3; 3
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;4
7/29/2019 Oracle Database 11g: SQL Lesson 02
26/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 26
Lesson Agenda
Limiting rows with: The WHERE clause
The comparison conditions using =,
7/29/2019 Oracle Database 11g: SQL Lesson 02
27/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 27
Substitution Variables
... salary = ?
department_id = ?
... last_name = ? ...
I want
to query
different
values.
7/29/2019 Oracle Database 11g: SQL Lesson 02
28/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 28
Substitution Variables
Use substitution variables to: Temporarily store values with single-ampersand (&) and
double-ampersand (&&) substitution
Use substitution variables to supplement the following:
WHERE conditions
ORDERBY clauses
Column expressions
Table names
Entire SELECT statements
7/29/2019 Oracle Database 11g: SQL Lesson 02
29/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 29
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;
Using the Single-Ampersand Substitution
Variable
Use a variable prefixed with an ampersand (&) to prompt theuser for a value:
7/29/2019 Oracle Database 11g: SQL Lesson 02
30/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 30
Using the Single-Ampersand Substitution
Variable
7/29/2019 Oracle Database 11g: SQL Lesson 02
31/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 31
SELECT last_name, department_id, salary*12
FROM employees
WHERE job_id = '&job_title' ;
Character and Date Values with
Substitution Variables
Use single quotation marks for date and character values:
7/29/2019 Oracle Database 11g: SQL Lesson 02
32/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 32
Specifying Column Names, Expressions, and Text
SELECT employee_id, last_name, job_id,&column_name
FROM employees
WHERE &condition
ORDER BY &order_column ;
7/29/2019 Oracle Database 11g: SQL Lesson 02
33/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 33
SELECT employee_id, last_name, job_id, &&column_name
FROM employees
ORDER BY &column_name ;
Using the Double-Ampersand
Substitution Variable
Use double ampersand (&&) if you want to reuse the variablevalue without prompting the user each time:
7/29/2019 Oracle Database 11g: SQL Lesson 02
34/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 34
Lesson Agenda
Limiting rows with: The WHERE clause
The comparison conditions using =,
7/29/2019 Oracle Database 11g: SQL Lesson 02
35/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 35
Using the DEFINE Command
Use the DEFINE command to create and assign a value toa variable.
Use the UNDEFINE command to remove a variable.
DEFINE employee_num = 200
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;
UNDEFINE employee_num
7/29/2019 Oracle Database 11g: SQL Lesson 02
36/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 36
SET VERIFY ON
SELECT employee_id, last_name, salary
FROM employeesWHERE employee_id = &employee_num;
Using theVERIFY Command
Use the VERIFY command to toggle the display of thesubstitution variable, both before and after SQL Developer
replaces substitution variables with values:
7/29/2019 Oracle Database 11g: SQL Lesson 02
37/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 37
Quiz
Which of the following are valid operators for the WHEREclause?
1. >=
2. IS NULL
3. !=4. IS LIKE
5. IN BETWEEN
6.
7/29/2019 Oracle Database 11g: SQL Lesson 02
38/39
Copyright 2010, Oracle and/or its affiliates. All rights reserved.2 - 38
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 logicalAND
,OR
, andNOT
operators
Use the ORDERBY clause to sort rows of output:
Use ampersand substitution to restrict and sort output at
run time
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, alias} [ASC|DESC]] ;
Summary
7/29/2019 Oracle Database 11g: SQL Lesson 02
39/39
Practice 2: Overview
This practice covers the following topics: Selecting data and changing the order of the rows
that are displayed
Restricting rows by using the WHERE clause
Sorting rows by using the ORDERBY clause Using substitution variables to add flexibility to your
SQL SELECT statements