SlideShare a Scribd company logo
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 in  i SQL*Plus to restrict and sort output at run time
Limiting Rows Using a Selection “ retrieve all employees in department 90” EMPLOYEES …
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) ];
Using the  WHERE  Clause SELECT employee_id, last_name, job_id, department_id FROM  employees WHERE  department_id = 90 ;
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-RR. SELECT last_name, job_id, department_id FROM  employees WHERE  last_name = 'Whalen' ;
Comparison Conditions Not equal to <> Between two values (inclusive) BETWEEN ...AND... Match any of a list of values  IN(set) Match a character pattern  LIKE Is a null value  IS NULL Less than < Less than or equal to <= Greater than or equal to >= Greater than > Equal to = Meaning Operator
Using Comparison Conditions SELECT last_name, salary FROM  employees WHERE  salary <= 3000 ;
Using the  BETWEEN  Condition Use the  BETWEEN  condition to display rows based on a range of values: SELECT last_name, salary FROM  employees WHERE  salary BETWEEN 2500 AND 3500 ; Lower limit Upper limit
Using the  IN  Condition Use the  IN  membership condition to test for values in a list: SELECT employee_id, last_name, salary, manager_id FROM  employees WHERE  manager_id IN (100, 101, 201) ;
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. SELECT first_name FROM  employees WHERE first_name LIKE 'S%' ;
You can combine pattern-matching characters: You can use the  ESCAPE  identifier to search for the actual  %  and  _  symbols. Using the  LIKE  Condition SELECT last_name FROM  employees WHERE  last_name LIKE '_o%' ;
Using the  NULL  Conditions Test for nulls with the  IS NULL  operator. SELECT last_name, manager_id FROM  employees WHERE  manager_id IS NULL ;
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 Meaning Operator
Using the  AND  Operator SELECT employee_id, last_name, job_id, salary FROM  employees WHERE  salary >=10000 AND  job_id LIKE '%MAN%' ; AND  requires both conditions to be true:
Using the  OR  Operator SELECT employee_id, last_name, job_id, salary FROM  employees WHERE  salary >= 10000 OR  job_id LIKE '%MAN%' ; OR  requires either condition to be true:
Using the  NOT  Operator SELECT last_name, job_id FROM  employees WHERE  job_id  NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
Rules of Precedence You can use parentheses to override rules of precedence. Not equal to 6 NOT  logical condition 7 AND  logical condition 8 OR  logical condition 9 IS   [NOT]   NULL ,  LIKE ,  [NOT]   IN 4 [NOT] BETWEEN 5 Comparison conditions 3 Concatenation operator 2 Arithmetic operators 1 Meaning Operator
Rules of Precedence SELECT last_name, job_id, salary FROM  employees WHERE  job_id = 'SA_REP' OR  job_id = 'AD_PRES' AND  salary > 15000; SELECT last_name, job_id, salary FROM  employees WHERE  ( job_id = 'SA_REP' OR  job_id = 'AD_PRES' ) AND  salary > 15000; 1 2
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 ; …
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
Substitution Variables ... salary = ? … …  department_id = ? …  ... last_name = ? ... I want  to query  different  values.
Substitution Variables Use  i SQL*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
Using the  &  Substitution Variable Use a variable prefixed with an ampersand ( & ) to prompt the user for a value: SELECT employee_id, last_name, salary, department_id FROM  employees WHERE  employee_id = &employee_num ;
Using the  &  Substitution Variable 101 1 2
Character and Date Values  with Substitution Variables Use single quotation marks for date and character values: SELECT last_name, department_id, salary*12 FROM  employees WHERE  job_id = '&job_title' ;
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
Using the  &&  Substitution Variable Use the double ampersand ( && ) if you want to reuse the variable value without prompting the user each time: SELECT  employee_id, last_name, job_id, &&column_name FROM  employees ORDER BY &column_name ; …
Using the  i SQL*Plus  DEFINE  Command Use the  i SQL*Plus  DEFINE  command to create and assign a value to a variable. Use the  i SQL*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
Using the  VERIFY  Command Use the  VERIFY  command to toggle the display of the substitution variable, both before and after  i SQL*Plus replaces substitution variables with values: 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;
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  i SQL*Plus to restrict and sort output at run time   SELECT  *|{[DISTINCT]  column|expression  [ alias ],...} FROM  table [WHERE  condition(s) ] [ORDER BY { column, expr, alias } [ASC|DESC]] ;
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
 
 
 
 
 
 
Ad

More Related Content

What's hot (20)

Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
Achmad Solichin
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
Salman Memon
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
SUBQUERIES.pptx
SUBQUERIES.pptxSUBQUERIES.pptx
SUBQUERIES.pptx
RenugadeviR5
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
Salman Memon
 
09 Managing Dependencies
09 Managing Dependencies09 Managing Dependencies
09 Managing Dependencies
rehaniltifat
 
SQL
SQLSQL
SQL
Vineeta Garg
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
Mohan Kumar.R
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
Nishant Munjal
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
mohdoracle
 
4. plsql
4. plsql4. plsql
4. plsql
Amrit Kaur
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
raj upadhyay
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
rehaniltifat
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
Srinath Maharana
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
rehaniltifat
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
Syed Zaid Irshad
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTION
Arun Sial
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
rehaniltifat
 
Check element entry value on particular effective date
Check element entry value on particular effective dateCheck element entry value on particular effective date
Check element entry value on particular effective date
Feras Ahmad
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
Achmad Solichin
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
Salman Memon
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
Salman Memon
 
09 Managing Dependencies
09 Managing Dependencies09 Managing Dependencies
09 Managing Dependencies
rehaniltifat
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
Mohan Kumar.R
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
Nishant Munjal
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
mohdoracle
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
raj upadhyay
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
rehaniltifat
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
rehaniltifat
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
Syed Zaid Irshad
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTION
Arun Sial
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
rehaniltifat
 
Check element entry value on particular effective date
Check element entry value on particular effective dateCheck element entry value on particular effective date
Check element entry value on particular effective date
Feras Ahmad
 

Similar to Les02 (restricting and sorting data) (20)

Les02
Les02Les02
Les02
Vijay Kumar
 
Day1_Structured Query Language2_To understand.ppt
Day1_Structured Query Language2_To understand.pptDay1_Structured Query Language2_To understand.ppt
Day1_Structured Query Language2_To understand.ppt
consravs
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
newrforce
 
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
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
Vibrant Technologies & Computers
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
Thuan Nguyen
 
Les02
Les02Les02
Les02
Abrianto Nugraha
 
Les18
Les18Les18
Les18
Vijay Kumar
 
Les06
Les06Les06
Les06
Sudharsan S
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
jhe04
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
Thuan Nguyen
 
e computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql pluse computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql plus
ecomputernotes
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
ecomputernotes
 
Les06
Les06Les06
Les06
Sudharsan S
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
oracle content
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
DataminingTools Inc
 
Day1_Structured Query Language2_To understand.ppt
Day1_Structured Query Language2_To understand.pptDay1_Structured Query Language2_To understand.ppt
Day1_Structured Query Language2_To understand.ppt
consravs
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
newrforce
 
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
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
Thuan Nguyen
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
jhe04
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
Thuan Nguyen
 
e computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql pluse computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql plus
ecomputernotes
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
ecomputernotes
 
Ad

More from Achmad Solichin (20)

Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)
Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)
Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)
Achmad Solichin
 
Materi Webinar Web 3.0 (16 Juli 2022)
Materi Webinar Web 3.0 (16 Juli 2022)Materi Webinar Web 3.0 (16 Juli 2022)
Materi Webinar Web 3.0 (16 Juli 2022)
Achmad Solichin
 
Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)
Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)
Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)
Achmad Solichin
 
Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)
Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)
Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)
Achmad Solichin
 
Webinar PHP-ID: Machine Learning dengan PHP
Webinar PHP-ID: Machine Learning dengan PHPWebinar PHP-ID: Machine Learning dengan PHP
Webinar PHP-ID: Machine Learning dengan PHP
Achmad Solichin
 
Webinar Data Mining dengan Rapidminer | Universitas Budi Luhur
Webinar Data Mining dengan Rapidminer | Universitas Budi LuhurWebinar Data Mining dengan Rapidminer | Universitas Budi Luhur
Webinar Data Mining dengan Rapidminer | Universitas Budi Luhur
Achmad Solichin
 
TREN DAN IDE RISET BIDANG DATA MINING TERBARU
TREN DAN IDE RISET BIDANG DATA MINING TERBARUTREN DAN IDE RISET BIDANG DATA MINING TERBARU
TREN DAN IDE RISET BIDANG DATA MINING TERBARU
Achmad Solichin
 
Metodologi Riset: Literature Review
Metodologi Riset: Literature ReviewMetodologi Riset: Literature Review
Metodologi Riset: Literature Review
Achmad Solichin
 
Materi Seminar: Artificial Intelligence dengan PHP
Materi Seminar: Artificial Intelligence dengan PHPMateri Seminar: Artificial Intelligence dengan PHP
Materi Seminar: Artificial Intelligence dengan PHP
Achmad Solichin
 
Percobaan Perpindahan Kalor melalui Konduksi, Konveksi dan Radiasi
Percobaan Perpindahan Kalor melalui Konduksi, Konveksi dan RadiasiPercobaan Perpindahan Kalor melalui Konduksi, Konveksi dan Radiasi
Percobaan Perpindahan Kalor melalui Konduksi, Konveksi dan Radiasi
Achmad Solichin
 
Metodologi Riset: Literature Review
Metodologi Riset: Literature ReviewMetodologi Riset: Literature Review
Metodologi Riset: Literature Review
Achmad Solichin
 
Depth First Search (DFS) pada Graph
Depth First Search (DFS) pada GraphDepth First Search (DFS) pada Graph
Depth First Search (DFS) pada Graph
Achmad Solichin
 
Breadth First Search (BFS) pada Graph
Breadth First Search (BFS) pada GraphBreadth First Search (BFS) pada Graph
Breadth First Search (BFS) pada Graph
Achmad Solichin
 
Binary Search Tree (BST) - Algoritma dan Struktur Data
Binary Search Tree (BST) - Algoritma dan Struktur DataBinary Search Tree (BST) - Algoritma dan Struktur Data
Binary Search Tree (BST) - Algoritma dan Struktur Data
Achmad Solichin
 
Computer Vision di Era Industri 4.0
Computer Vision di Era Industri 4.0Computer Vision di Era Industri 4.0
Computer Vision di Era Industri 4.0
Achmad Solichin
 
Seminar: Become a Reliable Web Programmer
Seminar: Become a Reliable Web ProgrammerSeminar: Become a Reliable Web Programmer
Seminar: Become a Reliable Web Programmer
Achmad Solichin
 
The Big 5: Future IT Trends
The Big 5: Future IT TrendsThe Big 5: Future IT Trends
The Big 5: Future IT Trends
Achmad Solichin
 
Modern PHP Developer
Modern PHP DeveloperModern PHP Developer
Modern PHP Developer
Achmad Solichin
 
Seminar: PHP Developer for Dummies
Seminar: PHP Developer for DummiesSeminar: PHP Developer for Dummies
Seminar: PHP Developer for Dummies
Achmad Solichin
 
Pertemuan 1 - Algoritma dan Struktur Data 1
Pertemuan 1 - Algoritma dan Struktur Data 1Pertemuan 1 - Algoritma dan Struktur Data 1
Pertemuan 1 - Algoritma dan Struktur Data 1
Achmad Solichin
 
Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)
Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)
Kuliah Umum - Tips Publikasi Jurnal SINTA untuk Mahasiswa Galau (6 Agustus 2022)
Achmad Solichin
 
Materi Webinar Web 3.0 (16 Juli 2022)
Materi Webinar Web 3.0 (16 Juli 2022)Materi Webinar Web 3.0 (16 Juli 2022)
Materi Webinar Web 3.0 (16 Juli 2022)
Achmad Solichin
 
Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)
Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)
Webinar: Kesadaran Keamanan Informasi (3 Desember 2021)
Achmad Solichin
 
Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)
Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)
Webinar PHP-ID: Mari Mengenal Logika Fuzzy (Fuzzy Logic)
Achmad Solichin
 
Webinar PHP-ID: Machine Learning dengan PHP
Webinar PHP-ID: Machine Learning dengan PHPWebinar PHP-ID: Machine Learning dengan PHP
Webinar PHP-ID: Machine Learning dengan PHP
Achmad Solichin
 
Webinar Data Mining dengan Rapidminer | Universitas Budi Luhur
Webinar Data Mining dengan Rapidminer | Universitas Budi LuhurWebinar Data Mining dengan Rapidminer | Universitas Budi Luhur
Webinar Data Mining dengan Rapidminer | Universitas Budi Luhur
Achmad Solichin
 
TREN DAN IDE RISET BIDANG DATA MINING TERBARU
TREN DAN IDE RISET BIDANG DATA MINING TERBARUTREN DAN IDE RISET BIDANG DATA MINING TERBARU
TREN DAN IDE RISET BIDANG DATA MINING TERBARU
Achmad Solichin
 
Metodologi Riset: Literature Review
Metodologi Riset: Literature ReviewMetodologi Riset: Literature Review
Metodologi Riset: Literature Review
Achmad Solichin
 
Materi Seminar: Artificial Intelligence dengan PHP
Materi Seminar: Artificial Intelligence dengan PHPMateri Seminar: Artificial Intelligence dengan PHP
Materi Seminar: Artificial Intelligence dengan PHP
Achmad Solichin
 
Percobaan Perpindahan Kalor melalui Konduksi, Konveksi dan Radiasi
Percobaan Perpindahan Kalor melalui Konduksi, Konveksi dan RadiasiPercobaan Perpindahan Kalor melalui Konduksi, Konveksi dan Radiasi
Percobaan Perpindahan Kalor melalui Konduksi, Konveksi dan Radiasi
Achmad Solichin
 
Metodologi Riset: Literature Review
Metodologi Riset: Literature ReviewMetodologi Riset: Literature Review
Metodologi Riset: Literature Review
Achmad Solichin
 
Depth First Search (DFS) pada Graph
Depth First Search (DFS) pada GraphDepth First Search (DFS) pada Graph
Depth First Search (DFS) pada Graph
Achmad Solichin
 
Breadth First Search (BFS) pada Graph
Breadth First Search (BFS) pada GraphBreadth First Search (BFS) pada Graph
Breadth First Search (BFS) pada Graph
Achmad Solichin
 
Binary Search Tree (BST) - Algoritma dan Struktur Data
Binary Search Tree (BST) - Algoritma dan Struktur DataBinary Search Tree (BST) - Algoritma dan Struktur Data
Binary Search Tree (BST) - Algoritma dan Struktur Data
Achmad Solichin
 
Computer Vision di Era Industri 4.0
Computer Vision di Era Industri 4.0Computer Vision di Era Industri 4.0
Computer Vision di Era Industri 4.0
Achmad Solichin
 
Seminar: Become a Reliable Web Programmer
Seminar: Become a Reliable Web ProgrammerSeminar: Become a Reliable Web Programmer
Seminar: Become a Reliable Web Programmer
Achmad Solichin
 
The Big 5: Future IT Trends
The Big 5: Future IT TrendsThe Big 5: Future IT Trends
The Big 5: Future IT Trends
Achmad Solichin
 
Seminar: PHP Developer for Dummies
Seminar: PHP Developer for DummiesSeminar: PHP Developer for Dummies
Seminar: PHP Developer for Dummies
Achmad Solichin
 
Pertemuan 1 - Algoritma dan Struktur Data 1
Pertemuan 1 - Algoritma dan Struktur Data 1Pertemuan 1 - Algoritma dan Struktur Data 1
Pertemuan 1 - Algoritma dan Struktur Data 1
Achmad Solichin
 
Ad

Recently uploaded (20)

Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...
Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...
Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...
Harsh Mishra
 
The GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury Empire
The GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury EmpireThe GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury Empire
The GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury Empire
CACompany
 
Trusted Forex Broker Reviews for Smarter Trading
Trusted Forex Broker Reviews for Smarter TradingTrusted Forex Broker Reviews for Smarter Trading
Trusted Forex Broker Reviews for Smarter Trading
Broker Reviewfx
 
Chapter of corporate finance of capital budegting
Chapter of corporate finance of capital budegtingChapter of corporate finance of capital budegting
Chapter of corporate finance of capital budegting
vanshsharma541544
 
paper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijk
paper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijkpaper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijk
paper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijk
Abodahab
 
Consolidated accounting notes presentation
Consolidated accounting notes presentationConsolidated accounting notes presentation
Consolidated accounting notes presentation
ashforddube14
 
Brown and Black Modern Watercolor Presentation.pptx
Brown and Black Modern Watercolor Presentation.pptxBrown and Black Modern Watercolor Presentation.pptx
Brown and Black Modern Watercolor Presentation.pptx
johncolumnago
 
Defined Benefit Pension Schemes - Regulators Opportunity (1).pdf
Defined Benefit Pension Schemes - Regulators Opportunity (1).pdfDefined Benefit Pension Schemes - Regulators Opportunity (1).pdf
Defined Benefit Pension Schemes - Regulators Opportunity (1).pdf
Henry Tapper
 
Money Matters_ Transforming Your Financial Relationship.pdf
Money Matters_ Transforming Your Financial Relationship.pdfMoney Matters_ Transforming Your Financial Relationship.pdf
Money Matters_ Transforming Your Financial Relationship.pdf
pckhetal
 
Hormones (mid terms) by yhbybhnybhunudr rida.pptx
Hormones (mid terms) by yhbybhnybhunudr rida.pptxHormones (mid terms) by yhbybhnybhunudr rida.pptx
Hormones (mid terms) by yhbybhnybhunudr rida.pptx
yousafmuzammil19
 
George Mankiw Principle of Economics Chapter 27
George Mankiw Principle of Economics Chapter 27George Mankiw Principle of Economics Chapter 27
George Mankiw Principle of Economics Chapter 27
DyandraRenata
 
The Impact of the Abhay Bhutada Foundation in Education
The Impact of the Abhay Bhutada Foundation in EducationThe Impact of the Abhay Bhutada Foundation in Education
The Impact of the Abhay Bhutada Foundation in Education
Harsh Mishra
 
DOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjs
DOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjsDOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjs
DOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjs
opppppppooooo5
 
Rural Livelihood.pptx Rural Development
Rural Livelihood.pptx  Rural DevelopmentRural Livelihood.pptx  Rural Development
Rural Livelihood.pptx Rural Development
Dr. Ravindra Pastor
 
How To Recover Stolen Funds From Online Trading Investment Scam
How To Recover Stolen Funds From Online Trading Investment ScamHow To Recover Stolen Funds From Online Trading Investment Scam
How To Recover Stolen Funds From Online Trading Investment Scam
raymondwilliam1022
 
George Mankiw Principle of Economics Chapter 26
George Mankiw Principle of Economics Chapter 26George Mankiw Principle of Economics Chapter 26
George Mankiw Principle of Economics Chapter 26
DyandraRenata
 
Truxton Capital: Middle Market Quarterly Review - April 2025
Truxton Capital: Middle Market Quarterly Review - April 2025Truxton Capital: Middle Market Quarterly Review - April 2025
Truxton Capital: Middle Market Quarterly Review - April 2025
truxtontrust
 
At Nonabel Disability, we redefine disability support services in Greater Syd...
At Nonabel Disability, we redefine disability support services in Greater Syd...At Nonabel Disability, we redefine disability support services in Greater Syd...
At Nonabel Disability, we redefine disability support services in Greater Syd...
zarishah73a
 
Endodontic CC 67890-98765e43567897652.pptx
Endodontic CC 67890-98765e43567897652.pptxEndodontic CC 67890-98765e43567897652.pptx
Endodontic CC 67890-98765e43567897652.pptx
KhalidLafi2
 
Abhay Bhutada Building Success with Vision and Responsibility
Abhay Bhutada Building Success with Vision and ResponsibilityAbhay Bhutada Building Success with Vision and Responsibility
Abhay Bhutada Building Success with Vision and Responsibility
Harsh Mishra
 
Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...
Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...
Understanding Abhay Bhutada's Salary Milestone and His Commitment to Social P...
Harsh Mishra
 
The GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury Empire
The GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury EmpireThe GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury Empire
The GensoL Scam: How 1,000 Cr EV Dreams Funded Jaggis' Luxury Empire
CACompany
 
Trusted Forex Broker Reviews for Smarter Trading
Trusted Forex Broker Reviews for Smarter TradingTrusted Forex Broker Reviews for Smarter Trading
Trusted Forex Broker Reviews for Smarter Trading
Broker Reviewfx
 
Chapter of corporate finance of capital budegting
Chapter of corporate finance of capital budegtingChapter of corporate finance of capital budegting
Chapter of corporate finance of capital budegting
vanshsharma541544
 
paper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijk
paper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijkpaper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijk
paper8.pdfiy87t6r5e5wsretdryfugihojp[][poipuoiyutyrtersweaserdtfyguhuijk
Abodahab
 
Consolidated accounting notes presentation
Consolidated accounting notes presentationConsolidated accounting notes presentation
Consolidated accounting notes presentation
ashforddube14
 
Brown and Black Modern Watercolor Presentation.pptx
Brown and Black Modern Watercolor Presentation.pptxBrown and Black Modern Watercolor Presentation.pptx
Brown and Black Modern Watercolor Presentation.pptx
johncolumnago
 
Defined Benefit Pension Schemes - Regulators Opportunity (1).pdf
Defined Benefit Pension Schemes - Regulators Opportunity (1).pdfDefined Benefit Pension Schemes - Regulators Opportunity (1).pdf
Defined Benefit Pension Schemes - Regulators Opportunity (1).pdf
Henry Tapper
 
Money Matters_ Transforming Your Financial Relationship.pdf
Money Matters_ Transforming Your Financial Relationship.pdfMoney Matters_ Transforming Your Financial Relationship.pdf
Money Matters_ Transforming Your Financial Relationship.pdf
pckhetal
 
Hormones (mid terms) by yhbybhnybhunudr rida.pptx
Hormones (mid terms) by yhbybhnybhunudr rida.pptxHormones (mid terms) by yhbybhnybhunudr rida.pptx
Hormones (mid terms) by yhbybhnybhunudr rida.pptx
yousafmuzammil19
 
George Mankiw Principle of Economics Chapter 27
George Mankiw Principle of Economics Chapter 27George Mankiw Principle of Economics Chapter 27
George Mankiw Principle of Economics Chapter 27
DyandraRenata
 
The Impact of the Abhay Bhutada Foundation in Education
The Impact of the Abhay Bhutada Foundation in EducationThe Impact of the Abhay Bhutada Foundation in Education
The Impact of the Abhay Bhutada Foundation in Education
Harsh Mishra
 
DOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjs
DOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjsDOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjs
DOC-20250502-WA0001..pdfjejsjjsjsjsjsjsjjsjs
opppppppooooo5
 
Rural Livelihood.pptx Rural Development
Rural Livelihood.pptx  Rural DevelopmentRural Livelihood.pptx  Rural Development
Rural Livelihood.pptx Rural Development
Dr. Ravindra Pastor
 
How To Recover Stolen Funds From Online Trading Investment Scam
How To Recover Stolen Funds From Online Trading Investment ScamHow To Recover Stolen Funds From Online Trading Investment Scam
How To Recover Stolen Funds From Online Trading Investment Scam
raymondwilliam1022
 
George Mankiw Principle of Economics Chapter 26
George Mankiw Principle of Economics Chapter 26George Mankiw Principle of Economics Chapter 26
George Mankiw Principle of Economics Chapter 26
DyandraRenata
 
Truxton Capital: Middle Market Quarterly Review - April 2025
Truxton Capital: Middle Market Quarterly Review - April 2025Truxton Capital: Middle Market Quarterly Review - April 2025
Truxton Capital: Middle Market Quarterly Review - April 2025
truxtontrust
 
At Nonabel Disability, we redefine disability support services in Greater Syd...
At Nonabel Disability, we redefine disability support services in Greater Syd...At Nonabel Disability, we redefine disability support services in Greater Syd...
At Nonabel Disability, we redefine disability support services in Greater Syd...
zarishah73a
 
Endodontic CC 67890-98765e43567897652.pptx
Endodontic CC 67890-98765e43567897652.pptxEndodontic CC 67890-98765e43567897652.pptx
Endodontic CC 67890-98765e43567897652.pptx
KhalidLafi2
 
Abhay Bhutada Building Success with Vision and Responsibility
Abhay Bhutada Building Success with Vision and ResponsibilityAbhay Bhutada Building Success with Vision and Responsibility
Abhay Bhutada Building Success with Vision and Responsibility
Harsh Mishra
 

Les02 (restricting and sorting data)

  • 2. 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 i SQL*Plus to restrict and sort output at run time
  • 3. Limiting Rows Using a Selection “ retrieve all employees in department 90” EMPLOYEES …
  • 4. 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. Using the WHERE Clause SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ;
  • 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-RR. SELECT last_name, job_id, department_id FROM employees WHERE last_name = 'Whalen' ;
  • 7. Comparison Conditions Not equal to <> Between two values (inclusive) BETWEEN ...AND... Match any of a list of values IN(set) Match a character pattern LIKE Is a null value IS NULL Less than < Less than or equal to <= Greater than or equal to >= Greater than > Equal to = Meaning Operator
  • 8. Using Comparison Conditions SELECT last_name, salary FROM employees WHERE salary <= 3000 ;
  • 9. Using the BETWEEN Condition Use the BETWEEN condition to display rows based on a range of values: SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500 ; Lower limit Upper limit
  • 10. Using the IN Condition Use the IN membership condition to test for values in a list: SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201) ;
  • 11. 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. SELECT first_name FROM employees WHERE first_name LIKE 'S%' ;
  • 12. You can combine pattern-matching characters: You can use the ESCAPE identifier to search for the actual % and _ symbols. Using the LIKE Condition SELECT last_name FROM employees WHERE last_name LIKE '_o%' ;
  • 13. Using the NULL Conditions Test for nulls with the IS NULL operator. SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL ;
  • 14. 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 Meaning Operator
  • 15. Using the AND Operator SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >=10000 AND job_id LIKE '%MAN%' ; AND requires both conditions to be true:
  • 16. Using the OR Operator SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 OR job_id LIKE '%MAN%' ; OR requires either condition to be true:
  • 17. Using the NOT Operator SELECT last_name, job_id FROM employees WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;
  • 18. Rules of Precedence You can use parentheses to override rules of precedence. Not equal to 6 NOT logical condition 7 AND logical condition 8 OR logical condition 9 IS [NOT] NULL , LIKE , [NOT] IN 4 [NOT] BETWEEN 5 Comparison conditions 3 Concatenation operator 2 Arithmetic operators 1 Meaning Operator
  • 19. Rules of Precedence SELECT last_name, job_id, salary FROM employees WHERE job_id = 'SA_REP' OR job_id = 'AD_PRES' AND salary > 15000; SELECT last_name, job_id, salary FROM employees WHERE ( job_id = 'SA_REP' OR job_id = 'AD_PRES' ) AND salary > 15000; 1 2
  • 20. 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. 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. Substitution Variables ... salary = ? … … department_id = ? … ... last_name = ? ... I want to query different values.
  • 23. Substitution Variables Use i SQL*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. Using the & Substitution Variable Use a variable prefixed with an ampersand ( & ) to prompt the user for a value: SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num ;
  • 25. Using the & Substitution Variable 101 1 2
  • 26. Character and Date Values with Substitution Variables Use single quotation marks for date and character values: SELECT last_name, department_id, salary*12 FROM employees WHERE job_id = '&job_title' ;
  • 27. 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. Using the && Substitution Variable Use the double ampersand ( && ) if you want to reuse the variable value without prompting the user each time: SELECT employee_id, last_name, job_id, &&column_name FROM employees ORDER BY &column_name ; …
  • 29. Using the i SQL*Plus DEFINE Command Use the i SQL*Plus DEFINE command to create and assign a value to a variable. Use the i SQL*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. Using the VERIFY Command Use the VERIFY command to toggle the display of the substitution variable, both before and after i SQL*Plus replaces substitution variables with values: 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;
  • 31. 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 i SQL*Plus to restrict and sort output at run time SELECT *|{[DISTINCT] column|expression [ alias ],...} FROM table [WHERE condition(s) ] [ORDER BY { column, expr, alias } [ASC|DESC]] ;
  • 32. 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
  • 33.  
  • 34.  
  • 35.  
  • 36.  
  • 37.  
  • 38.  

Editor's Notes

  • #3: Oracle Database 10 g : SQL Fundamentals I 2 - Objectives When retrieving data from the database, you may need to do the following: Restrict the rows of data that are displayed Specify the order in which the rows are displayed This lesson explains the SQL statements that you use to perform these actions.
  • #4: Oracle Database 10 g : SQL Fundamentals I 2 - Limiting Rows Using a Selection In the example in the slide, assume that you want to display all the employees in department 90. The rows with a value of 90 in the DEPARTMENT_ID column are the only ones that are returned. This method of restriction is the basis of the WHERE clause in SQL.
  • #5: Oracle Database 10 g : SQL Fundamentals I 2 - Limiting the Rows That Are Selected You can restrict the rows that are returned from the query by using the WHERE clause. A WHERE clause contains a condition that must be met, and it directly follows the FROM clause. If the condition is true, the row meeting the condition is returned. In the syntax: WHERE restricts the query to rows that meet a condition condition is composed of column names, expressions, constants, and a comparison operator The WHERE clause can compare values in columns, literal values, arithmetic expressions, or functions. It consists of three elements: Column name Comparison condition Column name, constant, or list of values
  • #6: Oracle Database 10 g : SQL Fundamentals I 2 - Using the WHERE Clause In the example, the SELECT statement retrieves the employee ID, name, job ID, and department number of all employees who are in department 90.
  • #7: Oracle Database 10 g : SQL Fundamentals I 2 - Character Strings and Dates Character strings and dates in the WHERE clause must be enclosed in single quotation marks ( &apos;&apos; ). Number constants, however, should not be enclosed in single quotation marks. All character searches are case sensitive. In the following example, no rows are returned because the EMPLOYEES table stores all the last names in mixed case: SELECT last_name, job_id, department_id FROM employees WHERE last_name = &apos;WHALEN&apos;; Oracle databases store dates in an internal numeric format, representing the century, year, month, day, hours, minutes, and seconds. The default date display is DD-MON-RR. Note: For details about the RR format and about changing the default date format, see the lesson titled “ Using Single-Row Functions to Customize Output .”
  • #8: Oracle Database 10 g : SQL Fundamentals I 2 - Comparison Conditions Comparison conditions are used in conditions that compare one expression to another value or expression. They are used in the WHERE clause in the following format: Syntax ... WHERE expr operator value Example ... WHERE hire_date = &apos;01-JAN-95&apos; ... WHERE salary &gt;= 6000 ... WHERE last_name = &apos;Smith&apos; An alias cannot be used in the WHERE clause. Note: The symbols != and ^= can also represent the not equal to condition.
  • #9: Oracle Database 10 g : SQL Fundamentals I 2 - Using Comparison Conditions In the example, the SELECT statement retrieves the last name and salary from the EMPLOYEES table for any employee whose salary is less than or equal to $3,000. Note that there is an explicit value supplied to the WHERE clause. The explicit value of 3000 is compared to the salary value in the SALARY column of the EMPLOYEES table.
  • #10: Oracle Database 10 g : SQL Fundamentals I 2 - Using the BETWEEN Condition You can display rows based on a range of values using the BETWEEN range condition. The range that you specify contains a lower limit and an upper limit. The SELECT statement in the slide returns rows from the EMPLOYEES table for any employee whose salary is between $2,500 and $3,500. Values that are specified with the BETWEEN condition are inclusive. You must specify the lower limit first. You can also use the BETWEEN condition on character values: SELECT last_name FROM employees WHERE last_name BETWEEN &apos;King&apos; AND &apos;Smith&apos;;
  • #11: Oracle Database 10 g : SQL Fundamentals I 2 - Using the IN Condition To test for values in a specified set of values, use the IN condition. The IN condition is also known as the membership condition . The slide example displays employee numbers, last names, salaries, and manager’s employee numbers for all the employees whose manager’s employee number is 100, 101, or 201. The IN condition can be used with any data type. The following example returns a row from the EMPLOYEES table for any employee whose last name is included in the list of names in the WHERE clause: SELECT employee_id, manager_id, department_id FROM employees WHERE last_name IN (&apos;Hartstein&apos;, &apos;Vargas&apos;); If characters or dates are used in the list, they must be enclosed in single quotation marks ( &apos;&apos; ).
  • #12: Oracle Database 10 g : SQL Fundamentals I 2 - Using the LIKE Condition You may not always know the exact value to search for. You can select rows that match a character pattern by using the LIKE condition. The character pattern – matching operation is referred to as a wildcard search. Two symbols can be used to construct the search string. The SELECT statement in the slide returns the employee first name from the EMPLOYEES table for any employee whose first name begins with the letter S . Note the uppercase S . Names beginning with an s are not returned. The LIKE condition can be used as a shortcut for some BETWEEN comparisons. The following example displays the last names and hire dates of all employees who joined between January 1995 and December 1995: SELECT last_name, hire_date FROM employees WHERE hire_date LIKE &apos;%95&apos;;
  • #13: Oracle Database 10 g : SQL Fundamentals I 2 - Combining Wildcard Characters The % and _ symbols can be used in any combination with literal characters. The example in the slide displays the names of all employees whose last names have the letter o as the second character. ESCAPE Option When you need to have an exact match for the actual % and _ characters, use the ESCAPE option. This option specifies what the escape character is. If you want to search for strings that contain SA_ , you can use the following SQL statement: SELECT employee_id, last_name, job_id FROM employees WHERE job_id LIKE &apos;%SA\_%&apos; ESCAPE &apos;&apos;; The ESCAPE option identifies the backslash () as the escape character. In the pattern, the escape character precedes the underscore (_). This causes the Oracle Server to interpret the underscore literally.
  • #14: Oracle Database 10 g : SQL Fundamentals I 2 - Using the NULL Conditions The NULL conditions include the IS NULL condition and the IS NOT NULL condition. The IS NULL condition tests for nulls. A null value means the value is unavailable, unassigned, unknown, or inapplicable. Therefore, you cannot test with = because a null cannot be equal or unequal to any value. The slide example retrieves the last names and managers of all employees who do not have a manager. Here is another example: To display last name, job ID, and commission for all employees who are not entitled to receive a commission, use the following SQL statement: SELECT last_name, job_id, commission_pct FROM employees WHERE commission_pct IS NULL; …
  • #15: Oracle Database 10 g : SQL Fundamentals I 2 - Logical Conditions A logical condition combines the result of two component conditions to produce a single result based on those conditions, or it inverts the result of a single condition. A row is returned only if the overall result of the condition is true. Three logical operators are available in SQL: AND OR NOT All the examples so far have specified only one condition in the WHERE clause. You can use several conditions in one WHERE clause using the AND and OR operators.
  • #16: Oracle Database 10 g : SQL Fundamentals I 2 - Using the AND Operator In the example, both conditions must be true for any record to be selected. Therefore, only employees who have a job title that contains the string ‘MAN’ and earn $10,000 or more are selected. All character searches are case sensitive. No rows are returned if ‘MAN’ is not uppercase. Character strings must be enclosed in quotation marks. AND Truth Table The following table shows the results of combining two expressions with AND :
  • #17: Oracle Database 10 g : SQL Fundamentals I 2 - Using the OR Operator In the example, either condition can be true for any record to be selected. Therefore, any employee who has a job ID that contains the string ‘MAN’ or earns $10,000 or more is selected. OR Truth Table The following table shows the results of combining two expressions with OR :
  • #18: Oracle Database 10 g : SQL Fundamentals I 2 - Using the NOT Operator The slide example displays the last name and job ID of all employees whose job ID is not IT_PROG , ST_CLERK , or SA_REP . NOT Truth Table The following table shows the result of applying the NOT operator to a condition: Note: The NOT operator can also be used with other SQL operators, such as BETWEEN , LIKE , and NULL . ... WHERE job_id NOT IN (&apos;AC_ACCOUNT&apos;, &apos;AD_VP&apos;) ... WHERE salary NOT BETWEEN 10000 AND 15000 ... WHERE last_name NOT LIKE &apos;%A%&apos; ... WHERE commission_pct IS NOT NULL
  • #19: Oracle Database 10 g : SQL Fundamentals I 2 - Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The table lists the default order of precedence. You can override the default order by using parentheses around the expressions that you want to calculate first.
  • #20: Oracle Database 10 g : SQL Fundamentals I 2 - Rules of Precedence (continued) Example of the Precedence of the AND Operator In this example, there are two conditions: The first condition is that the job ID is AD_PRES and the salary is greater than $15,000. The second condition is that the job ID is SA_REP . Therefore, the SELECT statement reads as follows: “ Select the row if an employee is a president and earns more than $15,000, or if the employee is a sales representative.” 2. Example of Using Parentheses In this example, there are two conditions: The first condition is that the job ID is AD_PRES or SA_REP . The second condition is that salary is greater than $15,000. Therefore, the SELECT statement reads as follows: “ Select the row if an employee is a president or a sales representative, and if the employee earns more than $15,000.”
  • #21: Oracle Database 10 g : SQL Fundamentals I 2 - Using the ORDER BY Clause The order of rows that are returned in a query result is undefined. The ORDER BY clause can be used to sort the rows. If you use the ORDER BY clause, it must be the last clause of the SQL statement. You can specify an expression, an alias, or a column position as the sort condition. Syntax SELECT expr FROM table [WHERE condition(s) ] [ORDER BY { column , expr, numeric_position } [ASC|DESC]]; In the syntax: ORDER BY specifies the order in which the retrieved rows are displayed ASC orders the rows in ascending order (this is the default order) DESC orders the rows in descending order If the ORDER BY clause is not used, the sort order is undefined, and the Oracle server may not fetch rows in the same order for the same query twice. Use the ORDER BY clause to display the rows in a specific order.
  • #22: Oracle Database 10 g : SQL Fundamentals I 2 - Default Ordering of Data The default sort order is ascending: Numeric values are displayed with the lowest values first (for example, 1 to 999). Date values are displayed with the earliest value first (for example, 01-JAN-92 before 01-JAN-95). Character values are displayed in alphabetical order (for example, A first and Z last). Null values are displayed last for ascending sequences and first for descending sequences. You can sort by a column that is not in the SELECT list. Examples 1. To reverse the order in which rows are displayed, specify the DESC keyword after the column name in the ORDER BY clause. The slide example sorts the result by the most recently hired employee. 2. You can use a column alias in the ORDER BY clause. The slide example sorts the data by annual salary. 3. You can sort query results by more than one column. The sort limit is the number of columns in the given table. In the ORDER BY clause, specify the columns and separate the column names using commas. If you want to reverse the order of a column, specify DESC after its name.
  • #23: Substitution Variables The examples so far have been hard-coded. In a finished application, the user would trigger the report, and the report would run without further prompting. The range of data would be predetermined by the fixed WHERE clause in the i SQL*Plus script file. Using i SQL*Plus, you can create reports that prompt users to supply their own values to restrict the range of data returned by using substitution variables. You can embed substitution variables in a command file or in a single SQL statement. A variable can be thought of as a container in which the values are temporarily stored. When the statement is run, the value is substituted.
  • #24: Substitution Variables (continued) In i SQL*Plus, you can use single-ampersand ( &amp; ) substitution variables to temporarily store values. You can predefine variables in i SQL*Plus by using the DEFINE command. DEFINE creates and assigns a value to a variable. Examples of Restricted Ranges of Data Reporting figures only for the current quarter or specified date range Reporting on data relevant only to the user requesting the report Displaying personnel only within a given department Other Interactive Effects Interactive effects are not restricted to direct user interaction with the WHERE clause. The same principles can be used to achieve other goals, such as: Obtaining input values from a file rather than from a person Passing values from one SQL statement to another i SQL*Plus does not support validation checks (except for data type) on user input.
  • #25: Single-Ampersand Substitution Variable When running a report, users often want to restrict the data that is returned dynamically. i SQL*Plus provides this flexibility with user variables. Use an ampersand ( &amp; ) to identify each variable in your SQL statement. You do not need to define the value of each variable. The example in the slide creates an i SQL*Plus substitution variable for an employee number. When the statement is executed, i SQL*Plus prompts the user for an employee number and then displays the employee number, last name, salary, and department number for that employee. With the single ampersand, the user is prompted every time the command is executed, if the variable does not exist.
  • #26: Single-Ampersand Substitution Variable (continued) When i SQL*Plus detects that the SQL statement contains an ampersand, you are prompted to enter a value for the substitution variable that is named in the SQL statement. After you enter a value and click the Continue button, the results are displayed in the output area of your i SQL*Plus session.
  • #27: Specifying Character and Date Values with Substitution Variables In a WHERE clause, date and character values must be enclosed in single quotation marks. The same rule applies to the substitution variables. Enclose the variable in single quotation marks within the SQL statement itself. The slide shows a query to retrieve the employee names, department numbers, and annual salaries of all employees based on the job title value of the i SQL*Plus substitution variable.
  • #28: Specifying Column Names, Expressions, and Text Not only can you use the substitution variables in the WHERE clause of a SQL statement, but these variables can also be used to substitute for column names, expressions, or text. Example The slide example displays the employee number, name, job title, and any other column that is specified by the user at run time, from the EMPLOYEES table. For each substitution variable in the SELECT statement, you are prompted to enter a value, and you then click the Continue button to proceed. If you do not enter a value for the substitution variable, you get an error when you execute the preceding statement. Note: A substitution variable can be used anywhere in the SELECT statement, except as the first word entered at the command prompt.
  • #29: Double-Ampersand Substitution Variable You can use the double-ampersand ( &amp;&amp; ) substitution variable if you want to reuse the variable value without prompting the user each time. The user sees the prompt for the value only once. In the example in the slide, the user is asked to give the value for variable column_name only once. The value that is supplied by the user ( department_id ) is used for both display and ordering of data. i SQL*Plus stores the value that is supplied by using the DEFINE command; it uses it again whenever you reference the variable name. After a user variable is in place, you need to use the UNDEFINE command to delete it as follows: UNDEFINE column_name
  • #30: Using the i SQL*Plus DEFINE Command The example shown creates an i SQL*Plus substitution variable for an employee number by using the DEFINE command. At run time, this displays the employee number, name, salary, and department number for that employee. Because the variable is created using the i SQL*Plus DEFINE command, the user is not prompted to enter a value for the employee number. Instead, the defined variable value is automatically substituted in the SELECT statement. The EMPLOYEE_NUM substitution variable is present in the session until the user undefines it or exits the i SQL*Plus session.
  • #31: Using the VERIFY Command To confirm the changes in the SQL statement, use the i SQL*Plus VERIFY command. Setting SET VERIFY ON forces i SQL*Plus to display the text of a command before and after it replaces substitution variables with values. The example in the slide displays the old as well as the new value of the EMPLOYEE_ID column. i SQL*Plus System Variables i SQL*Plus uses various system variables that control the working environment. One of those variables is VERIFY . To obtain a complete list of all system variables, you can issue the SHOW ALL command.
  • #32: Oracle Database 10 g : SQL Fundamentals I 2 - Summary In this lesson, you should have learned about restricting and sorting rows that are returned by the SELECT statement. You should also have learned how to implement various operators and conditions. By using the i SQL*Plus substitution variables, you can add flexibility to your SQL statements. You can query users at run time and enable them to specify criteria.
  • #33: Oracle Database 10 g : SQL Fundamentals I 2 - Practice 2: Overview In this practice, you build more reports, including statements that use the WHERE clause and the ORDER BY clause. You make the SQL statements more reusable and generic by including ampersand substitution.
  • #34: Oracle Database 10 g : SQL Fundamentals I 2 - Practice 2 The HR department needs your assistance with creating some queries. 1. Because of budget issues, the HR department needs a report that displays the last name and salary of employees who earn more than $12,000. Place your SQL statement in a text file named lab_02_01.sql . Run your query. 2. Create a report that displays the last name and department number for employee number 176. 3. The HR department needs to find high-salary and low-salary employees. Modify lab_02_01.sql to display the last name and salary for any employee whose salary is not in the range of $5,000 to $12,000. Place your SQL statement in a text file named lab_02_03.sql .
  • #35: Oracle Database 10 g : SQL Fundamentals I 2 - Practice 2 (continued) 4. Create a report to display the last name, job ID, and start date for the employees with the last names of Matos and Taylor. Order the query in ascending order by start date. 5. Display the last name and department number of all employees in departments 20 or 50 in ascending alphabetical order by name. 6. Modify lab_02_03.sql to display the last name and salary of employees who earn between $5,000 and $12,000 and are in department 20 or 50. Label the columns Employee and Monthly Salary , respectively. Resave lab_02_03.sql as lab_02_06.sql . Run the statement in lab_02_06.sql .
  • #36: Oracle Database 10 g : SQL Fundamentals I 2 - Practice 2 (continued) 7. The HR department needs a report that displays the last name and hire date for all employees who were hired in 1994. 8. Create a report to display the last name and job title of all employees who do not have a manager. 9. Create a report to display the last name, salary, and commission of all employees who earn commissions. Sort data in descending order of salary and commissions. 10. Members of the HR department want to have more flexibility with the queries that you are writing. They would like a report that displays the last name and salary of employees who earn more than an amount that the user specifies after a prompt. (You can use the query that you created in practice exercise 1 and modify it.) Save this query to a file named lab_02_10.sql . If you enter 12000 when prompted, the report displays the following results:
  • #37: Practice 2 (continued) 11. The HR department wants to run reports based on a manager. Create a query that prompts the user for a manager ID and generates the employee ID, last name, salary, and department for that manager’s employees. The HR department wants the ability to sort the report on a selected column. You can test the data with the following values: manager ID = 103, sorted by employee last name: manager ID = 201, sorted by salary: manager ID = 124, sorted by employee ID:
  • #38: Oracle Database 10 g : SQL Fundamentals I 2 - Practice 2 (continued) If you have time, complete the following exercises: 12. Display all employee last names in which the third letter of the name is a . 13. Display the last names of all employees who have both an a and an e in their last name. If you want an extra challenge, complete the following exercises: 14. Display the last name, job, and salary for all employees whose jobs are either sales representative or stock clerk and whose salaries are not equal to $2,500, $3,500, or $7,000. 15. Modify lab_02_06.sql to display the last name, salary, and commission for all employees whose commission amount is 20%. Resave lab_02_06.sql as lab_02_15.sql . Rerun the statement in lab_02_15.sql .