SlideShare une entreprise Scribd logo
1  sur  28
Restricting and Sorting Data
Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query
Limiting Rows Using a Selection EMP  EMPNO ENAME JOB		 ...  DEPTNO      7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20   ... "…retrieve allemployeesin department 10" EMP  EMPNO ENAME JOB		 ...  DEPTNO      7839KINGPRESIDENT10 7782CLARKMANAGER10 7934MILLERCLERK10
Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT		[DISTINCT] {*| column [alias], ...} FROM table [WHEREcondition(s)];
Using the WHERE Clause SQL> SELECT ename, job, deptno 2  FROM   emp 3  WHERE  job='CLERK'; ENAME      JOB          DEPTNO ---------- --------- --------- JAMES      CLERK            30 SMITH      CLERK            20 ADAMS      CLERK            20 MILLER     CLERK            10
Character Strings and Dates Character strings and date values are enclosed in single quotation marks. Character values are case sensitive and date values are format sensitive. The default date format is DD-MON-YY. SQL> SELECTename, job, deptno 2  FROM emp 3  WHEREename = 'JAMES';
Comparison Operators Operator = >       >=	 <       <=	 <> Meaning Equal to Greater than  Greater than or equal to  Less than  Less than or equal to Not equal to
Using the Comparison Operators SQL> SELECT ename, sal, comm 2  FROM   emp 3  WHERE  sal<=comm; ENAME            SAL      COMM ---------- --------- --------- MARTIN          12501400
Other Comparison Operators Operator BETWEEN...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive)	 Match any of a list of values  Match a character pattern  Is a null value
Using the BETWEEN Operator Lowerlimit Higherlimit Use the BETWEEN operator to display rows based on a range of values. SQL> SELECTename, sal 2  FROM emp 3  WHEREsal BETWEEN 1000 AND 1500; ENAME            SAL ---------- --------- MARTIN          1250 TURNER          1500 WARD            1250 ADAMS           1100 MILLER          1300
Using the IN Operator Use the IN operator to test for values in a list. SQL> SELECTempno, ename, sal, mgr 2  FROM emp 3  WHEREmgr IN (7902, 7566, 7788);     EMPNO ENAME            SAL       MGR --------- ---------- --------- --------- 7902 FORD            30007566 7369 SMITH            8007902 7788 SCOTT           30007566 7876 ADAMS           11007788
Using the LIKE Operator ,[object Object]
Search conditions can contain either literal characters or numbers.
% denotes zero or many characters.
 _ denotes one character.SQL> SELECTename 2  FROM emp 3  WHEREename LIKE 'S%';
Using the LIKE Operator You can combine pattern-matching characters. You can use the ESCAPE identifier to search for "%" or "_". SQL> SELECTename 2  FROMemp 3  WHEREename LIKE '_A%'; ENAME ----------  MARTIN JAMES    WARD
Using the IS NULL Operator Test for null values with the IS NULL operator. SQL> SELECT  ename, mgr 2  FROM    emp 3  WHERE   mgr IS NULL; ENAME            MGR ---------- --------- KING
Logical Operators Operator ANDOR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns TRUE if the following  condition is FALSE
Using the AND Operator AND requires both conditions to be TRUE. SQL> SELECT empno, ename, job, sal 2  FROM   emp 3  WHERE  sal>=1100 4  AND    job='CLERK';     EMPNO ENAME      JOB             SAL --------- ---------- --------- --------- 7876 ADAMS      CLERK          1100 7934 MILLER     CLERK          1300
Using the OR Operator OR requires either condition to be TRUE. SQL> SELECT empno, ename, job, sal 2  FROM   emp 3  WHERE  sal>=1100 4  OR     job='CLERK';     EMPNO ENAME      JOB             SAL --------- ---------- --------- --------- 7839 KING       PRESIDENT      5000 7698 BLAKE      MANAGER        2850 7782 CLARK      MANAGER        2450 7566 JONES      MANAGER        2975 7654 MARTIN     SALESMAN       1250      ...  7900 JAMES      CLERK           950      ... 14 rows selected.
Using the NOT Operator SQL> SELECT ename, job 2  FROM   emp 3  WHERE  job NOT IN ('CLERK','MANAGER','ANALYST'); ENAME      JOB ---------- --------- KING       PRESIDENT MARTIN     SALESMAN ALLEN      SALESMAN TURNER     SALESMAN WARD       SALESMAN
Rules of Precedence Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR Override rules of precedence by using parentheses.
Rules of Precedence SQL> SELECT ename, job, sal 2  FROM   emp 3  WHERE  job='SALESMAN' 4  OR     job='PRESIDENT' 5  AND    sal>1500; ENAME      JOB             SAL ---------- --------- --------- KING       PRESIDENT      5000 MARTIN     SALESMAN       1250 ALLEN      SALESMAN       1600 TURNER     SALESMAN       1500 WARD       SALESMAN       1250
Rules of Precedence Use parentheses to force priority. SQL> SELECT   ename, job, sal 2  FROM     emp 3  WHERE    (job='SALESMAN' 4  OR       job='PRESIDENT') 5  AND      sal>1500; ENAME      JOB             SAL ---------- --------- --------- KING       PRESIDENT      5000 ALLEN      SALESMAN       1600
ORDER BY Clause Sort rows with the ORDER BY clause ASC: ascending order, default DESC: descending order The ORDER BY clause comes last in the SELECT statement. SQL> SELECT   ename, job, deptno, hiredate 2  FROM     emp 3  ORDER BY hiredate; ENAME      JOB          DEPTNO HIREDATE ---------- --------- --------- --------- SMITH      CLERK            2017-DEC-80 ALLEN      SALESMAN         3020-FEB-81 ... 14 rows selected.
Sorting in Descending Order SQL> SELECT   ename, job, deptno, hiredate 2  FROM     emp 3  ORDER BY hiredate DESC; ENAME      JOB          DEPTNO HIREDATE ---------- --------- --------- --------- ADAMS      CLERK            2012-JAN-83 SCOTT      ANALYST          2009-DEC-82 MILLER     CLERK            1023-JAN-82 JAMES      CLERK            3003-DEC-81 FORD       ANALYST          2003-DEC-81 KING       PRESIDENT        1017-NOV-81 MARTIN     SALESMAN         3028-SEP-81 ... 14 rows selected.
Sorting by Column Alias SQL> SELECT   empno, ename, sal*12 annsal 2  FROM     emp 3  ORDER BY annsal;     EMPNO ENAME         ANNSAL --------- ---------- --------- 7369 SMITH           9600 7900 JAMES          11400 7876 ADAMS          13200 7654 MARTIN         15000 7521 WARD           15000 7934 MILLER         15600 7844 TURNER         18000 ... 14 rows selected.
Sorting by Multiple Columns The order of ORDER BY list is the order of sort. SQL> SELECT ename, deptno, sal 2  FROM  emp 3  ORDER BY deptno, sal DESC; ENAME         DEPTNO       SAL ---------- --------- --------- KING              105000 CLARK             102450 MILLER            101300 FORD              203000 ... 14 rows selected. ,[object Object],[object Object]

Contenu connexe

Tendances (20)

Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Les00 Intoduction
Les00 IntoductionLes00 Intoduction
Les00 Intoduction
 
Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 
Les01
Les01Les01
Les01
 
Les12
Les12Les12
Les12
 
Les02
Les02Les02
Les02
 
Les11
Les11Les11
Les11
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12
 
Les10
Les10Les10
Les10
 
Les09
Les09Les09
Les09
 
COIS 420 - Practice02
COIS 420 - Practice02COIS 420 - Practice02
COIS 420 - Practice02
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
 
SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5SQL WORKSHOP::Lecture 5
SQL WORKSHOP::Lecture 5
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
 
Trig
TrigTrig
Trig
 

En vedette

En vedette (10)

Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3
 
Dowry system in india
Dowry system in indiaDowry system in india
Dowry system in india
 
A Question of Dowry
A Question of DowryA Question of Dowry
A Question of Dowry
 
Dowry as a social problem
Dowry as a social problemDowry as a social problem
Dowry as a social problem
 
Dowry
DowryDowry
Dowry
 
Dowry system
Dowry systemDowry system
Dowry system
 
Oracle Basics and Architecture
Oracle Basics and ArchitectureOracle Basics and Architecture
Oracle Basics and Architecture
 
Dowry system
Dowry systemDowry system
Dowry system
 
Dowry System .ppt
Dowry System .pptDowry System .ppt
Dowry System .ppt
 

Similaire à Les02 Restricting And Sorting Data

Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Datasiavosh kaviani
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data HuzaifaMushtaq3
 
Les01-Oracle
Les01-OracleLes01-Oracle
Les01-Oraclesuman1248
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchiesConnor McDonald
 
Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Sanjay Pathak
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQLConnor McDonald
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with outputNexus
 
12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntaxConnor McDonald
 
Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017Connor McDonald
 
Sql statments c ha p# 1
Sql statments c ha p# 1Sql statments c ha p# 1
Sql statments c ha p# 1Nargis Ehsan
 
Get your moneys worth out of your database
Get your moneys worth out of your databaseGet your moneys worth out of your database
Get your moneys worth out of your databasePatrick Barel
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sqlSushil Mishra
 
SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4Umair Amjad
 

Similaire à Les02 Restricting And Sorting Data (20)

Sql2
Sql2Sql2
Sql2
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
 
chap2 (3).ppt
chap2 (3).pptchap2 (3).ppt
chap2 (3).ppt
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
 
Les01-Oracle
Les01-OracleLes01-Oracle
Les01-Oracle
 
Analytic SQL Sep 2013
Analytic SQL Sep 2013Analytic SQL Sep 2013
Analytic SQL Sep 2013
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
 
Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQL
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax
 
Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017Analytic functions in Oracle SQL - BIWA 2017
Analytic functions in Oracle SQL - BIWA 2017
 
Sql statments c ha p# 1
Sql statments c ha p# 1Sql statments c ha p# 1
Sql statments c ha p# 1
 
Sqlplus
SqlplusSqlplus
Sqlplus
 
Get your moneys worth out of your database
Get your moneys worth out of your databaseGet your moneys worth out of your database
Get your moneys worth out of your database
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
 
SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 

Dernier

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Dernier (20)

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

Les02 Restricting And Sorting Data

  • 2. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query
  • 3. Limiting Rows Using a Selection EMP EMPNO ENAME JOB ... DEPTNO 7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20 ... "…retrieve allemployeesin department 10" EMP EMPNO ENAME JOB ... DEPTNO 7839KINGPRESIDENT10 7782CLARKMANAGER10 7934MILLERCLERK10
  • 4. Limiting Rows Selected Restrict the rows returned by using the WHERE clause. The WHERE clause follows the FROM clause. SELECT [DISTINCT] {*| column [alias], ...} FROM table [WHEREcondition(s)];
  • 5. Using the WHERE Clause SQL> SELECT ename, job, deptno 2 FROM emp 3 WHERE job='CLERK'; ENAME JOB DEPTNO ---------- --------- --------- JAMES CLERK 30 SMITH CLERK 20 ADAMS CLERK 20 MILLER CLERK 10
  • 6. Character Strings and Dates Character strings and date values are enclosed in single quotation marks. Character values are case sensitive and date values are format sensitive. The default date format is DD-MON-YY. SQL> SELECTename, job, deptno 2 FROM emp 3 WHEREename = 'JAMES';
  • 7. Comparison Operators Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
  • 8. Using the Comparison Operators SQL> SELECT ename, sal, comm 2 FROM emp 3 WHERE sal<=comm; ENAME SAL COMM ---------- --------- --------- MARTIN 12501400
  • 9. Other Comparison Operators Operator BETWEEN...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive) Match any of a list of values Match a character pattern Is a null value
  • 10. Using the BETWEEN Operator Lowerlimit Higherlimit Use the BETWEEN operator to display rows based on a range of values. SQL> SELECTename, sal 2 FROM emp 3 WHEREsal BETWEEN 1000 AND 1500; ENAME SAL ---------- --------- MARTIN 1250 TURNER 1500 WARD 1250 ADAMS 1100 MILLER 1300
  • 11. Using the IN Operator Use the IN operator to test for values in a list. SQL> SELECTempno, ename, sal, mgr 2 FROM emp 3 WHEREmgr IN (7902, 7566, 7788); EMPNO ENAME SAL MGR --------- ---------- --------- --------- 7902 FORD 30007566 7369 SMITH 8007902 7788 SCOTT 30007566 7876 ADAMS 11007788
  • 12.
  • 13. Search conditions can contain either literal characters or numbers.
  • 14. % denotes zero or many characters.
  • 15. _ denotes one character.SQL> SELECTename 2 FROM emp 3 WHEREename LIKE 'S%';
  • 16. Using the LIKE Operator You can combine pattern-matching characters. You can use the ESCAPE identifier to search for "%" or "_". SQL> SELECTename 2 FROMemp 3 WHEREename LIKE '_A%'; ENAME ---------- MARTIN JAMES WARD
  • 17. Using the IS NULL Operator Test for null values with the IS NULL operator. SQL> SELECT ename, mgr 2 FROM emp 3 WHERE mgr IS NULL; ENAME MGR ---------- --------- KING
  • 18. Logical Operators Operator ANDOR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns TRUE if the following condition is FALSE
  • 19. Using the AND Operator AND requires both conditions to be TRUE. SQL> SELECT empno, ename, job, sal 2 FROM emp 3 WHERE sal>=1100 4 AND job='CLERK'; EMPNO ENAME JOB SAL --------- ---------- --------- --------- 7876 ADAMS CLERK 1100 7934 MILLER CLERK 1300
  • 20. Using the OR Operator OR requires either condition to be TRUE. SQL> SELECT empno, ename, job, sal 2 FROM emp 3 WHERE sal>=1100 4 OR job='CLERK'; EMPNO ENAME JOB SAL --------- ---------- --------- --------- 7839 KING PRESIDENT 5000 7698 BLAKE MANAGER 2850 7782 CLARK MANAGER 2450 7566 JONES MANAGER 2975 7654 MARTIN SALESMAN 1250 ... 7900 JAMES CLERK 950 ... 14 rows selected.
  • 21. Using the NOT Operator SQL> SELECT ename, job 2 FROM emp 3 WHERE job NOT IN ('CLERK','MANAGER','ANALYST'); ENAME JOB ---------- --------- KING PRESIDENT MARTIN SALESMAN ALLEN SALESMAN TURNER SALESMAN WARD SALESMAN
  • 22. Rules of Precedence Order EvaluatedOperator 1All comparison operators 2NOT 3AND 4OR Override rules of precedence by using parentheses.
  • 23. Rules of Precedence SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE job='SALESMAN' 4 OR job='PRESIDENT' 5 AND sal>1500; ENAME JOB SAL ---------- --------- --------- KING PRESIDENT 5000 MARTIN SALESMAN 1250 ALLEN SALESMAN 1600 TURNER SALESMAN 1500 WARD SALESMAN 1250
  • 24. Rules of Precedence Use parentheses to force priority. SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE (job='SALESMAN' 4 OR job='PRESIDENT') 5 AND sal>1500; ENAME JOB SAL ---------- --------- --------- KING PRESIDENT 5000 ALLEN SALESMAN 1600
  • 25. ORDER BY Clause Sort rows with the ORDER BY clause ASC: ascending order, default DESC: descending order The ORDER BY clause comes last in the SELECT statement. SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate; ENAME JOB DEPTNO HIREDATE ---------- --------- --------- --------- SMITH CLERK 2017-DEC-80 ALLEN SALESMAN 3020-FEB-81 ... 14 rows selected.
  • 26. Sorting in Descending Order SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate DESC; ENAME JOB DEPTNO HIREDATE ---------- --------- --------- --------- ADAMS CLERK 2012-JAN-83 SCOTT ANALYST 2009-DEC-82 MILLER CLERK 1023-JAN-82 JAMES CLERK 3003-DEC-81 FORD ANALYST 2003-DEC-81 KING PRESIDENT 1017-NOV-81 MARTIN SALESMAN 3028-SEP-81 ... 14 rows selected.
  • 27. Sorting by Column Alias SQL> SELECT empno, ename, sal*12 annsal 2 FROM emp 3 ORDER BY annsal; EMPNO ENAME ANNSAL --------- ---------- --------- 7369 SMITH 9600 7900 JAMES 11400 7876 ADAMS 13200 7654 MARTIN 15000 7521 WARD 15000 7934 MILLER 15600 7844 TURNER 18000 ... 14 rows selected.
  • 28.
  • 29. Practice Overview Selecting data and changing the order of rows displayed Restricting rows by using the WHERE clause Using the double quotation marks in column aliases