SlideShare a Scribd company logo
1 of 38
Download to read offline
Oracle OCP 考试系列培训
                之
         1Z0-007 Lesson2
                  Lesson2
                  www.OracleOnLinux.cn




2-1   Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
2
         Restricting and Sorting Data




2-2   Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Objectives


      After completing this lesson, you should be able to do
      the following:
       • Limit the rows that are retrieved by a query
       • Sort the rows that are retrieved by a query




2-3          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Limiting Rows Using a Selection


      EMPLOYEES




       …

            “retrieve all
            employees in
            department 90”90”




2-4         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Limiting the Rows That Are Selected


      •   Restrict the rows that are returned by using the
          WHERE clause:
      SELECT *|{[DISTINCT] column|expression [alias],...}
      FROM   table
      [WHERE condition(s)];

      •   The WHERE clause follows the FROM clause.
                                            clause.




2-5         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the WHERE Clause


      SELECT employee_id, last_name, job_id, department_id
      FROM   employees
      WHERE department_id = 90 ;




2-6         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Character Strings and Dates


      •   Character strings and date values are enclosed by
          single quotation marks.
      •   Character values are case-sensitive , and date
                               case-sensitive,
          values are format-sensitive .
                     format-sensitive.
      •   The default date format is DD-MON-RR.

      SELECT last_name, job_id, department_id
      FROM   employees
      WHERE last_name = 'Whalen' ;




2-7         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Comparison Operators

         Operator         Meaning
                 =        Equal to
                 >        Greater than
                >=        Greater than or equal to
                 <        Less than
                <=        Less than or equal to
                <>        Not equal to
         BETWEEN          Between two values (inclusive)
         ...AND...

         IN(set)          Match any of a list of values
         LIKE             Match a character pattern
         IS NULL          Is a null value



2-8   Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using Comparison Conditions


      SELECT    last_name, salary
      FROM      employees
      WHERE     salary <= 3000 ;




2-9            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the BETWEEN Condition


       Use the BETWEEN condition to display rows based on a
       range of values:
       SELECT    last_name, salary
       FROM      employees
       WHERE     salary BETWEEN 2500 AND 3500 ;


                                 Lower limit        Upper limit




2-10            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the IN Condition


       Use the IN membership condition to test for values in
       a list:
       SELECT employee_id, last_name, salary, manager_id
       FROM   employees
       WHERE manager_id IN (100, 101, 201) ;




2-11          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the LIKE Condition


       •   Use the LIKE condition to perform wildcard
           searches of valid search string values.
       •   Search conditions can contain either literal
           characters or numbers:
           – % denotes zero or many characters.
           – _ denotes one character.

       SELECT      first_name
       FROM        employees
       WHERE       first_name LIKE 'S%' ;




2-12            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the LIKE Condition


       •   You can combine pattern-matching characters:
       SELECT last_name
       FROM   employees
       WHERE last_name LIKE '_o%' ;




       •   You can use the ESCAPE identifier to search for the
           actual % and _ symbols.
       SELECT    employee_id,last_name,job_id
       FROM      employees
       WHERE     job_id LIKE '%SA_%' ESCAPE '' ;



2-13            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the LIKE Condition


       •   You can combine pattern-matching number:
       SELECT last_name,salary
       FROM   employees
       WHERE salary LIKE '1____' ;




2-14         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the NULL Conditions


       Test for nulls with the IS NULL operator.
       SELECT    last_name, manager_id
       FROM      employees
       WHERE     manager_id IS NULL ;



        SELECT last_name, manager_id
        FROM   employees
        WHERE manager_id = NULL ;


        SELECT last_name, manager_id
        FROM   employees
        WHERE manager_id <> NULL ;


2-15            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Logical Conditions


        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-16   Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the AND Operator


       AND requires both conditions to be true:
       SELECT    employee_id, last_name, job_id, salary
       FROM      employees
       WHERE     salary >=10000
       AND       job_id LIKE '%MAN%' ;




2-17            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the OR Operator


       OR requires either condition to be true:
       SELECT    employee_id, last_name, job_id, salary
       FROM      employees
       WHERE     salary >= 10000
       OR        job_id LIKE '%MAN%' ;




2-18            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the NOT Operator
       SELECT    last_name, job_id
       FROM      employees
       WHERE     job_id
                 NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;

       SELECT    last_name, job_id
       FROM      employees
       WHERE     NOT (job_id
                  IN ('IT_PROG', 'ST_CLERK', 'SA_REP')) ;




2-19            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Rules of Precedence


        Operator     Meaning
             1       Arithmetic operators
             2       Concatenation operator
             3       Comparison conditions
             4       IS [NOT] NULL, LIKE, [NOT] IN
                              NULL, LIKE,
             5       [NOT] BETWEEN
             6       Not equal to
             7       NOT logical condition
             8       AND logical condition
             9       OR logical condition

       You can use parentheses to override rules of precedence.


2-20   Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Rules of Precedence

       SELECT    last_name, job_id, salary
       FROM      employees
       WHERE     job_id = 'SA_REP'                                              1
       OR        job_id = 'AD_PRES'
       AND       salary > 15000;




       SELECT    last_name, job_id, salary
       FROM      employees
       WHERE     (job_id = 'SA_REP'                                             2
       OR        job_id = 'AD_PRES')
                           'AD_PRES')
       AND       salary > 15000;




2-21            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the ORDER BY Clause


       •   Sort retrieved rows with the ORDER BY clause:
           – ASC: ascending order, default
             ASC:
           – DESC: descending order
             DESC:
       •   The ORDER BY clause comes last in the SELECT
           statement:
           statement:
       SELECT     last_name, job_id, department_id, hire_date
       FROM       employees
       ORDER BY   hire_date ;




       …

2-22         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Sorting


       •   Sorting in descending order:
       SELECT   last_name, job_id, department_id, hire_date
       FROM     employees
       ORDER BY hire_date DESC ;                         1
       •   Sorting by column alias:
       SELECT employee_id, last_name, salary*12 annsal
                                      salary*
       FROM   employees                                                      2
       ORDER BY annsal ;

       •   Sorting by multiple columns:
       SELECT last_name, department_id, salary
       FROM   employees                                                      3
       ORDER BY department_id, salary DESC;


2-23         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Substitution Variables


                       ... salary = ? …
                       … department_id = ? …
                       ... last_name = ? ...

                                           I want
                                          to query
                                          different
                                           values.




2-24   Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Substitution Variables


       •   Use iSQL*Plus substitution variables to:
                SQL*
           –   Temporarily store values with single-ampersand ( &)
                                                              (&
               and double-ampersand (&&) substitution
                                      &&)
       •   Use substitution variables to supplement the
           following:
           – WHERE conditions
           – ORDER BY clauses
           – Column expressions
           – Table names
           – Entire SELECT statements




2-25           Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the & Substitution Variable


       Use a variable prefixed with an ampersand ( &) to
       prompt the user for a value:
       SELECT employee_id, last_name, salary, department_id
       FROM   employees
       WHERE employee_id = &employee_num ;




2-26          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the & Substitution Variable




         101




                                                       1
                                                                       2




2-27   Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Character and Date Values
                  with Substitution Variables

       Use single quotation marks for date and character
       values:

       SELECT last_name, department_id, salary *12
                                        salary*
       FROM   employees
       WHERE job_id = '&job_title' ;




2-28          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Specifying Column Names,
                   Expressions, and Text

       SELECT employee_id, last_name, job_id,&column_name
       FROM   employees
       WHERE &condition
       ORDER BY &order_column ;




                            salary




                        salary > 15000




                           last_name




2-29         Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the && Substitution Variable


       Use the double ampersand ( &&) if you want to reuse
       the variable value without prompting the user each
       time:
       SELECT   employee_id, last_name, job_id, &&column_name
       FROM     employees
       ORDER BY &column_name ;




   …

2-30          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the iSQL*Plus DEFINE Command
                      SQL*


       •    Use the iSQL*Plus DEFINE command to create and
                     SQL*
            assign a value to a variable.
       •    Use the iSQL*Plus UNDEFINE command to remove
                     SQL*
            a variable.

       DEFINE employee_num = 200

       SELECT    employee_id, last_name, salary, department_id
       FROM      employees
       WHERE     employee_id = &employee_num ;

       UNDEFINE employee_num




2-31            Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Using the VERIFY Command


       Use the VERIFY command to toggle the display of the
       substitution variable, both before and after iSQL*Plus
                                                     SQL*
       replaces substitution variables with values:

       SET VERIFY ON
       SELECT employee_id, last_name, salary, department_id
       FROM   employees
       WHERE employee_id = &employee_num;




       old    3: WHERE      employee_id = &employee_num
       new    3: WHERE      employee_id = 200



2-32          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Summary


       In this lesson, you should have learned how to:
        • Use the WHERE clause to restrict rows of output:
            – Use the comparison conditions
            – Use the BETWEEN, IN, LIKE, and NULL conditions
                      BETWEEN, IN, LIKE,
            – Apply the logical AND, OR, and NOT operators
                                AND, OR,
       •   Use the ORDER BY clause to sort rows of output:
       SELECT *|{[DISTINCT] column|expression [alias],...}
       FROM    table
       [WHERE condition(s)]
       [ORDER BY {column, expr, alias} [ASC|DESC]] ;

       •   Use ampersand substitution in iSQL*Plus to
                                             SQL*
           restrict and sort output at run time

2-33          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Practice 2: Overview


       This practice covers the following topics:
        • Selecting data and changing the order of
           the rows that are displayed
        • Restricting rows by using the WHERE clause
        • Sorting rows by using the ORDER BY clause
        • Using substitution variables to add flexibility to
           your SQL SELECT statements




2-34          Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Practice 2
       Q1:The EMPLOYEES table contains these columns:
       EMPLOYEE_ID         NUMBER(4)
       LAST_NAME           VARCHAR2 (25)
       JOB_ID              VARCHAR2(10)
       You want to search for strings that contain 'SA_' in the JOB_ID
       column. Which SQL statement do you use?

       A. SELECT employee_id, last_name, job_id
       FROM employees WHERE job_id LIKE '%SA_%' ESCAPE '';
       B. SELECT employee_id, last_name, job_id
       FROM employees WHERE job_id LIKE '%SA_';
       C. SELECT employee_id, last_name, job_id
       FROM employees WHERE job_id LIKE '%SA_' ESCAPE "";
       D. SELECT employee_id, last_name, job_id
       FROM employees WHERE job_id = '%SA_';


2-35           Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Practice 2
       Q2: The STUDENT_GRADES table has these columns:
       STUDENT_ID        NUMBER(12)
       SEMESTER_END DATE
       GPA               NUMBER(4,3)
       The registrar requested a report listing the students' grade point
       averages (GPA) sorted from highest grade point average to lowest.
       Which statement produces a report that displays the student ID
       and GPA in the sorted order requested by the registrar?
       A. SELECT student_id, gpa FROM student_grades
           ORDER BY gpa ASC;
       B. SELECT student_id, gpa FROM student_grades
           SORT ORDER BY gpa ASC;
       C. SELECT student_id, gpa FROM student_grades
          SORT ORDER BY gpa;
       D. SELECT student_id, gpa FROM student_grades
          ORDER BY gpa;
       E. SELECT student_id, gpa FROM student_grades
          SORT ORDER BY gpa DESC;
       F. SELECT student_id, gpa FROM student_grades
          ORDER BY gpa DESC;

2-36           Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Practice 2
       Q3:Evaluate these two SQL statements:

             SELECT last_name, salary , hire_date
             FROM EMPLOYEES
             ORDER BY salary DESC;

             SELECT last_name, salary , hire_date
             FROM EMPLOYEES
             ORDER BY 2 DESC;
       What is true about them?

       A. The two statements produce identical results.
       B. The second statement returns a syntax error.
       C. There is no need to specify DESC because the results are
       sorted in descending order by default.
       D. The two statements can be made to produce identical results
       by adding a column alias for the salary column
       in the second SQL statement.


2-37           Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
Practice 2
       Q4: Examine this statement:

           SELECT student_id, gpa
           FROM student_grades
           WHERE gpa > &&value;

       You run the statement once, and when prompted you enter a value
       of 2.0. A report is produced. What happens when you run the
       statement a second time?


       A. An error is returned.
       B. You are prompted to enter a new value.
       C. A report is produced that matches the first report produced.
       D. You are asked whether you want a new value or if you want to
       run the report based on the previous value.




2-38           Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.

More Related Content

What's hot (20)

Les06
Les06Les06
Les06
 
Les02
Les02Les02
Les02
 
Les08
Les08Les08
Les08
 
Les11
Les11Les11
Les11
 
Lesson04 学会使用分组函数
Lesson04 学会使用分组函数Lesson04 学会使用分组函数
Lesson04 学会使用分组函数
 
Les10
Les10Les10
Les10
 
plsql Les07
plsql Les07 plsql Les07
plsql Les07
 
Restricting and sorting data
Restricting and sorting dataRestricting and sorting data
Restricting and sorting data
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Understanding
Understanding Understanding
Understanding
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
Subquery & view
Subquery & viewSubquery & view
Subquery & view
 
Les04
Les04Les04
Les04
 
Frank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrank Rodenbaugh Portfolio
Frank Rodenbaugh Portfolio
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
SQA server performance tuning
SQA server performance tuningSQA server performance tuning
SQA server performance tuning
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
sql language
sql languagesql language
sql language
 

Similar to Lesson02 学会使用WHERE、ORDER BY子句

Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseSalman Memon
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptDrZeeshanBhatti
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBaseSalman Memon
 
Lesson08
Lesson08Lesson08
Lesson08renguzi
 
Lesson07
Lesson07Lesson07
Lesson07renguzi
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Achmad Solichin
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"Ankit Surti
 
e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting dataecomputernotes
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSgaurav koriya
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsSalman Memon
 

Similar to Lesson02 学会使用WHERE、ORDER BY子句 (20)

Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
 
Les02.ppt
Les02.pptLes02.ppt
Les02.ppt
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Lesson08
Lesson08Lesson08
Lesson08
 
Les02
Les02Les02
Les02
 
Lesson07
Lesson07Lesson07
Lesson07
 
Les07
Les07Les07
Les07
 
Les01
Les01Les01
Les01
 
Les05
Les05Les05
Les05
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Les06
Les06Les06
Les06
 
Les06
Les06Les06
Les06
 
plsql les01
 plsql les01 plsql les01
plsql les01
 
Les02
Les02Les02
Les02
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"
 
plsql Les08
plsql Les08 plsql Les08
plsql Les08
 
e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting data
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 

Recently uploaded

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
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
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
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
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.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
 
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
 

Recently uploaded (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
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 .
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
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
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.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
 
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
 

Lesson02 学会使用WHERE、ORDER BY子句

  • 1. Oracle OCP 考试系列培训 之 1Z0-007 Lesson2 Lesson2 www.OracleOnLinux.cn 2-1 Copyright © 2011, www.OracleOnLinux.cn . Part rights reserved.
  • 2. 2 Restricting and Sorting Data 2-2 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 3. Objectives After completing this lesson, you should be able to do the following: • Limit the rows that are retrieved by a query • Sort the rows that are retrieved by a query 2-3 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 4. Limiting Rows Using a Selection EMPLOYEES … “retrieve all employees in department 90”90” 2-4 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 5. Limiting the Rows That Are Selected • Restrict the rows that are returned by using the WHERE clause: SELECT *|{[DISTINCT] column|expression [alias],...} FROM table [WHERE condition(s)]; • The WHERE clause follows the FROM clause. clause. 2-5 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 6. Using the WHERE Clause SELECT employee_id, last_name, job_id, department_id FROM employees WHERE department_id = 90 ; 2-6 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 7. Character Strings and Dates • Character strings and date values are enclosed by single quotation marks. • Character values are case-sensitive , and date case-sensitive, values are format-sensitive . format-sensitive. • The default date format is DD-MON-RR. SELECT last_name, job_id, department_id FROM employees WHERE last_name = 'Whalen' ; 2-7 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 8. Comparison Operators Operator Meaning = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to BETWEEN Between two values (inclusive) ...AND... IN(set) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value 2-8 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 9. Using Comparison Conditions SELECT last_name, salary FROM employees WHERE salary <= 3000 ; 2-9 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 10. Using the BETWEEN Condition Use the BETWEEN condition to display rows based on a range of values: SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500 ; Lower limit Upper limit 2-10 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 11. Using the IN Condition Use the IN membership condition to test for values in a list: SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201) ; 2-11 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 12. Using the LIKE Condition • Use the LIKE condition to perform wildcard searches of valid search string values. • Search conditions can contain either literal characters or numbers: – % denotes zero or many characters. – _ denotes one character. SELECT first_name FROM employees WHERE first_name LIKE 'S%' ; 2-12 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 13. Using the LIKE Condition • You can combine pattern-matching characters: SELECT last_name FROM employees WHERE last_name LIKE '_o%' ; • You can use the ESCAPE identifier to search for the actual % and _ symbols. SELECT employee_id,last_name,job_id FROM employees WHERE job_id LIKE '%SA_%' ESCAPE '' ; 2-13 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 14. Using the LIKE Condition • You can combine pattern-matching number: SELECT last_name,salary FROM employees WHERE salary LIKE '1____' ; 2-14 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 15. Using the NULL Conditions Test for nulls with the IS NULL operator. SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL ; SELECT last_name, manager_id FROM employees WHERE manager_id = NULL ; SELECT last_name, manager_id FROM employees WHERE manager_id <> NULL ; 2-15 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 16. Logical Conditions 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-16 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 17. Using the AND Operator AND requires both conditions to be true: SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >=10000 AND job_id LIKE '%MAN%' ; 2-17 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 18. Using the OR Operator OR requires either condition to be true: SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 10000 OR job_id LIKE '%MAN%' ; 2-18 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 19. Using the NOT Operator SELECT last_name, job_id FROM employees WHERE job_id NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ; SELECT last_name, job_id FROM employees WHERE NOT (job_id IN ('IT_PROG', 'ST_CLERK', 'SA_REP')) ; 2-19 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 20. Rules of Precedence Operator Meaning 1 Arithmetic operators 2 Concatenation operator 3 Comparison conditions 4 IS [NOT] NULL, LIKE, [NOT] IN NULL, LIKE, 5 [NOT] BETWEEN 6 Not equal to 7 NOT logical condition 8 AND logical condition 9 OR logical condition You can use parentheses to override rules of precedence. 2-20 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 21. Rules of Precedence SELECT last_name, job_id, salary FROM employees WHERE job_id = 'SA_REP' 1 OR job_id = 'AD_PRES' AND salary > 15000; SELECT last_name, job_id, salary FROM employees WHERE (job_id = 'SA_REP' 2 OR job_id = 'AD_PRES') 'AD_PRES') AND salary > 15000; 2-21 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 22. Using the ORDER BY Clause • Sort retrieved rows with the ORDER BY clause: – ASC: ascending order, default ASC: – DESC: descending order DESC: • The ORDER BY clause comes last in the SELECT statement: statement: SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date ; … 2-22 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 23. Sorting • Sorting in descending order: SELECT last_name, job_id, department_id, hire_date FROM employees ORDER BY hire_date DESC ; 1 • Sorting by column alias: SELECT employee_id, last_name, salary*12 annsal salary* FROM employees 2 ORDER BY annsal ; • Sorting by multiple columns: SELECT last_name, department_id, salary FROM employees 3 ORDER BY department_id, salary DESC; 2-23 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 24. Substitution Variables ... salary = ? … … department_id = ? … ... last_name = ? ... I want to query different values. 2-24 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 25. Substitution Variables • Use iSQL*Plus substitution variables to: SQL* – Temporarily store values with single-ampersand ( &) (& and double-ampersand (&&) substitution &&) • Use substitution variables to supplement the following: – WHERE conditions – ORDER BY clauses – Column expressions – Table names – Entire SELECT statements 2-25 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 26. Using the & Substitution Variable Use a variable prefixed with an ampersand ( &) to prompt the user for a value: SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num ; 2-26 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 27. Using the & Substitution Variable 101 1 2 2-27 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 28. Character and Date Values with Substitution Variables Use single quotation marks for date and character values: SELECT last_name, department_id, salary *12 salary* FROM employees WHERE job_id = '&job_title' ; 2-28 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 29. Specifying Column Names, Expressions, and Text SELECT employee_id, last_name, job_id,&column_name FROM employees WHERE &condition ORDER BY &order_column ; salary salary > 15000 last_name 2-29 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 30. Using the && Substitution Variable Use the double ampersand ( &&) if you want to reuse the variable value without prompting the user each time: SELECT employee_id, last_name, job_id, &&column_name FROM employees ORDER BY &column_name ; … 2-30 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 31. Using the iSQL*Plus DEFINE Command SQL* • Use the iSQL*Plus DEFINE command to create and SQL* assign a value to a variable. • Use the iSQL*Plus UNDEFINE command to remove SQL* a variable. DEFINE employee_num = 200 SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num ; UNDEFINE employee_num 2-31 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 32. Using the VERIFY Command Use the VERIFY command to toggle the display of the substitution variable, both before and after iSQL*Plus SQL* replaces substitution variables with values: SET VERIFY ON SELECT employee_id, last_name, salary, department_id FROM employees WHERE employee_id = &employee_num; old 3: WHERE employee_id = &employee_num new 3: WHERE employee_id = 200 2-32 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 33. Summary In this lesson, you should have learned how to: • Use the WHERE clause to restrict rows of output: – Use the comparison conditions – Use the BETWEEN, IN, LIKE, and NULL conditions BETWEEN, IN, LIKE, – Apply the logical AND, OR, and NOT operators AND, OR, • Use the ORDER BY clause to sort rows of output: SELECT *|{[DISTINCT] column|expression [alias],...} FROM table [WHERE condition(s)] [ORDER BY {column, expr, alias} [ASC|DESC]] ; • Use ampersand substitution in iSQL*Plus to SQL* restrict and sort output at run time 2-33 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 34. Practice 2: Overview This practice covers the following topics: • Selecting data and changing the order of the rows that are displayed • Restricting rows by using the WHERE clause • Sorting rows by using the ORDER BY clause • Using substitution variables to add flexibility to your SQL SELECT statements 2-34 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 35. Practice 2 Q1:The EMPLOYEES table contains these columns: EMPLOYEE_ID NUMBER(4) LAST_NAME VARCHAR2 (25) JOB_ID VARCHAR2(10) You want to search for strings that contain 'SA_' in the JOB_ID column. Which SQL statement do you use? A. SELECT employee_id, last_name, job_id FROM employees WHERE job_id LIKE '%SA_%' ESCAPE ''; B. SELECT employee_id, last_name, job_id FROM employees WHERE job_id LIKE '%SA_'; C. SELECT employee_id, last_name, job_id FROM employees WHERE job_id LIKE '%SA_' ESCAPE ""; D. SELECT employee_id, last_name, job_id FROM employees WHERE job_id = '%SA_'; 2-35 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 36. Practice 2 Q2: The STUDENT_GRADES table has these columns: STUDENT_ID NUMBER(12) SEMESTER_END DATE GPA NUMBER(4,3) The registrar requested a report listing the students' grade point averages (GPA) sorted from highest grade point average to lowest. Which statement produces a report that displays the student ID and GPA in the sorted order requested by the registrar? A. SELECT student_id, gpa FROM student_grades ORDER BY gpa ASC; B. SELECT student_id, gpa FROM student_grades SORT ORDER BY gpa ASC; C. SELECT student_id, gpa FROM student_grades SORT ORDER BY gpa; D. SELECT student_id, gpa FROM student_grades ORDER BY gpa; E. SELECT student_id, gpa FROM student_grades SORT ORDER BY gpa DESC; F. SELECT student_id, gpa FROM student_grades ORDER BY gpa DESC; 2-36 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 37. Practice 2 Q3:Evaluate these two SQL statements: SELECT last_name, salary , hire_date FROM EMPLOYEES ORDER BY salary DESC; SELECT last_name, salary , hire_date FROM EMPLOYEES ORDER BY 2 DESC; What is true about them? A. The two statements produce identical results. B. The second statement returns a syntax error. C. There is no need to specify DESC because the results are sorted in descending order by default. D. The two statements can be made to produce identical results by adding a column alias for the salary column in the second SQL statement. 2-37 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.
  • 38. Practice 2 Q4: Examine this statement: SELECT student_id, gpa FROM student_grades WHERE gpa > &&value; You run the statement once, and when prompted you enter a value of 2.0. A report is produced. What happens when you run the statement a second time? A. An error is returned. B. You are prompted to enter a new value. C. A report is produced that matches the first report produced. D. You are asked whether you want a new value or if you want to run the report based on the previous value. 2-38 Copyright © 2011, www.OracleOnLinux.cn. Part rights reserved.