Chapter 02-Restricting and Sorting Data
Objectives
After completing this lesson,you should be able to do the following:
- 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 at run time.
Lesson Agenda
- Limiting rows with:
- -The WHERE clause
- -The comparison conditions using=,<=,BETWEEN,IN,LIKE and NULL conditions
- -Logical conditions using AND,OR and NOT operators
- Rules of precedence for operators in an expression
- Sorting rows using the ORDER BY clause
- Substitution variables
- DEFINE and VERIFY commands
Limiting the Rows that Are Selected
- Restrict the rows that are returned by using the WHERE clause:
SELECT *|{DISTINCT column|expression [alias],...} FROM table [WHERE condition(s)]
- The WHERE clause follows the FROM clause.
Using the WHERE Clause
Demo-01:
SQL> SELECT employee_id,last_name,job_id,department_id FROM employees WHERE department_id = 90; EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 100 King AD_PRES 90 101 Kochhar AD_VP 90 102 De Haan AD_VP 90
Demo-02:(注意:WHERE Clause不能使用alias)
SQL> SELECT employee_id,last_name,job_id,department_id deptid FROM employees WHERE deptid = 90; SELECT employee_id,last_name,job_id,department_id deptid FROM employees WHERE deptid = 90 * ERROR at line 1: ORA-00904: "DEPTID": invalid identifier
Character Strings and Dates
- 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.
Demo-01:
SQL> select last_name,department_id FROM employees WHERE last_name = 'Whalen'; LAST_NAME DEPARTMENT_ID ------------------------- ------------- Whalen 10 SQL> select last_name,department_id FROM employees WHERE last_name = 'WhalEn'; no rows selected
Demo-02:
SQL> SELECT last_name,hire_date,department_id FROM employees WHERE hire_date = '23-MAY-06'; LAST_NAME HIRE_DATE DEPARTMENT_ID ------------------------- --------- ------------- Feeney 23-MAY-06 50
Comparison Operators
Operators | Descriptions |
= | Equal to |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
<> | Not equal to(!= ^=) |
BETWEEN ... AND ... | Between two values(inclusive) |
IN(set) | Match any of a list of values |
LIKE | Match a character parttern |
IS NULL | Is a null value |
Using Comarison Operators
Demo-01:
SQL> SELECT last_name,salary FROM employees WHERE salary <= 3000; LAST_NAME SALARY ------------------------- ---------- OConnell 2600 Grant 2600 Baida 2900 Tobias 2800 Himuro 2600 Colmenares 2500 Mikkilineni 2700 Landry 2400 Markle 2200 Atkinson 2800 Marlow 2500 LAST_NAME SALARY ------------------------- ---------- Olson 2100 Rogers 2900 Gee 2400 Philtanker 2200 Seo 2700 Patel 2500 Matos 2600 Vargas 2500 Sullivan 2500 Geoni 2800 Cabrio 3000 LAST_NAME SALARY ------------------------- ---------- Gates 2900 Perkins 2500 Jones 2800 Feeney 3000 26 rows selected.
Range Conditions Using the BETWEEN Operators
Use the BETWEEN operator to display rows based on a range of values:
SQL> SELECT last_name,salary FROM employees WHERE salary BETWEEN 2500 AND 3500; LAST_NAME SALARY ------------------------- ---------- OConnell 2600 Grant 2600 Khoo 3100 Baida 2900 Tobias 2800 Himuro 2600 Colmenares 2500 Nayer 3200 Mikkilineni 2700 Bissot 3300 Atkinson 2800 LAST_NAME SALARY ------------------------- ---------- Marlow 2500 Mallin 3300 Rogers 2900 Stiles 3200 Seo 2700 Patel 2500 Rajs 3500 Davies 3100 Matos 2600 Vargas 2500 Taylor 3200 LAST_NAME SALARY ------------------------- ---------- Fleaur 3100 Sullivan 2500 Geoni 2800 Dellinger 3400 Cabrio 3000 Gates 2900 Perkins 2500 McCain 3200 Jones 2800 Walsh 3100 Feeney 3000 33 rows selected.
SQL> SELECT last_name,salary FROM employees WHERE salary >= 2500 AND salary <= 3500; LAST_NAME SALARY ------------------------- ---------- OConnell 2600 Grant 2600 Khoo 3100 Baida 2900 Tobias 2800 Himuro 2600 Colmenares 2500 Nayer 3200 Mikkilineni 2700 Bissot 3300 Atkinson 2800 LAST_NAME SALARY ------------------------- ---------- Marlow 2500 Mallin 3300 Rogers 2900 Stiles 3200 Seo 2700 Patel 2500 Rajs 3500 Davies 3100 Matos 2600 Vargas 2500 Taylor 3200 LAST_NAME SALARY ------------------------- ---------- Fleaur 3100 Sullivan 2500 Geoni 2800 Dellinger 3400 Cabrio 3000 Gates 2900 Perkins 2500 McCain 3200 Jones 2800 Walsh 3100 Feeney 3000 33 rows selected.
Membership Condition Using the IN Operators
use the IN operator to test for values in a list:
SQL> SELECT employee_id,last_name,salary,manager_id FROM employees WHERE manager_id IN(100,101,201); EMPLOYEE_ID LAST_NAME SALARY MANAGER_ID ----------- ------------------------- ---------- ---------- 201 Hartstein 13000 100 101 Kochhar 17000 100 102 De Haan 17000 100 114 Raphaely 11000 100 120 Weiss 8000 100 121 Fripp 8200 100 122 Kaufling 7900 100 123 Vollman 6500 100 124 Mourgos 5800 100 145 Russell 14000 100 146 Partners 13500 100 EMPLOYEE_ID LAST_NAME SALARY MANAGER_ID ----------- ------------------------- ---------- ---------- 147 Errazuriz 12000 100 148 Cambrault 11000 100 149 Zlotkey 10500 100 200 Whalen 4400 101 203 Mavris 6500 101 204 Baer 10000 101 205 Higgins 12008 101 108 Greenberg 12008 101 202 Fay 6000 201 20 rows selected.
SQL> SELECT employee_id,last_name,salary,manager_id FROM employees WHERE last_name IN('Hartstein','Vargas'); EMPLOYEE_ID LAST_NAME SALARY MANAGER_ID ----------- ------------------------- ---------- ---------- 201 Hartstein 13000 100 144 Vargas 2500 124
Parttern Matching Using the LIKE Operators
- 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.
Combining Wildcard Characters
- You can combine the two wildcard characters(%,_) with literal characters for pattern matching:
SQL> SELECT last_name FROM employees WHERE last_name LIKE '_o%'; LAST_NAME ------------------------- Colmenares Doran Fox Johnson Jones Kochhar Lorentz Mourgos Popp Rogers Tobias LAST_NAME ------------------------- Vollman 12 rows selected. SQL>
- You can use the ESCAPE identifier to search for the actual % and _ symbols.
SQL> SELECT employee_id,last_name,job_id FROM employees WHERE job_id LIKE '%SA\_%' ESCAPE '\'; EMPLOYEE_ID LAST_NAME JOB_ID ----------- ------------------------- ---------- 145 Russell SA_MAN 146 Partners SA_MAN 147 Errazuriz SA_MAN 148 Cambrault SA_MAN 149 Zlotkey SA_MAN 150 Tucker SA_REP 151 Bernstein SA_REP 152 Hall SA_REP 153 Olsen SA_REP 154 Cambrault SA_REP 155 Tuvault SA_REP EMPLOYEE_ID LAST_NAME JOB_ID ----------- ------------------------- ---------- 156 King SA_REP 157 Sully SA_REP 158 McEwen SA_REP 159 Smith SA_REP 160 Doran SA_REP 161 Sewall SA_REP 162 Vishney SA_REP 163 Greene SA_REP 164 Marvins SA_REP 165 Lee SA_REP 166 Ande SA_REP EMPLOYEE_ID LAST_NAME JOB_ID ----------- ------------------------- ---------- 167 Banda SA_REP 168 Ozer SA_REP 169 Bloom SA_REP 170 Fox SA_REP 171 Smith SA_REP 172 Bates SA_REP 173 Kumar SA_REP 174 Abel SA_REP 175 Hutton SA_REP 176 Taylor SA_REP 177 Livingston SA_REP EMPLOYEE_ID LAST_NAME JOB_ID ----------- ------------------------- ---------- 178 Grant SA_REP 179 Johnson SA_REP 35 rows selected.
Using the NULL Conditions
Test for nulls with the IS NULL operator.
SQL> SELECT last_name,manager_id FROM employees WHERE manager_id IS NULL; LAST_NAME MANAGER_ID ------------------------- ---------- King
SQL> SELECT last_name,manager_id FROM employees WHERE manager_id IS NOT NULL; LAST_NAME MANAGER_ID ------------------------- ---------- Abel 149 Ande 147 Atkinson 121 Austin 103 Baer 101 Baida 114 Banda 147 Bates 148 Bell 123 Bernstein 145 Bissot 121 LAST_NAME MANAGER_ID ------------------------- ---------- Bloom 148 Bull 121 Cabrio 121 Cambrault 100 Cambrault 145 Chen 108 Chung 122 Colmenares 114 Davies 124 De Haan 100 Dellinger 121 LAST_NAME MANAGER_ID ------------------------- ---------- Dilly 122 Doran 146 Ernst 103 Errazuriz 100 Everett 123 Faviet 108 Fay 201 Feeney 124 Fleaur 120 Fox 148 Fripp 100 LAST_NAME MANAGER_ID ------------------------- ---------- Gates 122 Gee 122 Geoni 120 Gietz 205 Grant 124 Grant 149 Greenberg 101 Greene 147 Hall 145 Hartstein 100 Higgins 101 LAST_NAME MANAGER_ID ------------------------- ---------- Himuro 114 Hunold 102 Hutton 149 Johnson 149 Jones 123 Kaufling 100 Khoo 114 King 146 Kochhar 100 Kumar 148 Ladwig 123 LAST_NAME MANAGER_ID ------------------------- ---------- Landry 120 Lee 147 Livingston 149 Lorentz 103 Mallin 122 Markle 120 Marlow 121 Marvins 147 Matos 124 Mavris 101 McCain 123 LAST_NAME MANAGER_ID ------------------------- ---------- McEwen 146 Mikkilineni 120 Mourgos 100 Nayer 120 OConnell 124 Olsen 145 Olson 121 Ozer 148 Partners 100 Pataballa 103 Patel 123 LAST_NAME MANAGER_ID ------------------------- ---------- Perkins 122 Philtanker 122 Popp 108 Rajs 124 Raphaely 100 Rogers 122 Russell 100 Sarchand 121 Sciarra 108 Seo 123 Sewall 146 LAST_NAME MANAGER_ID ------------------------- ---------- Smith 146 Smith 148 Stiles 123 Sullivan 120 Sully 146 Taylor 149 Taylor 120 Tobias 114 Tucker 145 Tuvault 145 Urman 108 LAST_NAME MANAGER_ID ------------------------- ---------- Vargas 124 Vishney 147 Vollman 100 Walsh 124 Weiss 100 Whalen 101 Zlotkey 100 106 rows selected.
Defining Conditions Using the Logical Operators
Operator | Menaing |
AND | Returns TRUE if both component conditions are true. |
OR | Returns TRUE if either component condition is true. |
NOT | Returns TRUE if the condition is false. |
Using the AND Operator
AND requires both the component conditions to be ture:
SQL> SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary >= 10000 AND job_id LIKE '%MAN%'; EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 201 Hartstein MK_MAN 13000 114 Raphaely PU_MAN 11000 145 Russell SA_MAN 14000 146 Partners SA_MAN 13500 147 Errazuriz SA_MAN 12000 148 Cambrault SA_MAN 11000 149 Zlotkey SA_MAN 10500 7 rows selected.
Using the OR Operator
OR requires either component conditions to be true:
SQL> SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary >= 10000 OR job_id LIKE '%MAN%'; EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 201 Hartstein MK_MAN 13000 204 Baer PR_REP 10000 205 Higgins AC_MGR 12008 100 King AD_PRES 24000 101 Kochhar AD_VP 17000 102 De Haan AD_VP 17000 108 Greenberg FI_MGR 12008 114 Raphaely PU_MAN 11000 120 Weiss ST_MAN 8000 121 Fripp ST_MAN 8200 122 Kaufling ST_MAN 7900 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 123 Vollman ST_MAN 6500 124 Mourgos ST_MAN 5800 145 Russell SA_MAN 14000 146 Partners SA_MAN 13500 147 Errazuriz SA_MAN 12000 148 Cambrault SA_MAN 11000 149 Zlotkey SA_MAN 10500 150 Tucker SA_REP 10000 156 King SA_REP 10000 162 Vishney SA_REP 10500 168 Ozer SA_REP 11500 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 169 Bloom SA_REP 10000 174 Abel SA_REP 11000 24 rows selected.
Using the NOT Operator
SQL> SELECT employee_id,last_name,job_id,salary FROM employees WHERE job_id NOT IN('IT_PROG','ST_CLERK','SA_REP'); EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 198 OConnell SH_CLERK 2600 199 Grant SH_CLERK 2600 200 Whalen AD_ASST 4400 201 Hartstein MK_MAN 13000 202 Fay MK_REP 6000 203 Mavris HR_REP 6500 204 Baer PR_REP 10000 205 Higgins AC_MGR 12008 206 Gietz AC_ACCOUNT 8300 100 King AD_PRES 24000 101 Kochhar AD_VP 17000 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 102 De Haan AD_VP 17000 108 Greenberg FI_MGR 12008 109 Faviet FI_ACCOUNT 9000 110 Chen FI_ACCOUNT 8200 111 Sciarra FI_ACCOUNT 7700 112 Urman FI_ACCOUNT 7800 113 Popp FI_ACCOUNT 6900 114 Raphaely PU_MAN 11000 115 Khoo PU_CLERK 3100 116 Baida PU_CLERK 2900 117 Tobias PU_CLERK 2800 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 118 Himuro PU_CLERK 2600 119 Colmenares PU_CLERK 2500 120 Weiss ST_MAN 8000 121 Fripp ST_MAN 8200 122 Kaufling ST_MAN 7900 123 Vollman ST_MAN 6500 124 Mourgos ST_MAN 5800 145 Russell SA_MAN 14000 146 Partners SA_MAN 13500 147 Errazuriz SA_MAN 12000 148 Cambrault SA_MAN 11000 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 149 Zlotkey SA_MAN 10500 180 Taylor SH_CLERK 3200 181 Fleaur SH_CLERK 3100 182 Sullivan SH_CLERK 2500 183 Geoni SH_CLERK 2800 184 Sarchand SH_CLERK 4200 185 Bull SH_CLERK 4100 186 Dellinger SH_CLERK 3400 187 Cabrio SH_CLERK 3000 188 Chung SH_CLERK 3800 189 Dilly SH_CLERK 3600 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 190 Gates SH_CLERK 2900 191 Perkins SH_CLERK 2500 192 Bell SH_CLERK 4000 193 Everett SH_CLERK 3900 194 McCain SH_CLERK 3200 195 Jones SH_CLERK 2800 196 Walsh SH_CLERK 3100 197 Feeney SH_CLERK 3000 52 rows selected.
Rules of Precedence
Operator | Meaning |
1 | Airthmetic operators |
2 | Concatenation operator |
3 | Comparison conditions |
4 | IS [NOT] NULL,LIKE,[NOT] IN |
5 | [NOT] BETWEEN |
6 | Not equal to |
7 | NOT logical condition |
8 | AND logcial condition |
9 | OR logical condition |
You can use parentheses to override rules of precedence
Rules of precedence
对比如下SQL,可分析出优先级的计算规则.
SQL> SELECT last_name,job_id,salary FROM employees WHERE job_id = 'SA_REP' OR job_id = 'AD_PREs' AND salary > 15000; LAST_NAME JOB_ID SALARY ------------------------- ---------- ---------- Tucker SA_REP 10000 Bernstein SA_REP 9500 Hall SA_REP 9000 Olsen SA_REP 8000 Cambrault SA_REP 7500 Tuvault SA_REP 7000 King SA_REP 10000 Sully SA_REP 9500 McEwen SA_REP 9000 Smith SA_REP 8000 Doran SA_REP 7500 LAST_NAME JOB_ID SALARY ------------------------- ---------- ---------- Sewall SA_REP 7000 Vishney SA_REP 10500 Greene SA_REP 9500 Marvins SA_REP 7200 Lee SA_REP 6800 Ande SA_REP 6400 Banda SA_REP 6200 Ozer SA_REP 11500 Bloom SA_REP 10000 Fox SA_REP 9600 Smith SA_REP 7400 LAST_NAME JOB_ID SALARY ------------------------- ---------- ---------- Bates SA_REP 7300 Kumar SA_REP 6100 Abel SA_REP 11000 Hutton SA_REP 8800 Taylor SA_REP 8600 Livingston SA_REP 8400 Grant SA_REP 7000 Johnson SA_REP 6200 30 rows selected. SQL> SELECT last_name,job_id,salary FROM employees WHERE (job_id = 'SA_REP' OR job_id = 'AD_PRES') AND salary > 15000; LAST_NAME JOB_ID SALARY ------------------------- ---------- ---------- King AD_PRES 24000
Using the ORDER BY Clause
- Sort retrieved rows with the ORDER BY clause:
- -ASC:Ascending order,default
- -DESC:Descending order
- The ORDER BY caluse come last in the SELECT statement
Sorting
- Sorting in descending order:
SQL> SELECT last_name,job_id,department_id,hire_date FROM employees ORDER BY hire_date; LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- De Haan AD_VP 90 13-JAN-01 Mavris HR_REP 40 07-JUN-02 Baer PR_REP 70 07-JUN-02 Higgins AC_MGR 110 07-JUN-02 Gietz AC_ACCOUNT 110 07-JUN-02 Faviet FI_ACCOUNT 100 16-AUG-02 Greenberg FI_MGR 100 17-AUG-02 Raphaely PU_MAN 30 07-DEC-02 Kaufling ST_MAN 50 01-MAY-03 Khoo PU_CLERK 30 18-MAY-03 King AD_PRES 90 17-JUN-03 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Ladwig ST_CLERK 50 14-JUL-03 Whalen AD_ASST 10 17-SEP-03 Rajs ST_CLERK 50 17-OCT-03 Sarchand SH_CLERK 50 27-JAN-04 King SA_REP 80 30-JAN-04 Bell SH_CLERK 50 04-FEB-04 Hartstein MK_MAN 20 17-FEB-04 Sully SA_REP 80 04-MAR-04 Abel SA_REP 80 11-MAY-04 Mallin ST_CLERK 50 14-JUN-04 Weiss ST_MAN 50 18-JUL-04 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- McEwen SA_REP 80 01-AUG-04 Russell SA_MAN 80 01-OCT-04 Partners SA_MAN 80 05-JAN-05 Davies ST_CLERK 50 29-JAN-05 Tucker SA_REP 80 30-JAN-05 Marlow ST_CLERK 50 16-FEB-05 Bull SH_CLERK 50 20-FEB-05 Everett SH_CLERK 50 03-MAR-05 Errazuriz SA_MAN 80 10-MAR-05 Smith SA_REP 80 10-MAR-05 Ozer SA_REP 80 11-MAR-05 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Hutton SA_REP 80 19-MAR-05 Bernstein SA_REP 80 24-MAR-05 Fripp ST_MAN 50 10-APR-05 Chung SH_CLERK 50 14-JUN-05 Austin IT_PROG 60 25-JUN-05 Nayer ST_CLERK 50 16-JUL-05 Tobias PU_CLERK 30 24-JUL-05 Dilly SH_CLERK 50 13-AUG-05 Fay MK_REP 20 17-AUG-05 Bissot ST_CLERK 50 20-AUG-05 Hall SA_REP 80 20-AUG-05 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Kochhar AD_VP 90 21-SEP-05 Chen FI_ACCOUNT 100 28-SEP-05 Sciarra FI_ACCOUNT 100 30-SEP-05 Vollman ST_MAN 50 10-OCT-05 Stiles ST_CLERK 50 26-OCT-05 Atkinson ST_CLERK 50 30-OCT-05 Vishney SA_REP 80 11-NOV-05 Doran SA_REP 80 15-DEC-05 Baida PU_CLERK 30 24-DEC-05 Hunold IT_PROG 60 03-JAN-06 Taylor SH_CLERK 50 24-JAN-06 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Fox SA_REP 80 24-JAN-06 Pataballa IT_PROG 60 05-FEB-06 Seo ST_CLERK 50 12-FEB-06 Fleaur SH_CLERK 50 23-FEB-06 Urman FI_ACCOUNT 100 07-MAR-06 Matos ST_CLERK 50 15-MAR-06 Bloom SA_REP 80 23-MAR-06 Taylor SA_REP 80 24-MAR-06 Olsen SA_REP 80 30-MAR-06 Patel ST_CLERK 50 06-APR-06 Livingston SA_REP 80 23-APR-06 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Walsh SH_CLERK 50 24-APR-06 Feeney SH_CLERK 50 23-MAY-06 Dellinger SH_CLERK 50 24-JUN-06 McCain SH_CLERK 50 01-JUL-06 Vargas ST_CLERK 50 09-JUL-06 Gates SH_CLERK 50 11-JUL-06 Rogers ST_CLERK 50 26-AUG-06 Mikkilineni ST_CLERK 50 28-SEP-06 Sewall SA_REP 80 03-NOV-06 Himuro PU_CLERK 30 15-NOV-06 Cambrault SA_REP 80 09-DEC-06 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Landry ST_CLERK 50 14-JAN-07 Cabrio SH_CLERK 50 07-FEB-07 Lorentz IT_PROG 60 07-FEB-07 Smith SA_REP 80 23-FEB-07 Jones SH_CLERK 50 17-MAR-07 Greene SA_REP 80 19-MAR-07 Bates SA_REP 80 24-MAR-07 Olson ST_CLERK 50 10-APR-07 Ernst IT_PROG 60 21-MAY-07 Grant SA_REP 24-MAY-07 OConnell SH_CLERK 50 21-JUN-07 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Sullivan SH_CLERK 50 21-JUN-07 Colmenares PU_CLERK 30 10-AUG-07 Cambrault SA_MAN 80 15-OCT-07 Mourgos ST_MAN 50 16-NOV-07 Tuvault SA_REP 80 23-NOV-07 Popp FI_ACCOUNT 100 07-DEC-07 Gee ST_CLERK 50 12-DEC-07 Perkins SH_CLERK 50 19-DEC-07 Johnson SA_REP 80 04-JAN-08 Grant SH_CLERK 50 13-JAN-08 Marvins SA_REP 80 24-JAN-08 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Zlotkey SA_MAN 80 29-JAN-08 Geoni SH_CLERK 50 03-FEB-08 Philtanker ST_CLERK 50 06-FEB-08 Lee SA_REP 80 23-FEB-08 Markle ST_CLERK 50 08-MAR-08 Ande SA_REP 80 24-MAR-08 Banda SA_REP 80 21-APR-08 Kumar SA_REP 80 21-APR-08 107 rows selected.
- Sorting by column alias:(ORDER BY Clause中可以使用alias,WHERE Clause中不可以使用alias)
SQL> SELECT employee_id,last_name,salary*12 annsal FROM employees ORDER BY annsal; EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 132 Olson 25200 136 Philtanker 26400 128 Markle 26400 127 Landry 28800 135 Gee 28800 191 Perkins 30000 119 Colmenares 30000 140 Patel 30000 144 Vargas 30000 182 Sullivan 30000 131 Marlow 30000 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 198 OConnell 31200 199 Grant 31200 118 Himuro 31200 143 Matos 31200 139 Seo 32400 126 Mikkilineni 32400 195 Jones 33600 183 Geoni 33600 117 Tobias 33600 130 Atkinson 33600 116 Baida 34800 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 190 Gates 34800 134 Rogers 34800 197 Feeney 36000 187 Cabrio 36000 181 Fleaur 37200 142 Davies 37200 196 Walsh 37200 115 Khoo 37200 125 Nayer 38400 180 Taylor 38400 138 Stiles 38400 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 194 McCain 38400 129 Bissot 39600 133 Mallin 39600 186 Dellinger 40800 141 Rajs 42000 189 Dilly 43200 137 Ladwig 43200 188 Chung 45600 193 Everett 46800 192 Bell 48000 185 Bull 49200 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 107 Lorentz 50400 184 Sarchand 50400 200 Whalen 52800 105 Austin 57600 106 Pataballa 57600 124 Mourgos 69600 202 Fay 72000 104 Ernst 72000 173 Kumar 73200 179 Johnson 74400 167 Banda 74400 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 166 Ande 76800 203 Mavris 78000 123 Vollman 78000 165 Lee 81600 113 Popp 82800 155 Tuvault 84000 161 Sewall 84000 178 Grant 84000 164 Marvins 86400 172 Bates 87600 171 Smith 88800 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 154 Cambrault 90000 160 Doran 90000 111 Sciarra 92400 112 Urman 93600 122 Kaufling 94800 120 Weiss 96000 153 Olsen 96000 159 Smith 96000 121 Fripp 98400 110 Chen 98400 206 Gietz 99600 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 177 Livingston 100800 176 Taylor 103200 175 Hutton 105600 152 Hall 108000 109 Faviet 108000 103 Hunold 108000 158 McEwen 108000 151 Bernstein 114000 157 Sully 114000 163 Greene 114000 170 Fox 115200 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 204 Baer 120000 150 Tucker 120000 156 King 120000 169 Bloom 120000 162 Vishney 126000 149 Zlotkey 126000 114 Raphaely 132000 174 Abel 132000 148 Cambrault 132000 168 Ozer 138000 147 Errazuriz 144000 EMPLOYEE_ID LAST_NAME ANNSAL ----------- ------------------------- ---------- 205 Higgins 144096 108 Greenberg 144096 201 Hartstein 156000 146 Partners 162000 145 Russell 168000 102 De Haan 204000 101 Kochhar 204000 100 King 288000 107 rows selected.
- Sorting by using the column`s numeric postion:
SQL> SELECT last_name,job_id,department_id,hire_date FROM employees ORDER BY 3; LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Whalen AD_ASST 10 17-SEP-03 Hartstein MK_MAN 20 17-FEB-04 Fay MK_REP 20 17-AUG-05 Raphaely PU_MAN 30 07-DEC-02 Colmenares PU_CLERK 30 10-AUG-07 Khoo PU_CLERK 30 18-MAY-03 Baida PU_CLERK 30 24-DEC-05 Tobias PU_CLERK 30 24-JUL-05 Himuro PU_CLERK 30 15-NOV-06 Mavris HR_REP 40 07-JUN-02 OConnell SH_CLERK 50 21-JUN-07 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Grant SH_CLERK 50 13-JAN-08 Weiss ST_MAN 50 18-JUL-04 Fripp ST_MAN 50 10-APR-05 Kaufling ST_MAN 50 01-MAY-03 Vollman ST_MAN 50 10-OCT-05 Mourgos ST_MAN 50 16-NOV-07 Nayer ST_CLERK 50 16-JUL-05 Mikkilineni ST_CLERK 50 28-SEP-06 Landry ST_CLERK 50 14-JAN-07 Markle ST_CLERK 50 08-MAR-08 Bissot ST_CLERK 50 20-AUG-05 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Atkinson ST_CLERK 50 30-OCT-05 Marlow ST_CLERK 50 16-FEB-05 Olson ST_CLERK 50 10-APR-07 Mallin ST_CLERK 50 14-JUN-04 Rogers ST_CLERK 50 26-AUG-06 Gee ST_CLERK 50 12-DEC-07 Philtanker ST_CLERK 50 06-FEB-08 Ladwig ST_CLERK 50 14-JUL-03 Stiles ST_CLERK 50 26-OCT-05 Seo ST_CLERK 50 12-FEB-06 Patel ST_CLERK 50 06-APR-06 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Rajs ST_CLERK 50 17-OCT-03 Davies ST_CLERK 50 29-JAN-05 Matos ST_CLERK 50 15-MAR-06 Vargas ST_CLERK 50 09-JUL-06 Taylor SH_CLERK 50 24-JAN-06 Fleaur SH_CLERK 50 23-FEB-06 Sullivan SH_CLERK 50 21-JUN-07 Geoni SH_CLERK 50 03-FEB-08 Sarchand SH_CLERK 50 27-JAN-04 Bull SH_CLERK 50 20-FEB-05 Dellinger SH_CLERK 50 24-JUN-06 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Cabrio SH_CLERK 50 07-FEB-07 Chung SH_CLERK 50 14-JUN-05 Dilly SH_CLERK 50 13-AUG-05 Gates SH_CLERK 50 11-JUL-06 Perkins SH_CLERK 50 19-DEC-07 Bell SH_CLERK 50 04-FEB-04 Everett SH_CLERK 50 03-MAR-05 McCain SH_CLERK 50 01-JUL-06 Jones SH_CLERK 50 17-MAR-07 Walsh SH_CLERK 50 24-APR-06 Feeney SH_CLERK 50 23-MAY-06 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Ernst IT_PROG 60 21-MAY-07 Hunold IT_PROG 60 03-JAN-06 Lorentz IT_PROG 60 07-FEB-07 Pataballa IT_PROG 60 05-FEB-06 Austin IT_PROG 60 25-JUN-05 Baer PR_REP 70 07-JUN-02 Taylor SA_REP 80 24-MAR-06 Livingston SA_REP 80 23-APR-06 Johnson SA_REP 80 04-JAN-08 Hutton SA_REP 80 19-MAR-05 Abel SA_REP 80 11-MAY-04 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Kumar SA_REP 80 21-APR-08 Bates SA_REP 80 24-MAR-07 Smith SA_REP 80 23-FEB-07 Fox SA_REP 80 24-JAN-06 Bloom SA_REP 80 23-MAR-06 Ozer SA_REP 80 11-MAR-05 Russell SA_MAN 80 01-OCT-04 Partners SA_MAN 80 05-JAN-05 Errazuriz SA_MAN 80 10-MAR-05 Cambrault SA_MAN 80 15-OCT-07 Zlotkey SA_MAN 80 29-JAN-08 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Tucker SA_REP 80 30-JAN-05 Bernstein SA_REP 80 24-MAR-05 Hall SA_REP 80 20-AUG-05 Olsen SA_REP 80 30-MAR-06 Cambrault SA_REP 80 09-DEC-06 Tuvault SA_REP 80 23-NOV-07 King SA_REP 80 30-JAN-04 Sully SA_REP 80 04-MAR-04 McEwen SA_REP 80 01-AUG-04 Smith SA_REP 80 10-MAR-05 Doran SA_REP 80 15-DEC-05 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Sewall SA_REP 80 03-NOV-06 Vishney SA_REP 80 11-NOV-05 Greene SA_REP 80 19-MAR-07 Marvins SA_REP 80 24-JAN-08 Lee SA_REP 80 23-FEB-08 Ande SA_REP 80 24-MAR-08 Banda SA_REP 80 21-APR-08 Kochhar AD_VP 90 21-SEP-05 King AD_PRES 90 17-JUN-03 De Haan AD_VP 90 13-JAN-01 Chen FI_ACCOUNT 100 28-SEP-05 LAST_NAME JOB_ID DEPARTMENT_ID HIRE_DATE ------------------------- ---------- ------------- --------- Greenberg FI_MGR 100 17-AUG-02 Sciarra FI_ACCOUNT 100 30-SEP-05 Urman FI_ACCOUNT 100 07-MAR-06 Popp FI_ACCOUNT 100 07-DEC-07 Faviet FI_ACCOUNT 100 16-AUG-02 Gietz AC_ACCOUNT 110 07-JUN-02 Higgins AC_MGR 110 07-JUN-02 Grant SA_REP 24-MAY-07 107 rows selected.
- Sorting by multiple columns:
SQL> SELECT last_name,department_id,salary FROM employees ORDER BY department_id,salary DESC; LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Whalen 10 4400 Hartstein 20 13000 Fay 20 6000 Raphaely 30 11000 Khoo 30 3100 Baida 30 2900 Tobias 30 2800 Himuro 30 2600 Colmenares 30 2500 Mavris 40 6500 Fripp 50 8200 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Weiss 50 8000 Kaufling 50 7900 Vollman 50 6500 Mourgos 50 5800 Sarchand 50 4200 Bull 50 4100 Bell 50 4000 Everett 50 3900 Chung 50 3800 Dilly 50 3600 Ladwig 50 3600 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Rajs 50 3500 Dellinger 50 3400 Bissot 50 3300 Mallin 50 3300 Stiles 50 3200 McCain 50 3200 Nayer 50 3200 Taylor 50 3200 Fleaur 50 3100 Davies 50 3100 Walsh 50 3100 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Cabrio 50 3000 Feeney 50 3000 Gates 50 2900 Rogers 50 2900 Geoni 50 2800 Atkinson 50 2800 Jones 50 2800 Seo 50 2700 Mikkilineni 50 2700 OConnell 50 2600 Grant 50 2600 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Matos 50 2600 Patel 50 2500 Sullivan 50 2500 Marlow 50 2500 Perkins 50 2500 Vargas 50 2500 Gee 50 2400 Landry 50 2400 Philtanker 50 2200 Markle 50 2200 Olson 50 2100 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Hunold 60 9000 Ernst 60 6000 Pataballa 60 4800 Austin 60 4800 Lorentz 60 4200 Baer 70 10000 Russell 80 14000 Partners 80 13500 Errazuriz 80 12000 Ozer 80 11500 Cambrault 80 11000 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Abel 80 11000 Vishney 80 10500 Zlotkey 80 10500 Tucker 80 10000 Bloom 80 10000 King 80 10000 Fox 80 9600 Bernstein 80 9500 Greene 80 9500 Sully 80 9500 McEwen 80 9000 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Hall 80 9000 Hutton 80 8800 Taylor 80 8600 Livingston 80 8400 Olsen 80 8000 Smith 80 8000 Cambrault 80 7500 Doran 80 7500 Smith 80 7400 Bates 80 7300 Marvins 80 7200 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Sewall 80 7000 Tuvault 80 7000 Lee 80 6800 Ande 80 6400 Banda 80 6200 Johnson 80 6200 Kumar 80 6100 King 90 24000 De Haan 90 17000 Kochhar 90 17000 Greenberg 100 12008 LAST_NAME DEPARTMENT_ID SALARY ------------------------- ------------- ---------- Faviet 100 9000 Chen 100 8200 Urman 100 7800 Sciarra 100 7700 Popp 100 6900 Higgins 110 12008 Gietz 110 8300 Grant 7000 107 rows selected.
注意:假如在排序的过程中,存在NULL值,那么在升序的情况下,NULL值放在最后;在降序的情况下,NULL值放在最前;但是,也可以自定义NULL值存放的位置顺序,就是在后面通过指定NULLS FIRST或者NULLS LAST关键字来实现.
Substitution Variables
Substitution variables是客户端的概念,不属于Oracle server side。
- Use substitution variables to
- -Temporarily store values with single-ampersand(&) and double-ampersand(&&) substitution
- Use substitution variables to supplement the following:
- -WHERE Clause
- -ORDER BY Clause
- -Column expression
- -Table names
- Entire SELECT statements
Using the single-Ampersand substitution variable
Use a variable prefixed with an ampersand(&) to prompt the user for a value:
SQL> SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = &employee_num; Enter value for employee_num: 128 old 1: SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = &employee_num new 1: SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = 128 EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 128 Markle 2200 50
Character and Date Values with Substitution variables
Use single quotation marks for date and character values:(就是说数值,不用加单引号,但是如果是字符串或者日期,就需要加单引号.)
SQL> SELECT last_name,department_id,salary*12 FROM employees WHERE job_id = '&job_title'; Enter value for job_title: IT_PROG old 1: SELECT last_name,department_id,salary*12 FROM employees WHERE job_id = '&job_title' new 1: SELECT last_name,department_id,salary*12 FROM employees WHERE job_id = 'IT_PROG' LAST_NAME DEPARTMENT_ID SALARY*12 ------------------------- ------------- ---------- Hunold 60 108000 Ernst 60 72000 Austin 60 57600 Pataballa 60 57600 Lorentz 60 50400
Specifying Column Names,Expressions,and Text
SQL> SELECT employee_id,last_name,job_id,&column_name FROM employees WHERE &condition ORDER BY &order_column; Enter value for column_name: salary Enter value for condition: salary>15000 Enter value for order_column: last_name old 1: SELECT employee_id,last_name,job_id,&column_name FROM employees WHERE &condition ORDER BY &order_column new 1: SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary>15000 ORDER BY last_name EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 102 De Haan AD_VP 17000 100 King AD_PRES 24000 101 Kochhar AD_VP 17000
Using the Double-Ampersand Substitution variable
use double ampersand(&&) if you want to reuse the variable value without prompting the user each time:
SQL> SELECT employee_id,last_name,job_id, &column_name FROM employees ORDER BY &column_name; Enter value for column_name: salary Enter value for column_name: last_name old 1: SELECT employee_id,last_name,job_id, &column_name FROM employees ORDER BY &column_name new 1: SELECT employee_id,last_name,job_id, salary FROM employees ORDER BY last_name EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 174 Abel SA_REP 11000 166 Ande SA_REP 6400 130 Atkinson ST_CLERK 2800 105 Austin IT_PROG 4800 204 Baer PR_REP 10000 116 Baida PU_CLERK 2900 167 Banda SA_REP 6200 172 Bates SA_REP 7300 192 Bell SH_CLERK 4000 151 Bernstein SA_REP 9500 129 Bissot ST_CLERK 3300 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 169 Bloom SA_REP 10000 185 Bull SH_CLERK 4100 187 Cabrio SH_CLERK 3000 148 Cambrault SA_MAN 11000 154 Cambrault SA_REP 7500 110 Chen FI_ACCOUNT 8200 188 Chung SH_CLERK 3800 119 Colmenares PU_CLERK 2500 142 Davies ST_CLERK 3100 102 De Haan AD_VP 17000 186 Dellinger SH_CLERK 3400 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 189 Dilly SH_CLERK 3600 160 Doran SA_REP 7500 104 Ernst IT_PROG 6000 147 Errazuriz SA_MAN 12000 193 Everett SH_CLERK 3900 109 Faviet FI_ACCOUNT 9000 202 Fay MK_REP 6000 197 Feeney SH_CLERK 3000 181 Fleaur SH_CLERK 3100 170 Fox SA_REP 9600 121 Fripp ST_MAN 8200 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 190 Gates SH_CLERK 2900 135 Gee ST_CLERK 2400 183 Geoni SH_CLERK 2800 206 Gietz AC_ACCOUNT 8300 199 Grant SH_CLERK 2600 178 Grant SA_REP 7000 108 Greenberg FI_MGR 12008 163 Greene SA_REP 9500 152 Hall SA_REP 9000 201 Hartstein MK_MAN 13000 205 Higgins AC_MGR 12008 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 118 Himuro PU_CLERK 2600 103 Hunold IT_PROG 9000 175 Hutton SA_REP 8800 179 Johnson SA_REP 6200 195 Jones SH_CLERK 2800 122 Kaufling ST_MAN 7900 115 Khoo PU_CLERK 3100 100 King AD_PRES 24000 156 King SA_REP 10000 101 Kochhar AD_VP 17000 173 Kumar SA_REP 6100 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 137 Ladwig ST_CLERK 3600 127 Landry ST_CLERK 2400 165 Lee SA_REP 6800 177 Livingston SA_REP 8400 107 Lorentz IT_PROG 4200 133 Mallin ST_CLERK 3300 128 Markle ST_CLERK 2200 131 Marlow ST_CLERK 2500 164 Marvins SA_REP 7200 143 Matos ST_CLERK 2600 203 Mavris HR_REP 6500 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 194 McCain SH_CLERK 3200 158 McEwen SA_REP 9000 126 Mikkilineni ST_CLERK 2700 124 Mourgos ST_MAN 5800 125 Nayer ST_CLERK 3200 198 OConnell SH_CLERK 2600 153 Olsen SA_REP 8000 132 Olson ST_CLERK 2100 168 Ozer SA_REP 11500 146 Partners SA_MAN 13500 106 Pataballa IT_PROG 4800 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 140 Patel ST_CLERK 2500 191 Perkins SH_CLERK 2500 136 Philtanker ST_CLERK 2200 113 Popp FI_ACCOUNT 6900 141 Rajs ST_CLERK 3500 114 Raphaely PU_MAN 11000 134 Rogers ST_CLERK 2900 145 Russell SA_MAN 14000 184 Sarchand SH_CLERK 4200 111 Sciarra FI_ACCOUNT 7700 139 Seo ST_CLERK 2700 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 161 Sewall SA_REP 7000 171 Smith SA_REP 7400 159 Smith SA_REP 8000 138 Stiles ST_CLERK 3200 182 Sullivan SH_CLERK 2500 157 Sully SA_REP 9500 176 Taylor SA_REP 8600 180 Taylor SH_CLERK 3200 117 Tobias PU_CLERK 2800 150 Tucker SA_REP 10000 155 Tuvault SA_REP 7000 EMPLOYEE_ID LAST_NAME JOB_ID SALARY ----------- ------------------------- ---------- ---------- 112 Urman FI_ACCOUNT 7800 144 Vargas ST_CLERK 2500 162 Vishney SA_REP 10500 123 Vollman ST_MAN 6500 196 Walsh SH_CLERK 3100 120 Weiss ST_MAN 8000 200 Whalen AD_ASST 4400 149 Zlotkey SA_MAN 10500 107 rows selected.
SQL> SELECT employee_id,last_name,job_id,&&column_name FROM employees ORDER BY &column_name; Enter value for column_name: department_id old 1: SELECT employee_id,last_name,job_id,&&column_name FROM employees ORDER BY &column_name new 1: SELECT employee_id,last_name,job_id,department_id FROM employees ORDER BY department_id EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 200 Whalen AD_ASST 10 201 Hartstein MK_MAN 20 202 Fay MK_REP 20 114 Raphaely PU_MAN 30 119 Colmenares PU_CLERK 30 115 Khoo PU_CLERK 30 116 Baida PU_CLERK 30 117 Tobias PU_CLERK 30 118 Himuro PU_CLERK 30 203 Mavris HR_REP 40 198 OConnell SH_CLERK 50 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 199 Grant SH_CLERK 50 120 Weiss ST_MAN 50 121 Fripp ST_MAN 50 122 Kaufling ST_MAN 50 123 Vollman ST_MAN 50 124 Mourgos ST_MAN 50 125 Nayer ST_CLERK 50 126 Mikkilineni ST_CLERK 50 127 Landry ST_CLERK 50 128 Markle ST_CLERK 50 129 Bissot ST_CLERK 50 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 130 Atkinson ST_CLERK 50 131 Marlow ST_CLERK 50 132 Olson ST_CLERK 50 133 Mallin ST_CLERK 50 134 Rogers ST_CLERK 50 135 Gee ST_CLERK 50 136 Philtanker ST_CLERK 50 137 Ladwig ST_CLERK 50 138 Stiles ST_CLERK 50 139 Seo ST_CLERK 50 140 Patel ST_CLERK 50 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 141 Rajs ST_CLERK 50 142 Davies ST_CLERK 50 143 Matos ST_CLERK 50 144 Vargas ST_CLERK 50 180 Taylor SH_CLERK 50 181 Fleaur SH_CLERK 50 182 Sullivan SH_CLERK 50 183 Geoni SH_CLERK 50 184 Sarchand SH_CLERK 50 185 Bull SH_CLERK 50 186 Dellinger SH_CLERK 50 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 187 Cabrio SH_CLERK 50 188 Chung SH_CLERK 50 189 Dilly SH_CLERK 50 190 Gates SH_CLERK 50 191 Perkins SH_CLERK 50 192 Bell SH_CLERK 50 193 Everett SH_CLERK 50 194 McCain SH_CLERK 50 195 Jones SH_CLERK 50 196 Walsh SH_CLERK 50 197 Feeney SH_CLERK 50 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 104 Ernst IT_PROG 60 103 Hunold IT_PROG 60 107 Lorentz IT_PROG 60 106 Pataballa IT_PROG 60 105 Austin IT_PROG 60 204 Baer PR_REP 70 176 Taylor SA_REP 80 177 Livingston SA_REP 80 179 Johnson SA_REP 80 175 Hutton SA_REP 80 174 Abel SA_REP 80 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 173 Kumar SA_REP 80 172 Bates SA_REP 80 171 Smith SA_REP 80 170 Fox SA_REP 80 169 Bloom SA_REP 80 168 Ozer SA_REP 80 145 Russell SA_MAN 80 146 Partners SA_MAN 80 147 Errazuriz SA_MAN 80 148 Cambrault SA_MAN 80 149 Zlotkey SA_MAN 80 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 150 Tucker SA_REP 80 151 Bernstein SA_REP 80 152 Hall SA_REP 80 153 Olsen SA_REP 80 154 Cambrault SA_REP 80 155 Tuvault SA_REP 80 156 King SA_REP 80 157 Sully SA_REP 80 158 McEwen SA_REP 80 159 Smith SA_REP 80 160 Doran SA_REP 80 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 161 Sewall SA_REP 80 162 Vishney SA_REP 80 163 Greene SA_REP 80 164 Marvins SA_REP 80 165 Lee SA_REP 80 166 Ande SA_REP 80 167 Banda SA_REP 80 101 Kochhar AD_VP 90 100 King AD_PRES 90 102 De Haan AD_VP 90 110 Chen FI_ACCOUNT 100 EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 108 Greenberg FI_MGR 100 111 Sciarra FI_ACCOUNT 100 112 Urman FI_ACCOUNT 100 113 Popp FI_ACCOUNT 100 109 Faviet FI_ACCOUNT 100 206 Gietz AC_ACCOUNT 110 205 Higgins AC_MGR 110 178 Grant SA_REP 107 rows selected.
Using the DEFINE Command
- Use the DEFINE command to create and assign a value to a variable.
- Use the UNDEFINE command to remove a variable.
SQL> DEFINE employee_num = 200 SQL> SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = &employee_num; old 1: SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = &employee_num new 1: SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = 200 EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 200 Whalen 4400 10 SQL> UNDEFINE employee_num
SQL> DEFINE employee_num=200 SQL> define DEFINE _DATE = "14-APR-13" (CHAR) DEFINE _CONNECT_IDENTIFIER = "poli" (CHAR) DEFINE _USER = "HR" (CHAR) DEFINE _PRIVILEGE = "" (CHAR) DEFINE _SQLPLUS_RELEASE = "1102000100" (CHAR) DEFINE _EDITOR = "ed" (CHAR) DEFINE _O_VERSION = "Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR) DEFINE _O_RELEASE = "1102000100" (CHAR) DEFINE _RC = "0" (CHAR) DEFINE COLUMN_NAME = "department_id" (CHAR) DEFINE EMPLOYEE_NUM = "200" (CHAR)
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:
SQL> l 1* SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = &employee_num SQL> set verify on SQL> / Enter value for employee_num: 123 old 1: SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = &employee_num new 1: SELECT employee_id,last_name,salary,department_id FROM employees WHERE employee_id = 123 EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 123 Vollman 6500 50 SQL> set verify off SQL> / Enter value for employee_num: 123 EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID ----------- ------------------------- ---------- ------------- 123 Vollman 6500 50 SQL>
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,and NULL operators
- -Apply the logical AND,OR,and NOT operators
- Use the ORDER BY clause to sort rows of output:
SELECT * | {[DISTINCT] column|expression [alias],...} FROM table [WHERE conditions(s)] [ORDER BY {column,expr,alias} [ASC|DESC]];
- Use ampersand substitution to restrict and sort output at run time.