Restricting and sorting data 16 May 201516 May 201516 May 20151 Created By Pantharee Sawasdimongkol.
-
Upload
lesley-williamson -
Category
Documents
-
view
218 -
download
0
Transcript of Restricting and sorting data 16 May 201516 May 201516 May 20151 Created By Pantharee Sawasdimongkol.
Restricting and sorting Restricting and sorting datadata
April 18, 2023April 18, 2023 11Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
objectivesobjectives
After completing this lesson, you should After completing this lesson, you should
be able to do the following:be able to do the following: Limit the rows retrieved by a queryLimit the rows retrieved by a query Sort the rows retrieved by a querySort the rows retrieved by a query
April 18, 2023April 18, 2023 22Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Limiting Rows Using a SelectionLimiting Rows Using a Selection
EMPNEMPNOO ENAMEENAME JOBJOB ………… DEPTNODEPTNO
78397839 KINGKING PRESIDENTPRESIDENT 1010
76987698 BLAKEBLAKE MANAGERMANAGER 3030
77827782 CLARKCLARK MANAGERMANAGER 1010
75667566 JONESJONES MANAGERMANAGER 2020
EMPNOEMPNO ENAMEENAME JOBJOB ………… DEPTNODEPTNO
78397839 KINGKING PRESIDENTPRESIDENT 1010
77827782 CLARKCLARK MANAGERMANAGER 1010
79347934 MILLERMILLER CLERKCLERK 1010
EMP
EMP
“…retrieve all
employees
In department 10”
April 18, 2023April 18, 2023 33Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Limiting Rows SelectedLimiting Rows Selected
Restrict the rows returned by using theRestrict the rows returned by using the
WHERE clauseWHERE clause
SELECT [ DISTINCT ] { * } column [ alias] , …}FROM table[WHERE condition (s) ] ;
The WHERE clause follows the FROM The WHERE clause follows the FROM clause.clause.
April 18, 2023April 18, 2023 44Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the WHERE ClauseUsing the WHERE Clause
SQL> SELECT ename , job , deptno 2 FROM emp 3 WHERE job = ‘CLERK’ ;
ENAMEENAME JOBJOB DEPTNODEPTNO
-------------------------- ---------------------------- --------------------------
JAMESJAMES
SMITHSMITH
ADAMSADAMS
MILLERMILLER
CLERKCLERK
CLERKCLERK
CLERKCLERK
CLERKCLERK
3030
2020
2020
1010
April 18, 2023April 18, 2023 55Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Character Strings and DatesCharacter Strings and Dates
Character Strings and date values are Character Strings and date values are
Enclosed in single quotation marks.Enclosed in single quotation marks. Character values are case sensitive andCharacter values are case sensitive and
Data values are format sensitive.Data values are format sensitive. The default data format is DD-MON-YY.The default data format is DD-MON-YY.
SQL> SELECT ename , job , deptno 2 FROM emp; 3 WHERE ename = ‘JAMES’ ;
April 18, 2023April 18, 2023 66Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Comparison OperatorsComparison Operators
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
< > , != Not equal to
April 18, 2023April 18, 2023 77Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the ComparisonUsing the ComparisonOperatorsOperators
SQL> SELECT ename , sal , comm 2 FROM emp 3 WHERE sal <= comm;
ENAMEENAME SALSAL COMMCOMM
-------------------------- ---------------------------- --------------------------
MARTINMARTIN
12501250 14001400
April 18, 2023April 18, 2023 88Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Other Comparison operatorsOther Comparison operators
Operator Meaning
BETWEEN Between two values (inclusive)
…AND…
IN (list) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
April 18, 2023April 18, 2023 99Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the BETWEEN OperatorUsing the BETWEEN Operator Use the Use the BETWEENBETWEEN operator to display operator to displayRows based on a range of values.Rows based on a range of values.
SQL> SELECT ename , sal 2 FROM emp 3 WHERE sal BETWEEN 1000 AND 1500 ;
ENAME SAL---------- ----------WARD 1250MARTIN 1250TURNER 1500ADAMS 1100MILLER 1300
Lower limit
Higherlimit
April 18, 2023April 18, 2023 1010Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the IN OperatorUsing the IN Operator
Use the Use the ININ operator to test for values in a operator to test for values in a list.list.
SQL> SELECT empno , ename , sal , mgr 2 FROM emp 3 WHERE mgr IN ( 7902 , 7566 , 7788 ) ;
EMPNO ENAME SAL MGR----------- --------- ---------- ---------- 7369 SMITH 800 7902 7788 SCOTT 3000 7566 7876 ADAMS 1100 7788 7902 FORD 3000 7566
April 18, 2023April 18, 2023 1111Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the LIKE OperatorUsing the LIKE Operator UseUse the the LIKELIKE operator to perform wildcard operator to perform wildcard
searches of valid search string values.searches of valid search string values. Search conditions can contain either literalSearch conditions can contain either literal Characters or numbers.Characters or numbers.
-- % denotes zero or many characters. % denotes zero or many characters. -- _ denotes one characters. _ denotes one characters.
SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE ‘ S% ’ ;
April 18, 2023April 18, 2023 1212Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the LIKE OperatorUsing the LIKE Operator You can combine pattern-matching characters.You can combine pattern-matching characters.
SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE ‘_A%’ ;
ENAME ---------------- MARTIN JAMES WARD
You can use the You can use the ESCAPEESCAPE( ( \\ ) identifier to search ) identifier to search for “%” or “_” .for “%” or “_” .
April 18, 2023April 18, 2023 1313Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the IS NULL OperatorUsing the IS NULL Operator
Test for null values with the Test for null values with the IS NULL IS NULL
Operator.Operator.
SQL> SELECT ename , mgr 2 FROM emp 3 WHERE mgr IS NULL ;
ENAME MGR ---------------- ------------------ KING
April 18, 2023April 18, 2023 1414Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Logical OperatorsLogical Operators
Operator Meaning
AND Returns TRUE if both component
conditions are TRUE
OR Returns TRUE if either component conditions are TRUE
NOT Returns TRUE if the component conditions are FALSE
April 18, 2023April 18, 2023 1515Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the AND OperatorUsing the AND Operator
AND requires both conditions to be TRUEAND requires both conditions to be TRUE
SQL> SELECT empno , ename , job , sal 2 FROM emp 3 WHERE sal >= 1100 4 AND job = ‘CLERK’ ;
EMPNO ENAME JOB SAL----------- --------- ---------- ---------- 7876 ADAMS CLERK 1100 7934 MILLER CLERK 1300
April 18, 2023April 18, 2023 1616Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the OR OperatorUsing the OR Operator
EMPNO ENAME JOB SAL----------- --------- ---------- ---------- 7839 KING PRESIDENT 50007782 CLARK MANAGER 24507566 JONES MANAGER 29757654 MARTIN SALESMAN 1250………7900 JAMES CLERK 950
14 rows selected.
OR requires either condition to be TRUE.OR requires either condition to be TRUE.
SQL> SELECT empno , ename , job , sal 2 FROM emp 3 WHERE sal >= 1100 4 OR job = ‘CLERK’ ;
April 18, 2023April 18, 2023 1717Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Using the NOT OperatorUsing the NOT Operator
EMPNO JOB------------------- ----------------------KING PRESIDENTMARTIN SALESMANALLEN SALESMANTURNER SALESMANWARD SALESMAN
SQL> SELECT empno , job 2 FROM emp
3 WHERE job NOT IN ( ‘ CLERK ', ' MANAGER ', 'ANALYST’);
April 18, 2023April 18, 2023 1818Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Rules of PrecedenceRules of Precedence
Order Evaluated Meaning
1 All comparison
operators
2 NOT
3 AND
4 OR
Override rules of precedence by using Override rules of precedence by using
parentheses.parentheses.
April 18, 2023April 18, 2023 1919Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Rules of PrecedenceRules of Precedence
ENAME JOB SAL---------- ------------- ---------------------ALLEN SALESMAN 1600WARD SALESMAN 1250MARTIN SALESMAN 1250 KING PRESIDENT 5000TURNER SALESMAN 1500
SQL> SELECT ename , job , sal 2 FROM emp 3 WHERE job = 'SALESMAN' 4 OR job = 'PRESIDENT ' 5 AND sal > 1500;
April 18, 2023April 18, 2023 2020Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Rules of PrecedenceRules of Precedence
Use parentheses to force priority.Use parentheses to force priority.
ENAME JOB SAL---------- --------- ----------ALLEN SALESMAN 1600KING PRESIDENT 5000
SQL> SELECT ename , job , sal 2 FROM emp 3 WHERE ( job='SALESMAN' 4 OR job='PRESIDENT') 5 AND sal > 1500;
April 18, 2023April 18, 2023 2121Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
ORDER BY ClauseORDER BY Clause SortSort rows with the ORDER BY clauserows with the ORDER BY clause
- ASC : ascending order,default- ASC : ascending order,default- DESC : descending order- DESC : descending order
The ORDER BY clause comes last in the The ORDER BY clause comes last in the SELECT statement.SELECT statement.
ENAME JOB DEPTNO HIREDATE---------- --------- -------------- -------------------SMITH CLERK 20 17-DEC-80ALLEN SALESMAN 30 20-FEB-81…….14 rows selected.
SQL> SELECT ename , job , deptno , hiredate 2 FROM emp 3 ORDER BY hiredate;
April 18, 2023April 18, 2023 2222Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Sorting in Descending OrderSorting in Descending Order
ENAME JOB DEPTNO HIREDATE---------- --------- ---------------- ------------------ADAMS CLERK 20 23-MAY-87SCOTT ANALYST 20 19-APR-87MILLER CLERK 10 23-JAN-82JAMES CLERK 30 03-DEC-81FORD ANALYST 20 03-DEC-81KING PRESIDENT 10 17-NOV-81MARTIN SALESMAN 30 28-SEP-81…….14 rows selected.
SQL> SELECT ename , job , deptno, hiredate 2 FROM emp 3 ORDER BY hiredate DESC;
April 18, 2023April 18, 2023 2323Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Sorting by column AliasSorting by column Alias
EMPNO ENAME ANNSAL--------- ---------------- ------------------ 7369 SMITH 9600 7900 JAMES 11400 7876 ADAMS 13200 7521 WARD 15000 7654 MARTIN 15000 7934 MILLER 15600 7844 TURNER 18000 …….14 rows selected.
SQL> SELECT empno , ename , sal*12 annsal 2 FROM emp 3 ORDER BY annsal;
April 18, 2023April 18, 2023 2424Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Sorting by Multiple ColumnsSorting by Multiple Columns The order of ORDER BY list is the order of The order of ORDER BY list is the order of
sort.sort.
ENAME DEPTNO SAL---------- ---------- ----------KING 10 5000CLARK 10 2450MILLER 10 1300SCOTT 20 3000…….14 rows selected.
SQL> SELECT ename , deptno , sal 2 FROM emp 3 ORDER BY deptno , sal DESC ;
April 18, 2023April 18, 2023 2525Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
SummarySummary
SELECT [ DISTINCT ] { * } column [ alias ] , …}FROM table[WHERE condition (S) ][ ORDER BY {column , expr , alias} [ASC | DESC ] ] ;
April 18, 2023April 18, 2023 2626Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol
Practice OverviewPractice Overview
Selecting data and changing the order of Selecting data and changing the order of rows displayedrows displayed
Restricting rows by using the WHERE Restricting rows by using the WHERE clauseclause
Using the double quotation marks in Using the double quotation marks in column aliasescolumn aliases
April 18, 2023April 18, 2023 2727Created By Pantharee Created By Pantharee
SawasdimongkolSawasdimongkol