Upload
labhnesh-jindal
View
214
Download
0
Embed Size (px)
Citation preview
7/17/2019 Les02.ppt
1/28
2Copyright Oracle Corporation, 2001. All rights reserved.
Restricting and Sorting Data
7/17/2019 Les02.ppt
2/28
2-2 Copyright Oracle Corporation, 2001. All rights reserved.
Objectives
After completing this lesson, you should be able todo the following
!imit the rows retrieved by a "uery
Sort the rows retrieved by a "uery
7/17/2019 Les02.ppt
3/28
2-# Copyright Oracle Corporation, 2001. All rights reserved.
!imiting Rows $sing a Selection
%retrieve allemployees
in department &'(
EMPLOYEES
)
7/17/2019 Les02.ppt
4/28
2-* Copyright Oracle Corporation, 2001. All rights reserved.
!imiting the Rows Selected
Restrict the rows returned by using theWHEREclause+
heWHEREclause follows the FROMclause+
SELECT *|{[DISTINCT] column|expression[alias],...}
FROM table[WHERE condition(s)];
7/17/2019 Les02.ppt
5/28
2- Copyright Oracle Corporation, 2001. All rights reserved.
$sing theWHERE.lause
SELECT emploee!"#, l$%&!'$me, (o)!"#, #ep$&me'&!"#
FROM emploee%
WHERE #ep$&me'&!"# + - ;
7/17/2019 Les02.ppt
6/28
2-/ Copyright Oracle Corporation, 2001. All rights reserved.
.haracter Strings and Dates
.haracter strings and date values are enclosed insingle "uotation mar0s+
.haracter values are case sensitive, and date
values are format sensitive+ he default date format is DD-1O-RR+
SELECT l$%&!'$me, (o)!"#, #ep$&me'&!"#
FROM emploee%
WHERE l$%&!'$me + W/$le';
7/17/2019 Les02.ppt
7/282-3 Copyright Oracle Corporation, 2001. All rights reserved.
.omparison .onditions
Operator
4
5
54
6
64
65
1eaning
7"ual to
8reater than
8reater than or e"ual to
!ess than
!ess than or e"ual to
ot e"ual to
7/17/2019 Les02.ppt
8/282-9 Copyright Oracle Corporation, 2001. All rights reserved.
SELECT l$%&!'$me, %$l$
FROM emploee%
WHERE %$l$ 0+ 1---;
$sing .omparison .onditions
7/17/2019 Les02.ppt
9/282-& Copyright Oracle Corporation, 2001. All rights reserved.
Other .omparison .onditions
Operator
2ETWEEN
...3ND...
IN4%e&5
LI6E
IS N7LL
1eaning
:etween two values ;inclusive
7/17/2019 Les02.ppt
10/282->' Copyright Oracle Corporation, 2001. All rights reserved.
$sing the 2ETWEEN.ondition
$se the 2ETWEENcondition to display rows based on arange of values+
SELECT l$%&!'$me, %$l$
FROM emploee%
WHERE %$l$ 2ETWEEN 89-- 3ND 19--;
!ower limit $pper limit
7/17/2019 Les02.ppt
11/282->> Copyright Oracle Corporation, 2001. All rights reserved.
SELECT emploee!"#, l$%&!'$me, %$l$, m$'$:e!"#
FROM emploee%
WHERE m$'$:e!"# IN 4--, -, 8-5;
$sing the IN.ondition
$se the INmembership condition to test for values in
a list+
7/17/2019 Les02.ppt
12/282->2 Copyright Oracle Corporation, 2001. All rights reserved.
$sing the LI6E.ondition
$se the LI6Econdition to perform wildcardsearches of valid search string values+
Search conditions can contain either literal
characters or numbers
7/17/2019 Les02.ppt
13/282-># Copyright Oracle Corporation, 2001. All rights reserved.
@ou can combine pattern-matching characters+
@ou can use the ESC3PEidentifier to search for theactual %and_ symbols+
$sing the LI6E.ondition
SELECT l$%&!'$me
FROM emploee%
WHERE l$%&!'$me LI6E !o
7/17/2019 Les02.ppt
14/282->* Copyright Oracle Corporation, 2001. All rights reserved.
$sing theN7LL.onditions
est for nulls with the IS N7LLoperator+
SELECT l$%&!'$me, m$'$:e!"#
FROM emploee%WHERE m$'$:e!"# IS N7LL;
7/17/2019 Les02.ppt
15/282-> Copyright Oracle Corporation, 2001. All rights reserved.
!ogical .onditions
Operator
3ND
OR
NOT
1eaning
Returns TR7Eif both component
conditions are true
Returns TR7Eif either componentcondition is true
Returns TR7Eif the following
condition is false
7/17/2019 Les02.ppt
16/282->/ Copyright Oracle Corporation, 2001. All rights reserved.
$sing the3NDOperator
3NDre"uires both conditions to be true+
SELECT emploee!"#, l$%&!'$me, (o)!"#, %$l$
FROM emploee%
WHERE %$l$ >+----
3ND (o)!"# LI6E
7/17/2019 Les02.ppt
17/282->3 Copyright Oracle Corporation, 2001. All rights reserved.
$sing the OROperator
ORre"uires either condition to be true+
SELECT emploee!"#, l$%&!'$me, (o)!"#, %$l$
FROM emploee%
WHERE %$l$ >+ ----
OR (o)!"# LI6E
7/17/2019 Les02.ppt
18/282->9 Copyright Oracle Corporation, 2001. All rights reserved.
SELECT l$%&!'$me, (o)!"#
FROM emploee%
WHERE (o)!"#
NOT IN 4IT!PRO?, ST!CLER6, S3!REP5;
$sing theNOTOperator
7/17/2019 Les02.ppt
19/282->& Copyright Oracle Corporation, 2001. All rights reserved.
Rules of recedence
Override rules of precedence by using parentheses+
Order 7valuated Operator
> Arithmetic operators
2 .oncatenation operator
# .omparison conditions* IS[NOT]N7LL, LI6E, [NOT]IN
[NOT] 2ETWEEN
/ NOTlogical condition
3 3NDlogical condition9 ORlogical condition
7/17/2019 Les02.ppt
20/282-2' Copyright Oracle Corporation, 2001. All rights reserved.
SELECT l$%&!'$me, (o)!"#, %$l$
FROM emploee%
WHERE (o)!"# + S3!REP
OR (o)!"# + 3D!PRES
3ND %$l$ > 9---;
Rules of recedence
7/17/2019 Les02.ppt
21/282-2> Copyright Oracle Corporation, 2001. All rights reserved.
SELECT l$%&!'$me, (o)!"#, %$l$
FROM emploee%
WHERE 4(o)!"# + S3!REP
OR (o)!"# + 3D!PRES5
3ND %$l$ > 9---;
Rules of recedence
$se parentheses to force priority+
7/17/2019 Les02.ppt
22/28
2-22 Copyright Oracle Corporation, 2001. All rights reserved.
SELECT l$%&!'$me, (o)!"#, #ep$&me'&!"#, /"e!#$&e
FROM emploee%ORDER 2Y /"e!#$&e ;
ORDER 2Y.lause
Sort rows with the ORDER 2Yclause AS. ascending order, default
D7S. descending order
he ORDER 2Yclause comes last in the SELECT
statement+
)
7/17/2019 Les02.ppt
23/28
2-2# Copyright Oracle Corporation, 2001. All rights reserved.
Sorting in Descending Order
SELECT l$%&!'$me, (o)!"#, #ep$&me'&!"#, /"e!#$&e
FROM emploee%
ORDER 2Y /"e!#$&e DESC ;
)
7/17/2019 Les02.ppt
24/28
2-2* Copyright Oracle Corporation, 2001. All rights reserved.
Sorting by .olumn Alias
SELECT emploee!"#, l$%&!'$me, %$l$*8 $''%$l
FROM emploee%
ORDER 2Y $''%$l;
)
7/17/2019 Les02.ppt
25/28
2-2 Copyright Oracle Corporation, 2001. All rights reserved.
he order of ORDER 2Ylist is the order of sort+
@ou can sort by a column that is not in theSELECTlist+
SELECT l$%&!'$me, #ep$&me'&!"#, %$l$
FROM emploee%
ORDER 2Y #ep$&me'&!"#, %$l$ DESC;
Sorting by 1ultiple .olumns
)
7/17/2019 Les02.ppt
26/28
2-2/ Copyright Oracle Corporation, 2001. All rights reserved.
Summary
SELECT *|{[DISTINCT] column|expression[alias],...}
FROM table[WHERE condition(s)]
[ORDER 2Y {column, expr, alias} [3SC|DESC]];
=n this lesson, you should have learned how to
$se theWHEREclause to restrict rows of output $se the comparison conditions
$se the 2ETWEEN, IN, LI6E, andN7LLconditions
Apply the logical3ND, OR, andNOToperators
$se the ORDER 2Yclause to sort rows of output
7/17/2019 Les02.ppt
27/28
2-23 Copyright Oracle Corporation, 2001. All rights reserved.
ractice 2 Overview
his practice covers the following topics
Selecting data and changing the order ofrows displayed
Restricting rows by using theWHEREclause Sorting rows by using the ORDER 2Yclause
7/17/2019 Les02.ppt
28/28