SlideShare a Scribd company logo
1 of 26
2
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




2-2
Limiting Rows Using a Selection
EMP
 EMPNO ENAME      JOB         ...   DEPTNO
                                                "…retrieve all
   7839   KING    PRESIDENT             10        employees
   7698   BLAKE   MANAGER               30    in department 10"
   7782   CLARK   MANAGER               10
   7566   JONES   MANAGER               20
   ...

                        EMP
                         EMPNO ENAME    JOB        ...   DEPTNO

                          7839 KING   PRESIDENT              10
                          7782 CLARK MANAGER                 10
                          7934 MILLER CLERK                  10



 2-3
Limiting Rows Selected

  • Restrict the rows returned by using the
    WHERE clause.

 SELECT      [DISTINCT] {*| column [alias], ...}
 FROM        table
 [WHERE      condition(s)];



  • The WHERE clause follows the FROM
    clause.



2-4
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




2-5
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> SELECT   ename, job, deptno
        2 FROM      emp
        3 WHERE     ename = 'JAMES';




2-6
Comparison Operators

       Operator   Meaning

          =       Equal to

          >       Greater than

          >=      Greater than or equal to

          <       Less than

          <=      Less than or equal to

          <>      Not equal to




2-7
Using the Comparison
               Operators

 SQL> SELECT ename, sal, comm
   2 FROM    emp
   3 WHERE sal<=comm;



 ENAME            SAL      COMM
 ---------- --------- ---------
 MARTIN          1250      1400




2-8
Other Comparison Operators

       Operator    Meaning

       BETWEEN     Between two values (inclusive)
       ...AND...

       IN(list)    Match any of a list of values

       LIKE        Match a character pattern

       IS NULL     Is a null value




2-9
Using the BETWEEN Operator
Use the BETWEEN operator to display
rows based on a range of values.
 SQL> SELECT   ename, sal
   2 FROM      emp
   3 WHERE     sal BETWEEN 1000 AND 1500;

 ENAME            SAL
 ---------- ---------     Lower    Higher
 MARTIN          1250      limit    limit
 TURNER          1500
 WARD            1250
 ADAMS           1100
 MILLER          1300



2-10
Using the IN Operator
  Use the IN operator to test for values in a
  list.

 SQL> SELECT    empno, ename, sal, mgr
   2 FROM       emp
   3 WHERE      mgr IN (7902, 7566, 7788);


     EMPNO   ENAME            SAL       MGR
 ---------   ---------- --------- ---------
      7902   FORD            3000      7566
      7369   SMITH            800      7902
      7788   SCOTT           3000      7566
      7876   ADAMS           1100      7788


2-11
Using the LIKE Operator
• Use the LIKE operator to perform
  wildcard searches of valid search string
  values.
• Search conditions can contain either
  literal characters or numbers.
       – % denotes zero or many characters.
       – _ denotes one character.

  SQL> SELECT   ename
    2 FROM      emp
    3 WHERE     ename LIKE 'S%';


2-12
Using the LIKE Operator

 • You can combine pattern-matching
   characters.
  SQL> SELECT   ename
    2 FROM      emp
    3 WHERE     ename LIKE '_A%';

   ENAME
   ----------
   MARTIN
   JAMES
   WARD

 • You can use the ESCAPE identifier to
   search for "%" or "_".
2-13
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




2-14
Logical Operators

       Operator      Meaning

       AND           Returns TRUE if both component
                     conditions are TRUE
       OR            Returns TRUE if either component
                     condition is TRUE

       NOT           Returns TRUE if the following
                     condition is FALSE




2-15
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




2-16
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.
2-17
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




2-18
Rules of Precedence

  Order Evaluated   Operator
        1           All comparison
                    operators
        2           NOT
        3           AND
        4           OR

 Override rules of precedence by using
 parentheses.

2-19
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



2-20
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




2-21
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            20 17-DEC-80
ALLEN      SALESMAN         30 20-FEB-81
...
14 rows selected.
2-22
Sorting in Descending Order
 SQL> SELECT  ename, job, deptno, hiredate
   2 FROM     emp
   3 ORDER BY hiredate DESC;


 ENAME      JOB          DEPTNO HIREDATE
 ---------- --------- --------- ---------
 ADAMS      CLERK            20 12-JAN-83
 SCOTT      ANALYST          20 09-DEC-82
 MILLER     CLERK            10 23-JAN-82
 JAMES      CLERK            30 03-DEC-81
 FORD       ANALYST          20 03-DEC-81
 KING       PRESIDENT        10 17-NOV-81
 MARTIN     SALESMAN         30 28-SEP-81
 ...
 14 rows selected.

2-23
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.

2-24
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              10      5000
   CLARK             10      2450
   MILLER            10      1300
   FORD              20      3000
   ...
   14 rows selected.

• You can sort by a column that is not in the
  SELECT list.
2-25
Summary

 SELECT      [DISTINCT] {*| column [alias], ...}
 FROM        table
 [WHERE      condition(s)]
 [ORDER BY   {column, expr, alias} [ASC|DESC]];




2-26

More Related Content

What's hot (20)

Les04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple TableLes04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple Table
 
Les01
Les01Les01
Les01
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
Les01
Les01Les01
Les01
 
Les12 creating views
Les12 creating viewsLes12 creating views
Les12 creating views
 
Les03
Les03Les03
Les03
 
Sqlplus
SqlplusSqlplus
Sqlplus
 
Basic sql statements
Basic sql statementsBasic sql statements
Basic sql statements
 
Sql statments c ha p# 1
Sql statments c ha p# 1Sql statments c ha p# 1
Sql statments c ha p# 1
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
 
Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 
COIS 420 - Practice01
COIS 420 - Practice01COIS 420 - Practice01
COIS 420 - Practice01
 
Les00 Intoduction
Les00 IntoductionLes00 Intoduction
Les00 Intoduction
 
Les06[1]Subqueries
Les06[1]SubqueriesLes06[1]Subqueries
Les06[1]Subqueries
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12
 
Analytic SQL Sep 2013
Analytic SQL Sep 2013Analytic SQL Sep 2013
Analytic SQL Sep 2013
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
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
 
SQL Tuning 101 - Sep 2013
SQL Tuning 101 - Sep 2013SQL Tuning 101 - Sep 2013
SQL Tuning 101 - Sep 2013
 

Similar to COIS 420 - Practice02

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
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchiesConnor McDonald
 
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
 
Les01-Oracle
Les01-OracleLes01-Oracle
Les01-Oraclesuman1248
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with outputNexus
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQLConnor McDonald
 
CHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptxCHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptxMuhammadSheraz836877
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developersInSync Conference
 
Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Sanjay Pathak
 
Sql scripting sorcerypresentation
Sql scripting sorcerypresentationSql scripting sorcerypresentation
Sql scripting sorcerypresentationoracle documents
 
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
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sqlSushil Mishra
 
COIS 420 - Practice04
COIS 420 - Practice04COIS 420 - Practice04
COIS 420 - Practice04Angel G Diaz
 
SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1Umair Amjad
 
SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4Umair Amjad
 
12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQL12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQLConnor McDonald
 

Similar to COIS 420 - Practice02 (20)

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
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
 
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
 
Les01-Oracle
Les01-OracleLes01-Oracle
Les01-Oracle
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQL
 
CHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptxCHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptx
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developers
 
11 things about 11gr2
11 things about 11gr211 things about 11gr2
11 things about 11gr2
 
Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)Assignment 2 (16-08-2013)
Assignment 2 (16-08-2013)
 
Sql scripting sorcerypresentation
Sql scripting sorcerypresentationSql scripting sorcerypresentation
Sql scripting sorcerypresentation
 
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
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
 
COIS 420 - Practice04
COIS 420 - Practice04COIS 420 - Practice04
COIS 420 - Practice04
 
SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1SQL WORKSHOP::Lecture 1
SQL WORKSHOP::Lecture 1
 
SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4
 
12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQL12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQL
 

More from Angel G Diaz

SQL Developer installation-guide
SQL Developer installation-guideSQL Developer installation-guide
SQL Developer installation-guideAngel G Diaz
 
Lesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementLesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementAngel G Diaz
 
Lesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQLLesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQLAngel G Diaz
 
Lesson 01 - Network Assessment
Lesson 01 - Network AssessmentLesson 01 - Network Assessment
Lesson 01 - Network AssessmentAngel G Diaz
 
OracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper InstallOracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper InstallAngel G Diaz
 
GNS3 Simulator Installation
GNS3 Simulator InstallationGNS3 Simulator Installation
GNS3 Simulator InstallationAngel G Diaz
 
WAMP & Joomla! Installation
WAMP & Joomla! InstallationWAMP & Joomla! Installation
WAMP & Joomla! InstallationAngel G Diaz
 
COIS 420 - Practice 03
COIS 420 - Practice 03COIS 420 - Practice 03
COIS 420 - Practice 03Angel G Diaz
 
Lesson02 - Network Design & LAN
Lesson02 - Network Design & LANLesson02 - Network Design & LAN
Lesson02 - Network Design & LANAngel G Diaz
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlAngel G Diaz
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlAngel G Diaz
 
Making Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and CompressionMaking Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and CompressionAngel G Diaz
 
Making Connections
Making ConnectionsMaking Connections
Making ConnectionsAngel G Diaz
 
Conducted and Wireless Media
Conducted and Wireless MediaConducted and Wireless Media
Conducted and Wireless MediaAngel G Diaz
 
Fundamentals of Data and Signals
Fundamentals of Data and SignalsFundamentals of Data and Signals
Fundamentals of Data and SignalsAngel G Diaz
 
Introduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data CommunicationsIntroduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data CommunicationsAngel G Diaz
 

More from Angel G Diaz (17)

SQL Developer installation-guide
SQL Developer installation-guideSQL Developer installation-guide
SQL Developer installation-guide
 
Lesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementLesson - 02 Network Design and Management
Lesson - 02 Network Design and Management
 
Lesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQLLesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQL
 
Lesson 01 - Network Assessment
Lesson 01 - Network AssessmentLesson 01 - Network Assessment
Lesson 01 - Network Assessment
 
OracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper InstallOracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper Install
 
GNS3 Simulator Installation
GNS3 Simulator InstallationGNS3 Simulator Installation
GNS3 Simulator Installation
 
WAMP & Joomla! Installation
WAMP & Joomla! InstallationWAMP & Joomla! Installation
WAMP & Joomla! Installation
 
COIS 420 - Practice 03
COIS 420 - Practice 03COIS 420 - Practice 03
COIS 420 - Practice 03
 
Lesson02 - Network Design & LAN
Lesson02 - Network Design & LANLesson02 - Network Design & LAN
Lesson02 - Network Design & LAN
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error Control
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error Control
 
Making Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and CompressionMaking Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and Compression
 
Making Connections
Making ConnectionsMaking Connections
Making Connections
 
Conducted and Wireless Media
Conducted and Wireless MediaConducted and Wireless Media
Conducted and Wireless Media
 
Fundamentals of Data and Signals
Fundamentals of Data and SignalsFundamentals of Data and Signals
Fundamentals of Data and Signals
 
Introduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data CommunicationsIntroduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data Communications
 
Cois240 lesson01
Cois240 lesson01Cois240 lesson01
Cois240 lesson01
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

COIS 420 - Practice02

  • 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 2-2
  • 3. Limiting Rows Using a Selection EMP EMPNO ENAME JOB ... DEPTNO "…retrieve all 7839 KING PRESIDENT 10 employees 7698 BLAKE MANAGER 30 in department 10" 7782 CLARK MANAGER 10 7566 JONES MANAGER 20 ... EMP EMPNO ENAME JOB ... DEPTNO 7839 KING PRESIDENT 10 7782 CLARK MANAGER 10 7934 MILLER CLERK 10 2-3
  • 4. Limiting Rows Selected • Restrict the rows returned by using the WHERE clause. SELECT [DISTINCT] {*| column [alias], ...} FROM table [WHERE condition(s)]; • The WHERE clause follows the FROM clause. 2-4
  • 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 2-5
  • 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> SELECT ename, job, deptno 2 FROM emp 3 WHERE ename = 'JAMES'; 2-6
  • 7. Comparison Operators Operator Meaning = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to 2-7
  • 8. Using the Comparison Operators SQL> SELECT ename, sal, comm 2 FROM emp 3 WHERE sal<=comm; ENAME SAL COMM ---------- --------- --------- MARTIN 1250 1400 2-8
  • 9. Other Comparison Operators Operator Meaning BETWEEN Between two values (inclusive) ...AND... IN(list) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value 2-9
  • 10. Using the BETWEEN Operator Use the BETWEEN operator to display rows based on a range of values. SQL> SELECT ename, sal 2 FROM emp 3 WHERE sal BETWEEN 1000 AND 1500; ENAME SAL ---------- --------- Lower Higher MARTIN 1250 limit limit TURNER 1500 WARD 1250 ADAMS 1100 MILLER 1300 2-10
  • 11. Using the IN Operator Use the IN operator to test for values in a list. SQL> SELECT empno, ename, sal, mgr 2 FROM emp 3 WHERE mgr IN (7902, 7566, 7788); EMPNO ENAME SAL MGR --------- ---------- --------- --------- 7902 FORD 3000 7566 7369 SMITH 800 7902 7788 SCOTT 3000 7566 7876 ADAMS 1100 7788 2-11
  • 12. Using the LIKE Operator • Use the LIKE operator to perform wildcard searches of valid search string values. • Search conditions can contain either literal characters or numbers. – % denotes zero or many characters. – _ denotes one character. SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE 'S%'; 2-12
  • 13. Using the LIKE Operator • You can combine pattern-matching characters. SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE '_A%'; ENAME ---------- MARTIN JAMES WARD • You can use the ESCAPE identifier to search for "%" or "_". 2-13
  • 14. 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 2-14
  • 15. Logical Operators Operator Meaning AND Returns TRUE if both component conditions are TRUE OR Returns TRUE if either component condition is TRUE NOT Returns TRUE if the following condition is FALSE 2-15
  • 16. 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 2-16
  • 17. 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. 2-17
  • 18. 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 2-18
  • 19. Rules of Precedence Order Evaluated Operator 1 All comparison operators 2 NOT 3 AND 4 OR Override rules of precedence by using parentheses. 2-19
  • 20. 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 2-20
  • 21. 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 2-21
  • 22. ORDER BY Clause • Sort rows with the ORDER BY clause – ASC: ascending order, default – DESC: descending order • The ORDER BY clause comes last in the SELECT statement. SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate; ENAME JOB DEPTNO HIREDATE ---------- --------- --------- --------- SMITH CLERK 20 17-DEC-80 ALLEN SALESMAN 30 20-FEB-81 ... 14 rows selected. 2-22
  • 23. Sorting in Descending Order SQL> SELECT ename, job, deptno, hiredate 2 FROM emp 3 ORDER BY hiredate DESC; ENAME JOB DEPTNO HIREDATE ---------- --------- --------- --------- ADAMS CLERK 20 12-JAN-83 SCOTT ANALYST 20 09-DEC-82 MILLER CLERK 10 23-JAN-82 JAMES CLERK 30 03-DEC-81 FORD ANALYST 20 03-DEC-81 KING PRESIDENT 10 17-NOV-81 MARTIN SALESMAN 30 28-SEP-81 ... 14 rows selected. 2-23
  • 24. 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. 2-24
  • 25. 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 10 5000 CLARK 10 2450 MILLER 10 1300 FORD 20 3000 ... 14 rows selected. • You can sort by a column that is not in the SELECT list. 2-25
  • 26. Summary SELECT [DISTINCT] {*| column [alias], ...} FROM table [WHERE condition(s)] [ORDER BY {column, expr, alias} [ASC|DESC]]; 2-26