Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.

Similar presentations


Presentation on theme: "2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan."— Presentation transcript:

1 2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp.  For further information, visit www.oracle.comwww.oracle.com  This presentation must be used for only education purpose for students at Central Washington University which is a member of Oracle Academic Initiatives (OAI) and has used Oracle systems for HRIS & Accounting Systems as a database platform embedded on PeopleSoft ERP system, since 1999.

2 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

3 Limiting Rows Using a Selection EMP “…retrieve all employees in department 10”

4 Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT [DISTINCT] {*| column [alias], …} FROMtable [WHEREcondition(s)];

5 Using the WHERE clause SQL>SELECTename, job, deptno 2FROMemp 3WHEREjob =‘CLERK’; ENAMEJOBDEPTNO JAMESCLERK30 SMITHCLERK20 ADAMSCLERK20 MILLERCLERK10

6 Character Strings and Dates Character strings and date values are enclosed in single quotation marks. Character values are case sensitive and date values are format sensitive. The default date format is DD-MON-YY. SQL>SELECTename, job, deptno 2FROMemp 3WHERE ename = ‘JAMES’;

7 Comparison Operators

8 Using the Comparison Operators SQL>SELECTename, sal, comm 2FROMemp 3WHEREsal <= comm; ENAMESALCOMM MARTIN1250  1400

9 Other Comparison Operators

10 Using the BETWEEN Operator Use the BETWEEN operator to display rows based on a range of values SQL>SELECTename, sal 2FROMemp 3 WHEREsal BETWEEN 1000 AND 1500; ENAMESAL MARTIN1250 TURNER1500 WARD1250 ADAMS1100 MILLER1300 Lower Higher limit

11 Using the IN Operator Use the IN operator to test for values in a list. SQL>SELECTempno, ename, sal, mgr 2FROMemp 3WHEREmgr IN (7902, 7566, 7788); EMPNOENAMESALMGR 7902FORD30007566 7369SMITH8007902 7788SCOTT30007566 7876ADAMS11007788

12 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. - % denotes zero or many characters. - _ denotes one character. SQL>SELECTename 2FROMemp 3WHEREename LIKE ‘S%’;

13 Using the LIKE Operator You can combine pattern-matching character. You can use the ESCAPE identifier to search for “%” or “_”. SQL>SELECTename 2FROMemp 3WHEREename LIKE ‘_A%’; ENAME MARTIN JAMES WARD

14 Using the IS NULL Operator Test for null values with the IS NULL operator. SQL> SELECTename, mgr 2FROMemp 3WHEREmgr IS NULL; ENAMEMGR KING

15 Logical Operators

16 Using the AND Operator AND requires both conditions to be TRUE. SQL>SELECTempno, ename, job, sal 2FROMemp 3WHEREsal >= 1100 4ANDjob=‘CLERK’; EMPNOENAMEJOBSAL 7876ADAMSCLERK1100 7934MILLERCLERK1300

17 Using the OR Operator OR requires either condition to be TRUE SQL>SELECTempno, ename, job, sal 2FROMemp 3WHEREsal >= 1100 4ORjob=‘CLERK’; EMPNOENAMEJOBSAL 7839KINGPRESIDENT5000 7698BLAKEMANAGER2850 7782CLARKMANAGER2450 7566JONESMANAGER2975 7654MARTINSALESMAN1250 … 7900JAMESCLERK950...

18 Using the NOT Operator SQL>SELECTename, job 2FROMemp 3WHEREjob NOT IN (’CLERK’, ‘MANAGER’, ‘ANALYST’); ENAMEJOB KINGPRESIDENT MARTINSALESMAN ALLENSALESMAN TURNERSALESMAN WARDSALESMAN

19 Rules of Precedence (Priority) Override rules of precedence by using parentheses.

20 Rules of Precedence SQL>SELECTename, job, sal 2FROMemp 3WHEREjob=‘SALESMAN’ 4ORjob=‘PRESIDENT’ 5ANDsal>1500; ENAMEJOBSAL KINGPRESIDENT5000 MARTINSALESMAN1250 ALLENSALESMAN1600 TURNERSALESMAN1500 WARDSALESMAN1250

21 Rules of Precedence Use parentheses to force priority. SQL>SELECTename, job, sal 2FROMemp 3WHERE ( job=‘SALESMAN’ 4ORjob=‘PRESIDENT’ ) 5ANDsal > 1500; ENAMEJOBSAL KINGPRESIDENT5000 ALLENSALESMAN1600

22 ORDER BY Clause Sort rows with the ORDER BY clause –ASC: ascending order, default –DESC: descending order The ORDER BY clause comes last in the SELECT statement SQL>SELECTename, job, deptno, hiredate 2FROM emp 3ORDER BYhiredate; ENAMEJOB DEPTNO HIREDATE SMITHCLERK 2017-DEC-80 ALLENSALESMAN 3020-FEB-81 … 14 rows selected.

23 Sorting in Descending Order SQL>SELECTename, job, deptno, hiredate 2FROMemp 3ORDER BYhiredate DESC ; ENAMEJOB DEPTNO HIREDATE ADAMSCLERK 2012-JAN-83 SCOTTANALYST 2009-DEC-82 MILLERCLERK 1023-JAN-82 JAMESCLERK 3003-DEC-81 FORDANALYST 2003-DEC-81 KINGPRESIDENT 1017-NOB-81 MARTINSALESMAN 30 28-DEP-81 … 14 rows selected

24 Sorting by Column Alias SQL>SELECTename, job, sal*12 annsal 2FROMemp 3ORDER BYannsal; EMPNOENAMEANNSAL 7369SMITH9600 7900JAMES11400 7876ADAMS13200 7654MARTIN15000 7521WARD15000 7934MILLER15600 7844TURNER18000 … 14 rows selected.

25 Sorting by Multiple Columns The order of ORDER BY list is the order of sort. You can sort by a column that is not in the SELECT list. SQL>SELECTename, deptno, sal 2FROMemp 3ORDER BYdeptno, sal DESC; ENAMEDEPTNOSAL KING 105000 CLARK 102450 MILLER 101300 FORD 203000 … 14 rows selected.

26 Summary SELECT[DISTINCT] {*| column [alias], …} FROMtable [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];

27 Practice Overview Selecting data and changing the order of rows displayed Restricting rows by using the WHERE clause Using the double quotation marks in column aliases


Download ppt "2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan."

Similar presentations


Ads by Google