computer notes - Writing Basic SQL SELECT Statements

download computer notes - Writing Basic SQL SELECT Statements

of 23

Transcript of computer notes - Writing Basic SQL SELECT Statements

  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    1/23

    Basic SELECT Statement

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    2/23

    SELECT

    FROM

    Basic SELECT Statement

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

    SELECTidentifieswhatcolumns

    FROMidentifieswhichtable

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    3/23

    SELECT *

    Selecting All Columns

    FROM departments;

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    4/23

    Selecting Specific Columns

    SELECT department_id, location_id

    FROM departments;

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    5/23

    Writing SQL Statements

    SQL statements are not case sensitive.

    SQL statements can be on one or more lines.

    Keywords cannot be abbreviated or splitacross lines.

    Clauses are usually placed on separate lines.

    Indents are used to enhance readability.

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    6/23

    Column Heading Defaults

    iSQL*Plus:

    Default heading justification: Center

    Default heading display: Uppercase

    SQL*Plus:

    Character and Date column headings are left-justified

    Number column headings are right-justified

    Default heading display: Uppercase

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    7/23

    Arithmetic Expressions

    Create expressions with number and date data byusing arithmetic operators.

    Operator

    +

    -

    *

    /

    Description

    Add

    Subtract

    Multiply

    Divide

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    8/23

    Using Arithmetic Operators

    SELECT last_name, salary, salary + 300

    FROM employees;

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    9/23

    Operator Precedence

    * / +_

    Multiplication and division take priority over

    addition and subtraction.Operators of the same priority are evaluated fromleft to right.

    Parentheses are used to force prioritizedevaluation and to clarify statements.

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    10/23

    Operator Precedence

    SELECT last_name, salary, 12*salary+100

    FROM employees;

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    11/23

    Using Parentheses

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

    FROM employees;

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    12/23

    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_pct

    FROM employees;

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    13/23

    Null Values

    in Arithmetic Expressions

    Arithmetic expressions containing a null valueevaluate to null.

    SELECT last_name, 12*salary*commission_pct

    FROM employees;

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    14/23

    Defining a Column Alias

    A column alias:

    Renames a column heading

    Is useful with calculations

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

    Requires double quotation marks if it containsspaces or special characters or is case sensitive

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    15/23

    Using Column Aliases

    SELECT last_name AS name, commission_pct comm

    FROM employees;

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

    FROM employees;

    http://ecomputernotes.com

    .

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    16/23

    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 characterexpression

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    17/23

    Using the Concatenation Operator

    SELECT

    FROM

    last_name||job_id AS "Employees"

    employees;

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    18/23

    Literal Character Strings

    A literal is a character, a number, or a dateincluded in theSELECTlist.

    Date and character literal values must be enclosed

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

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    19/23

    Using Literal Character Strings

    SELECT last_name ||' is a '||job_id

    AS "Employee Details"

    FROM employees;

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    20/23

    Duplicate Rows

    The default display of queries is all rows, including

    duplicate rows.

    SELECT department_id

    FROM employees;

    .

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    21/23

    Eliminating Duplicate Rows

    Eliminate duplicate rows by using theDISTINCTkeyword in theSELECTclause.

    SELECT DISTINCT department_id

    FROM employees;

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    22/23

    Displaying Table Structure

    DESCRIBE employees

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 computer notes - Writing Basic SQL SELECT Statements

    23/23