SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
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
 
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
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 

Kürzlich hochgeladen (20)

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 

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