SlideShare a Scribd company logo
1 of 32
Download to read offline
Copyright © 2004, Oracle. All rights reserved.
Restricting and Sorting Data
Copyright © 2004, Oracle. All rights reserved.
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 in iSQL*Plus to
restrict and sort output at run time
Copyright © 2004, Oracle. All rights reserved.
Limiting Rows Using a Selection
“retrieve all
employees in
department 90”
EMPLOYEES
…
Copyright © 2004, Oracle. All rights reserved.
Limiting the Rows That Are Selected
• Restrict the rows that are returned by using the
WHERE clause:
• The WHERE clause follows the FROM clause.
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;
Using the WHERE Clause
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen' ;
Character Strings and Dates
• Character strings and date values are enclosed by
single quotation marks.
• Character values are case-sensitive, and date
values are format-sensitive.
• The default date format is DD-MON-RR.
Copyright © 2004, Oracle. All rights reserved.
Comparison Conditions
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<
Less than or equal to<=
Greater than or equal to>=
Greater than>
Equal to=
MeaningOperator
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, salary
FROM employees
WHERE salary <= 3000 ;
Using Comparison Conditions
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500 ;
Using the BETWEEN Condition
Use the BETWEEN condition to display rows based on a
range of values:
Lower limit Upper limit
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100, 101, 201) ;
Using the IN Condition
Use the IN membership condition to test for values in
a list:
Copyright © 2004, Oracle. All rights reserved.
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%' ;
Using the LIKE Condition
• Use the LIKE condition 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.
Copyright © 2004, Oracle. All rights reserved.
• You can combine pattern-matching characters:
• You can use the ESCAPE identifier to search for the
actual % and _ symbols.
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%' ;
Using the LIKE Condition
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL ;
Using the NULL Conditions
Test for nulls with the IS NULL operator.
Copyright © 2004, Oracle. All rights reserved.
Logical Conditions
Returns TRUE if the following
condition is false
NOT
Returns TRUE if either component
condition is true
OR
Returns TRUE if both component
conditions are true
AND
MeaningOperator
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >=10000
AND job_id LIKE '%MAN%' ;
Using the AND Operator
AND requires both conditions to be true:
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%' ;
Using the OR Operator
OR requires either condition to be true:
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
Using the NOT Operator
Copyright © 2004, Oracle. All rights reserved.
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
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, job_id, salary
FROM 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
Copyright © 2004, Oracle. All rights reserved.
Using the ORDER BY Clause
• Sort retrieved rows with the ORDER BY clause:
– ASC: ascending order, default
– DESC: descending order
• The ORDER BY clause comes last in the SELECT
statement:
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date ;
…
Copyright © 2004, Oracle. All rights reserved.
Sorting
• Sorting in descending order:
• Sorting by column alias:
• Sorting by multiple columns:
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ; 1
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal ;
2
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;
3
Copyright © 2004, Oracle. All rights reserved.
Substitution Variables
... salary = ? …
… department_id = ? …
... last_name = ? ...
I want
to query
different
values.
Copyright © 2004, Oracle. All rights reserved.
Substitution Variables
• Use iSQL*Plus substitution variables to:
– Temporarily store values with single-ampersand (&)
and double-ampersand (&&) substitution
• Use substitution variables to supplement the
following:
– WHERE conditions
– ORDER BY clauses
– Column expressions
– Table names
– Entire SELECT statements
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;
Using the & Substitution Variable
Use a variable prefixed with an ampersand (&) to
prompt the user for a value:
Copyright © 2004, Oracle. All rights reserved.
Using the & Substitution Variable
101
1
2
Copyright © 2004, Oracle. All rights reserved.
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:
Copyright © 2004, Oracle. All rights reserved.
Specifying Column Names,
Expressions, and Text
SELECT employee_id, last_name, job_id,&column_name
FROM employees
WHERE &condition
ORDER BY &order_column ;
salary
salary > 15000
last_name
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, last_name, job_id, &&column_name
FROM employees
ORDER BY &column_name ;
…
Using the && Substitution Variable
Use the double ampersand (&&) if you want to reuse
the variable value without prompting the user each
time:
Copyright © 2004, Oracle. All rights reserved.
Using the iSQL*Plus DEFINE Command
• Use the iSQL*Plus DEFINE command to create and
assign a value to a variable.
• Use the iSQL*Plus 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
Copyright © 2004, Oracle. All rights reserved.
old 3: WHERE employee_id = &employee_num
new 3: WHERE employee_id = 200
SET VERIFY ON
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num;
Using the VERIFY Command
Use the VERIFY command to toggle the display of the
substitution variable, both before and after iSQL*Plus
replaces substitution variables with values:
Copyright © 2004, Oracle. All rights reserved.
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, alias} [ASC|DESC]] ;
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, LIKE, and NULL conditions
– Apply the logical AND, OR, and NOT operators
• Use the ORDER BY clause to sort rows of output:
• Use ampersand substitution in iSQL*Plus to
restrict and sort output at run time
Copyright © 2004, Oracle. All rights reserved.
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 ORDER BY clause
• Using substitution variables to add flexibility to
your SQL SELECT statements

More Related Content

Viewers also liked

Viewers also liked (8)

Les01
Les01Les01
Les01
 
Les02
Les02Les02
Les02
 
Tupen 8 1235010002
Tupen 8   1235010002Tupen 8   1235010002
Tupen 8 1235010002
 
Les02
Les02Les02
Les02
 
Les02
Les02Les02
Les02
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 

Similar to Les02

e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting data
ecomputernotes
 

Similar to Les02 (20)

Les06
Les06Les06
Les06
 
Les02.ppt
Les02.pptLes02.ppt
Les02.ppt
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
 
Les02
Les02Les02
Les02
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句Lesson02 学会使用WHERE、ORDER BY子句
Lesson02 学会使用WHERE、ORDER BY子句
 
Les02
Les02Les02
Les02
 
Les07
Les07Les07
Les07
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
 
Les06
Les06Les06
Les06
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Les06
Les06Les06
Les06
 
Les06
Les06Les06
Les06
 
e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting data
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Oracle examples
Oracle examplesOracle examples
Oracle examples
 
Les08
Les08Les08
Les08
 
Les04
Les04Les04
Les04
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
plsql les01
 plsql les01 plsql les01
plsql les01
 

More from Abrianto Nugraha

More from Abrianto Nugraha (20)

Ds sn is-02
Ds sn is-02Ds sn is-02
Ds sn is-02
 
Ds sn is-01
Ds sn is-01Ds sn is-01
Ds sn is-01
 
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkapPertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
 
04 pemodelan spk
04 pemodelan spk04 pemodelan spk
04 pemodelan spk
 
02 sistem pengambilan-keputusan_revised
02 sistem pengambilan-keputusan_revised02 sistem pengambilan-keputusan_revised
02 sistem pengambilan-keputusan_revised
 
01 pengantar sistem-pendukung_keputusan
01 pengantar sistem-pendukung_keputusan01 pengantar sistem-pendukung_keputusan
01 pengantar sistem-pendukung_keputusan
 
Pertemuan 7
Pertemuan 7Pertemuan 7
Pertemuan 7
 
Pertemuan 7 dan_8
Pertemuan 7 dan_8Pertemuan 7 dan_8
Pertemuan 7 dan_8
 
Pertemuan 5
Pertemuan 5Pertemuan 5
Pertemuan 5
 
Pertemuan 6
Pertemuan 6Pertemuan 6
Pertemuan 6
 
Pertemuan 4
Pertemuan 4Pertemuan 4
Pertemuan 4
 
Pertemuan 3
Pertemuan 3Pertemuan 3
Pertemuan 3
 
Pertemuan 2
Pertemuan 2Pertemuan 2
Pertemuan 2
 
Pertemuan 1
Pertemuan 1Pertemuan 1
Pertemuan 1
 
Modul 1 mengambil nilai parameter
Modul 1   mengambil nilai parameterModul 1   mengambil nilai parameter
Modul 1 mengambil nilai parameter
 
Modul 3 object oriented programming dalam php
Modul 3   object oriented programming dalam phpModul 3   object oriented programming dalam php
Modul 3 object oriented programming dalam php
 
Modul 2 menyimpan ke database
Modul 2  menyimpan ke databaseModul 2  menyimpan ke database
Modul 2 menyimpan ke database
 
Pbo 7
Pbo 7Pbo 7
Pbo 7
 
Pbo 6
Pbo 6Pbo 6
Pbo 6
 
Pbo 4
Pbo 4Pbo 4
Pbo 4
 

Recently uploaded

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

Les02

  • 1. Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data
  • 2. Copyright © 2004, Oracle. All rights reserved. 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 in iSQL*Plus to restrict and sort output at run time
  • 3. Copyright © 2004, Oracle. All rights reserved. Limiting Rows Using a Selection “retrieve all employees in department 90” EMPLOYEES …
  • 4. Copyright © 2004, Oracle. All rights reserved. Limiting the Rows That Are Selected • Restrict the rows that are returned by using the WHERE clause: • The WHERE clause follows the FROM clause. SELECT *|{[DISTINCT] column|expression [alias],...} FROM table [WHERE condition(s)];
  • 5. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ; Using the WHERE Clause
  • 6. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id, department_id FROM employees WHERE last_name = 'Whalen' ; Character Strings and Dates • Character strings and date values are enclosed by single quotation marks. • Character values are case-sensitive, and date values are format-sensitive. • The default date format is DD-MON-RR.
  • 7. Copyright © 2004, Oracle. All rights reserved. Comparison Conditions 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< Less than or equal to<= Greater than or equal to>= Greater than> Equal to= MeaningOperator
  • 8. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, salary FROM employees WHERE salary <= 3000 ; Using Comparison Conditions
  • 9. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500 ; Using the BETWEEN Condition Use the BETWEEN condition to display rows based on a range of values: Lower limit Upper limit
  • 10. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201) ; Using the IN Condition Use the IN membership condition to test for values in a list:
  • 11. Copyright © 2004, Oracle. All rights reserved. SELECT first_name FROM employees WHERE first_name LIKE 'S%' ; Using the LIKE Condition • Use the LIKE condition 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.
  • 12. Copyright © 2004, Oracle. All rights reserved. • You can combine pattern-matching characters: • You can use the ESCAPE identifier to search for the actual % and _ symbols. SELECT last_name FROM employees WHERE last_name LIKE '_o%' ; Using the LIKE Condition
  • 13. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL ; Using the NULL Conditions Test for nulls with the IS NULL operator.
  • 14. Copyright © 2004, Oracle. All rights reserved. Logical Conditions Returns TRUE if the following condition is false NOT Returns TRUE if either component condition is true OR Returns TRUE if both component conditions are true AND MeaningOperator
  • 15. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >=10000 AND job_id LIKE '%MAN%' ; Using the AND Operator AND requires both conditions to be true:
  • 16. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 OR job_id LIKE '%MAN%' ; Using the OR Operator OR requires either condition to be true:
  • 17. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id FROM employees WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ; Using the NOT Operator
  • 18. Copyright © 2004, Oracle. All rights reserved. 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
  • 19. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id, salary FROM 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
  • 20. Copyright © 2004, Oracle. All rights reserved. Using the ORDER BY Clause • Sort retrieved rows with the ORDER BY clause: – ASC: ascending order, default – DESC: descending order • The ORDER BY clause comes last in the SELECT statement: SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date ; …
  • 21. Copyright © 2004, Oracle. All rights reserved. Sorting • Sorting in descending order: • Sorting by column alias: • Sorting by multiple columns: SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date DESC ; 1 SELECT employee_id, last_name, salary*12 annsal FROM employees ORDER BY annsal ; 2 SELECT last_name, department_id, salary FROM employees ORDER BY department_id, salary DESC; 3
  • 22. Copyright © 2004, Oracle. All rights reserved. Substitution Variables ... salary = ? … … department_id = ? … ... last_name = ? ... I want to query different values.
  • 23. Copyright © 2004, Oracle. All rights reserved. Substitution Variables • Use iSQL*Plus substitution variables to: – Temporarily store values with single-ampersand (&) and double-ampersand (&&) substitution • Use substitution variables to supplement the following: – WHERE conditions – ORDER BY clauses – Column expressions – Table names – Entire SELECT statements
  • 24. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num ; Using the & Substitution Variable Use a variable prefixed with an ampersand (&) to prompt the user for a value:
  • 25. Copyright © 2004, Oracle. All rights reserved. Using the & Substitution Variable 101 1 2
  • 26. Copyright © 2004, Oracle. All rights reserved. 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:
  • 27. Copyright © 2004, Oracle. All rights reserved. Specifying Column Names, Expressions, and Text SELECT employee_id, last_name, job_id,&column_name FROM employees WHERE &condition ORDER BY &order_column ; salary salary > 15000 last_name
  • 28. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, job_id, &&column_name FROM employees ORDER BY &column_name ; … Using the && Substitution Variable Use the double ampersand (&&) if you want to reuse the variable value without prompting the user each time:
  • 29. Copyright © 2004, Oracle. All rights reserved. Using the iSQL*Plus DEFINE Command • Use the iSQL*Plus DEFINE command to create and assign a value to a variable. • Use the iSQL*Plus 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
  • 30. Copyright © 2004, Oracle. All rights reserved. old 3: WHERE employee_id = &employee_num new 3: WHERE employee_id = 200 SET VERIFY ON SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num; Using the VERIFY Command Use the VERIFY command to toggle the display of the substitution variable, both before and after iSQL*Plus replaces substitution variables with values:
  • 31. Copyright © 2004, Oracle. All rights reserved. SELECT *|{[DISTINCT] column|expression [alias],...} FROM table [WHERE condition(s)] [ORDER BY {column, expr, alias} [ASC|DESC]] ; 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, LIKE, and NULL conditions – Apply the logical AND, OR, and NOT operators • Use the ORDER BY clause to sort rows of output: • Use ampersand substitution in iSQL*Plus to restrict and sort output at run time
  • 32. Copyright © 2004, Oracle. All rights reserved. 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 ORDER BY clause • Using substitution variables to add flexibility to your SQL SELECT statements