basdat-2 sql select

Upload: budi-wang

Post on 10-Apr-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Basdat-2 SQL Select

    1/25

    1Copyright Oracle Corporation, 2001. All rights reserved.

    Writing BasicSQL SELECT Statements

  • 8/8/2019 Basdat-2 SQL Select

    2/25

    1-2 Copyright Oracle Corporation, 2001. All rights reserved.

    Objectives

    After completing this lesson, you should be able todo the following:

    List the capabilities of SQL SELECT statements

    Execute a basic SELECT statement Differentiate between SQL statements and

    iSQL*Plus commands

  • 8/8/2019 Basdat-2 SQL Select

    3/25

    1-3 Copyright Oracle Corporation, 2001. All rights reserved.

    Capabilities of SQL SELECT Statements

    SelectionProjection

    Table 1 Table 2

    Table 1Table 1

    Join

  • 8/8/2019 Basdat-2 SQL Select

    4/25

    1-4 Copyright Oracle Corporation, 2001. All rights reserved.

    Basic SELECT Statement

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

    FROM table;

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

    FROM table;

    SELECT identifies whatcolumns

    FROMidentifies which table

  • 8/8/2019 Basdat-2 SQL Select

    5/25

    1-5 Copyright Oracle Corporation, 2001. All rights reserved.

    SELECT *

    FROM departments;

    Selecting All Columns

  • 8/8/2019 Basdat-2 SQL Select

    6/25

    1-6 Copyright Oracle Corporation, 2001. All rights reserved.

    Selecting Specific Columns

    SELECT department_id, location_id

    FROM departments;

  • 8/8/2019 Basdat-2 SQL Select

    7/251-7 Copyright Oracle Corporation, 2001. All rights reserved.

    Writing SQL Statements

    SQL statements are not case sensitive.

    SQL statements can be 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.

  • 8/8/2019 Basdat-2 SQL Select

    8/251-9 Copyright Oracle Corporation, 2001. All rights reserved.

    Arithmetic Expressions

    Create expressions with number and date data byusing arithmetic operators.

    Operator

    +

    -

    *

    /

    Description

    Add

    Subtract

    Multiply

    Divide

  • 8/8/2019 Basdat-2 SQL Select

    9/251-10 Copyright Oracle Corporation, 2001. All rights reserved.

    Using Arithmetic Operators

    SELECT last_name, salary, salary + 300

    FROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    10/251-11 Copyright Oracle Corporation, 2001. All rights reserved.

    Operator Precedence

    Multiplication and division take priority overaddition and subtraction.

    Operators of the same priority are evaluated fromleft to right.

    Parentheses are used to force prioritizedevaluation and to clarify statements.

    *** /// +++ ___

  • 8/8/2019 Basdat-2 SQL Select

    11/251-12 Copyright Oracle Corporation, 2001. All rights reserved.

    Operator Precedence

    SELECT last_name, salary, 12*salary+100

    FROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    12/251-13 Copyright Oracle Corporation, 2001. All rights reserved.

    Using Parentheses

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

    FROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    13/251-14 Copyright Oracle Corporation, 2001. All rights reserved.

    Defining a Null Value

    A null is a value that is unavailable, unassigned,unknown, or inapplicable.

    A null is not the same as zero or a blank space.

    SELECT last_name, job_id, salary, commission_pctFROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    14/251-15 Copyright Oracle Corporation, 2001. All rights reserved.

    SELECT last_name, 12*salary*commission_pct

    FROM employees;

    Null Valuesin Arithmetic Expressions

    Arithmetic expressions containing a null valueevaluate to null.

  • 8/8/2019 Basdat-2 SQL Select

    15/251-16 Copyright Oracle Corporation, 2001. All rights reserved.

    Defining a Column Alias

    A column alias:

    Renames a column heading

    Is useful with calculations

    Immediately follows the column name - there canalso be the optionalAS keyword between thecolumn name and alias

    Requires double quotation marks if it contains

    spaces or special characters or is case sensitive

  • 8/8/2019 Basdat-2 SQL Select

    16/251-17 Copyright Oracle Corporation, 2001. All rights reserved.

    Using Column Aliases

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

    FROM employees;

    SELECT last_name AS name, commission_pct commFROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    17/251-18 Copyright Oracle Corporation, 2001. All rights reserved.

    Concatenation Operator

    A concatenation operator:

    Concatenates columns or character strings toother columns

    Is represented by two vertical bars (||) Creates a resultant column that is a character

    expression

  • 8/8/2019 Basdat-2 SQL Select

    18/25

  • 8/8/2019 Basdat-2 SQL Select

    19/251-20 Copyright Oracle Corporation, 2001. All rights reserved.

    Literal Character Strings

    A literal is a character, a number, or a dateincluded in the SELECT list.

    Date and character literal values must be enclosed

    within single quotation marks. Each character string is output once for eachrow returned.

  • 8/8/2019 Basdat-2 SQL Select

    20/251-21 Copyright Oracle Corporation, 2001. All rights reserved.

    Using Literal Character Strings

    SELECT last_name ||' is a '||job_id

    AS "Employee Details"

    FROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    21/251-22 Copyright Oracle Corporation, 2001. All rights reserved.

    Duplicate Rows

    The default display of queries is all rows, includingduplicate rows.

    SELECT department_id

    FROM employees;

    SELECT department_id

    FROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    22/25

    1-23 Copyright Oracle Corporation, 2001. All rights reserved.

    Eliminating Duplicate Rows

    Eliminate duplicate rows by using the DISTINCTkeyword in the SELECT clause.

    SELECT DISTINCT department_id

    FROM employees;

  • 8/8/2019 Basdat-2 SQL Select

    23/25

    1-29 Copyright Oracle Corporation, 2001. All rights reserved.

    Displaying Table Structure

    Use the iSQL*Plus DESCRIBE command to displaythe structure of a table.

    DESC[RIBE] tablenameDESC[RIBE] tablename

  • 8/8/2019 Basdat-2 SQL Select

    24/25

    1-30 Copyright Oracle Corporation, 2001. All rights reserved.

    Displaying Table Structure

    DESCRIBE employeesDESCRIBE employees

  • 8/8/2019 Basdat-2 SQL Select

    25/25

    Summary

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

    FROM table;

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

    FROM table;

    In this lesson, you should have learned how to:

    Write a SELECT statement that: Returns all rows and columns from a table

    Returns specified columns from a table

    Uses column aliases to give descriptive columnheadings

    Use the iSQL*Plus environment to write, save, andexecute SQL statements and iSQL*Plus commands.