chapter3 thai 6

Upload: ake-thcitizen

Post on 29-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Chapter3 Thai 6

    1/45

    Copyright 2007, Oracle. All rights reserved.

    Oracle

  • 8/9/2019 Chapter3 Thai 6

    2/45

    Copyright 2007, Oracle. All rights reserved.3 - 2

    Capabilities of SQL SELECT Statements

    SelectionProjection

    Table 1 Table 2

    Table 1Table 1

    Join

  • 8/9/2019 Chapter3 Thai 6

    3/45

    Copyright 2007, Oracle. All rights reserved.3 - 3

    Basic SELECT Statement

    SELECT identifies the columns to be displayed.

    FROM identifies the table containing those columns.

    SELECT *|{[DISTINCT] column|expression [alias],...}

    FROM table;

  • 8/9/2019 Chapter3 Thai 6

    4/45

    Copyright 2007, Oracle. All rights reserved.3 - 4

    Selecting All Columns

    SELECT *

    FROM departments;

  • 8/9/2019 Chapter3 Thai 6

    5/45

    Copyright 2007, Oracle. All rights reserved.3 - 5

    Selecting Specific Columns

    SELECT department_id, location_id

    FROM departments;

  • 8/9/2019 Chapter3 Thai 6

    6/45

    Copyright 2007, Oracle. All rights reserved.3 - 6

    Writing SQL Statements

    SQL statements are not case-sensitive. SQL statements can be entered on one or more lines.

    Keywords cannot be abbreviated or split across lines.

    Clauses are usually placed on separate lines.

    Indents are used to enhance readability. In SQL Developer, SQL statements can optionally be

    terminated by a semicolon (;). Semicolons are required whenyou execute multiple SQL statements.

    In SQL*Plus, you are required to end each SQL statementwith a semicolon (;).

  • 8/9/2019 Chapter3 Thai 6

    7/45Copyright 2007, Oracle. All rights reserved.3 - 7

    Column Heading Defaults

    SQL Developer: Default heading alignment: Left-aligned

    Default heading display: Uppercase

    SQL*Plus:

    Character and Date column headings are left-aligned. Number column headings are right-aligned.

    Default heading display: Uppercase

  • 8/9/2019 Chapter3 Thai 6

    8/45Copyright 2007, Oracle. All rights reserved.3 - 8

    Arithmetic Expressions

    Create expressions with number and date data by usingarithmetic operators.

    Multiply*

    Divide/

    Subtract-

    Add+

    DescriptionOperator

  • 8/9/2019 Chapter3 Thai 6

    9/45Copyright 2007, Oracle. All rights reserved.3 - 9

    SELECT last_name, salary, salary + 300

    FROM employees;

    Using Arithmetic Operators

  • 8/9/2019 Chapter3 Thai 6

    10/45Copyright 2007, Oracle. All rights reserved.3 - 1 0

    SELECT last_name, salary, 12*salary+100

    FROM employees;

    Operator Precedence

    SELECT last_name, salary, 12*(salary+100)

    FROM employees;

    1

    2

  • 8/9/2019 Chapter3 Thai 6

    11/45Copyright 2007, Oracle. All rights reserved.3 - 11

    Defining a Null Value

    Null is a value that is unavailable, unassigned, unknown, orinapplicable.

    Null is not the same as zero or a blank space.

    SELECT last_name, job_id, salary, commission_pct

    FROM employees;

  • 8/9/2019 Chapter3 Thai 6

    12/45Copyright 2007, Oracle. All rights reserved.3 - 1 2

    SELECT last_name, 12*salary*commission_pct

    FROM employees;

    Null Values in Arithmetic Expressions

    Arithmetic expressions containing a null value evaluate to null.

  • 8/9/2019 Chapter3 Thai 6

    13/45Copyright 2007, Oracle. All rights reserved.3 - 1 3

    Defining a Column Alias

    A column alias: Renames a column heading

    Is useful with calculations

    Immediately follows the column name (There can also be the

    optional AS keyword between the column name and alias.) Requires double quotation marks if it contains spaces or

    special characters, or if it is case-sensitive

  • 8/9/2019 Chapter3 Thai 6

    14/45Copyright 2007, Oracle. All rights reserved.3 - 1 4

    Using Column Aliases

    SELECT last_name "Name" , salary*12 "Annual Salary"

    FROM employees;

    SELECT last_name AS name, commission_pct comm

    FROM employees;

  • 8/9/2019 Chapter3 Thai 6

    15/45Copyright 2007, Oracle. All rights reserved.3 - 1 5

    Concatenation Operator

    A concatenation operator: Links columns or character strings to other columns

    Is represented by two vertical bars (||)

    Creates a resultant column that is a character expression

    SELECT last_name||job_id AS "Employees"

    FROM employees;

  • 8/9/2019 Chapter3 Thai 6

    16/45Copyright 2007, Oracle. All rights reserved.3 - 1 6

    Literal Character Strings

    A literal is a character, a number, or a date that is included inthe SELECT statement.

    Date and character literal values must be enclosed withinsingle quotation marks.

    Each character string is output once for each row returned.

  • 8/9/2019 Chapter3 Thai 6

    17/45Copyright 2007, Oracle. All rights reserved.3 - 1 7

    Using Literal Character Strings

    SELECT last_name ||' is a '||job_id

    AS "Employee Details"

    FROM employees;

  • 8/9/2019 Chapter3 Thai 6

    18/45Copyright 2007, Oracle. All rights reserved.3 - 1 8

    Alternative Quote (q) Operator

    Specify your own quotation mark delimiter. Select any delimiter.

    Increase readability and usability.

    SELECT department_name || ' Department' ||

    q'['s Manager Id: ]'|| manager_id

    AS "Department and Manager"

    FROM departments;

  • 8/9/2019 Chapter3 Thai 6

    19/45Copyright 2007, Oracle. All rights reserved.3 - 1 9

    Duplicate Rows

    The default display of queries is all rows, including duplicaterows.

    SELECT department_id

    FROM employees;

    SELECT DISTINCT department_id

    FROM employees;

    1

    2

  • 8/9/2019 Chapter3 Thai 6

    20/45Copyright 2007, Oracle. All rights reserved.3 - 2 0

    Limiting Rows Using a Selection

    retrieve allemployees in

    department 90

    EMPLOYEES

  • 8/9/2019 Chapter3 Thai 6

    21/45Copyright 2007, Oracle. All rights reserved.3 - 2 1

    Limiting the Rows that Are Selected

    Restrict the rows that are returned by using the WHEREclause:

    The WHERE clause follows the FROM clause.

    SELECT *|{[DISTINCT] column|expression[alias],...}

    FROM table

    [WHERE condition(s)];

  • 8/9/2019 Chapter3 Thai 6

    22/45Copyright 2007, Oracle. All rights reserved.3 - 2 2

    SELECT employee_id, last_name, job_id, department_id

    FROM employees

    WHERE department_id = 90 ;

    Using theWHERE Clause

  • 8/9/2019 Chapter3 Thai 6

    23/45Copyright 2007, Oracle. All rights reserved.3 - 2 3

    SELECT last_name, job_id, department_id

    FROM employees

    WHERE last_name = 'Whalen' ;

    Character Strings and Dates

    Character strings and date values are enclosed with singlequotation marks.

    Character values are case-sensitive and date values areformat-sensitive.

    The default date display format is DD-MON-RR.

    SELECT last_name

    FROM employees

    WHERE hire_date = '17-FEB-96' ;

  • 8/9/2019 Chapter3 Thai 6

    24/45Copyright 2007, Oracle. All rights reserved.3 - 2 4

    Comparison Operators

    Not equal to

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

    Match any of a list of valuesIN(set)

    Match a character patternLIKE

    Is a null valueIS NULL

    Less than

    Equal to=

    MeaningOperator

  • 8/9/2019 Chapter3 Thai 6

    25/45Copyright 2007, Oracle. All rights reserved.3 - 2 5

    SELECT last_name, salary

    FROM employees

    WHERE salary

  • 8/9/2019 Chapter3 Thai 6

    26/45

    Copyright 2007, Oracle. All rights reserved.3 - 2 6

    SELECT last_name, salary

    FROM employees

    WHERE salary BETWEEN 2500 AND 3500 ;

    Range Conditions Using the BETWEEN Operator

    Use the BETWEEN operator to display rows based on a range ofvalues:

    Lower limit Upper limit

  • 8/9/2019 Chapter3 Thai 6

    27/45

    Copyright 2007, Oracle. All rights reserved.3 - 2 7

    SELECT employee_id, last_name, salary, manager_id

    FROM employees

    WHERE manager_id IN (100, 101, 201) ;

    Membership Condition Using the IN Operator

    Use the IN operator to test for values in a list:

  • 8/9/2019 Chapter3 Thai 6

    28/45

    Copyright 2007, Oracle. All rights reserved.3 - 2 8

    SELECT first_name

    FROM employees

    WHERE first_name LIKE 'S%' ;

    Pattern Matching Using the LIKE Operator

    Use the LIKE operator to perform wildcard searches of validsearch string values.

    Search conditions can contain either literal characters ornumbers:

    % denotes zero or many characters.

    _ denotes one character.

  • 8/9/2019 Chapter3 Thai 6

    29/45

  • 8/9/2019 Chapter3 Thai 6

    30/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 0

    SELECT last_name, manager_id

    FROM employees

    WHERE manager_id IS NULL ;

    Using theNULL Conditions

    Test for nulls with the IS NULL operator.

  • 8/9/2019 Chapter3 Thai 6

    31/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 1

    Defining Conditions Using the Logical Operators

    Returns TRUE if the condition is falseNOT

    Returns TRUE if eithercomponent

    condition is true

    OR

    Returns TRUE if bothcomponentconditions are true

    AND

    MeaningOperator

  • 8/9/2019 Chapter3 Thai 6

    32/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 2

    SELECT employee_id, last_name, job_id, salary

    FROM employees

    WHERE salary >= 10000

    AND job_id LIKE '%MAN%' ;

    Using theAND Operator

    AND requires both the component conditions to be true:

  • 8/9/2019 Chapter3 Thai 6

    33/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 3

    SELECT employee_id, last_name, job_id, salary

    FROM employees

    WHERE salary >= 10000

    OR job_id LIKE '%MAN%' ;

    Using the OROperator

    OR requires either component condition to be true:

  • 8/9/2019 Chapter3 Thai 6

    34/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 4

    SELECT last_name, job_id

    FROM employees

    WHERE job_id

    NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP') ;

    Using theNOT Operator

  • 8/9/2019 Chapter3 Thai 6

    35/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 5

    Rules of Precedence

    You can use parentheses to override rules of precedence.

    Not equal to6

    NOT logical condition7

    AND logical condition8

    OR logical condition9

    IS [NOT] NULL, LIKE, [NOT] IN4

    [NOT] BETWEEN5

    Comparison conditions3

    Concatenation operator2

    Arithmetic operators1

    MeaningOperator

  • 8/9/2019 Chapter3 Thai 6

    36/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 6

    SELECT last_name, job_id, salary

    FROM employeesWHERE job_id = 'SA_REP'

    OR job_id = 'AD_PRES'

    AND salary > 15000;

    Rules of Precedence

    SELECT last_name, job_id, salary

    FROM employees

    WHERE (job_id = 'SA_REP'

    OR job_id = 'AD_PRES')

    AND salary > 15000;

    1

    2

  • 8/9/2019 Chapter3 Thai 6

    37/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 7

    Using the ORDER BY Clause

    Sort retrieved rows with the ORDER BY clause: ASC: Ascending order, default

    DESC: Descending order

    The ORDER BY clause comes last in the SELECT statement:

    SELECT last_name, job_id, department_id, hire_dateFROM employees

    ORDER BY hire_date ;

  • 8/9/2019 Chapter3 Thai 6

    38/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 8

    Sorting

    Sorting in descending order:

    Sorting by column alias:

    SELECT last_name, job_id, department_id, hire_date

    FROM employees

    ORDER BY hire_date DESC ;1

    SELECT employee_id, last_name, salary*12 annsal

    FROM employees

    ORDER BY annsal ;

    2

  • 8/9/2019 Chapter3 Thai 6

    39/45

    Copyright 2007, Oracle. All rights reserved.3 - 3 9

    Sorting

    Sorting by using the columns numeric position:

    Sorting by multiple columns:

    SELECT last_name, job_id, department_id, hire_date

    FROM employees

    ORDER BY 3;3

    SELECT last_name, department_id, salary

    FROM employees

    ORDER BY department_id, salary DESC;4

  • 8/9/2019 Chapter3 Thai 6

    40/45

    Copyright 2007, Oracle. All rights reserved.3 - 4 0

    Substitution Variables

    ... salary = ?

    department_id = ?

    ... last_name = ? ...

    I wantto query

    different

    values.

  • 8/9/2019 Chapter3 Thai 6

    41/45

    Copyright 2007, Oracle. All rights reserved.3 - 4 1

    SELECT employee_id, last_name, salary, department_id

    FROM employees

    WHERE employee_id = &employee_num ;

    Using the Single-Ampersand SubstitutionVariable

    Use a variable prefixed with an ampersand (&) to prompt theuser for a value:

  • 8/9/2019 Chapter3 Thai 6

    42/45

    Copyright 2007, Oracle. All rights reserved.3 - 4 2

    Using the Single-Ampersand SubstitutionVariable

  • 8/9/2019 Chapter3 Thai 6

    43/45

    Copyright 2007, Oracle. All rights reserved.3 - 4 3

    SELECT last_name, department_id, salary*12

    FROM employees

    WHERE job_id = '&job_title' ;

    Character and Date Values withSubstitution Variables

    Use single quotation marks for date and character values:

  • 8/9/2019 Chapter3 Thai 6

    44/45

    Copyright 2007, Oracle. All rights reserved.3 - 4 4

    Specifying Column Names,Expressions, and Text

    SELECT employee_id, last_name, job_id,&column_name

    FROM employees

    WHERE &condition

    ORDER BY &order_column ;

  • 8/9/2019 Chapter3 Thai 6

    45/45

    Workshop : Overview

    6.1 6.20