Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Objectives

After completing this lesson, you should be able to do


the following:
• List the capabilities of SQL SELECT statements
Retrieving Data Using
• Execute a basic SELECT statement
the SQL SELECT Statement
• Differentiate between SQL statements and
iSQL*Plus commands

Copyright © 2006, Oracle. All rights reserved. 1-2 Copyright © 2006, Oracle. All rights reserved.

Capabilities of SQL SELECT Statements Basic SELECT Statement

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


FROM table;

• SELECT identifies the columns to be displayed.


• FROM identifies the table containing those columns.
Table 1 Table 1

Join

Table 1 Table 2

1-3 Copyright © 2006, Oracle. All rights reserved. 1-4 Copyright © 2006, Oracle. All rights reserved.
Selecting All Columns Selecting Specific Columns

SELECT * SELECT department_id, location_id


FROM departments; FROM departments;

1-5 Copyright © 2006, Oracle. All rights reserved. 1-6 Copyright © 2006, Oracle. All rights reserved.

Writing SQL Statements Column Heading Defaults

• SQL statements are not case sensitive. • iSQL*Plus:


• SQL statements can be on one or more lines. – Default heading alignment: Center
• Keywords cannot be abbreviated or split – Default heading display: Uppercase
across lines. • SQL*Plus:
• Clauses are usually placed on separate lines. – Character and Date column headings are left-aligned
• Indents are used to enhance readability. – Number column headings are right-aligned
• In iSQL*Plus, SQL statements can optionally be – Default heading display: Uppercase
terminated by a semicolon (;). Semicolons are
required if you execute multiple SQL statements.
• In SQL*Plus, you are required to end each SQL
statement with a semicolon (;).

1-7 Copyright © 2006, Oracle. All rights reserved. 1-8 Copyright © 2006, Oracle. All rights reserved.
Arithmetic Expressions Using Arithmetic Operators

Create expressions with number and date data by using SELECT last_name, salary, salary + 300
arithmetic operators. FROM employees;

Operator Description
+ Add
- Subtract
* Multiply
/ Divide …

1-9 Copyright © 2006, Oracle. All rights reserved. 1 - 10 Copyright © 2006, Oracle. All rights reserved.

Operator Precedence Defining a Null Value

SELECT last_name, salary, 12*salary+100 • A null is a value that is unavailable, unassigned,


FROM employees; 1 unknown, or inapplicable.
• A null is not the same as a zero or a blank space.
SELECT last_name, job_id, salary, commission_pct
… FROM employees;

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


FROM employees;
2

… …

1 - 11 Copyright © 2006, Oracle. All rights reserved. 1 - 12 Copyright © 2006, Oracle. All rights reserved.
Null Values Defining a Column Alias
in Arithmetic Expressions

Arithmetic expressions containing a null value evaluate to A column alias:


null. • Renames a column heading
SELECT last_name, 12*salary*commission_pct • Is useful with calculations
FROM employees; • 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

1 - 13 Copyright © 2006, Oracle. All rights reserved. 1 - 14 Copyright © 2006, Oracle. All rights reserved.

Using Column Aliases Concatenation Operator

SELECT last_name AS name, commission_pct comm A concatenation operator:


FROM employees;
• 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 "Name" , salary*12 "Annual Salary"
SELECT last_name||job_id AS "Employees"
FROM employees;
FROM employees;

… …

1 - 15 Copyright © 2006, Oracle. All rights reserved. 1 - 16 Copyright © 2006, Oracle. All rights reserved.
Literal Character Strings Using Literal Character Strings

• A literal is a character, a number, or a date that is


SELECT last_name ||' is a '||job_id
included in the SELECT statement. AS "Employee Details"
• Date and character literal values must be enclosed FROM employees;
by single quotation marks.
• Each character string is output once for each
row returned.

1 - 17 Copyright © 2006, Oracle. All rights reserved. 1 - 18 Copyright © 2006, Oracle. All rights reserved.

Alternative Quote (q) Operator Duplicate Rows

• Specify your own quotation mark delimiter The default display of queries is all rows, including
• Choose any delimiter duplicate rows.
• Increase readability and usability SELECT department_id
FROM employees; 1
SELECT department_name ||
q'[, it's assigned Manager Id: ]'
|| manager_id
AS "Department and Manager" …
FROM departments;
SELECT DISTINCT department_id
FROM employees; 2


1 - 19 Copyright © 2006, Oracle. All rights reserved. 1 - 20 Copyright © 2006, Oracle. All rights reserved.
SQL and iSQL*Plus Interaction SQL Statements Versus
iSQL*Plus Commands
SQL iSQL*Plus
• A language • An environment
SQL statements
Oracle • ANSI standard • Oracle-proprietary
server
• Keyword cannot be • Keywords can be
Internet abbreviated abbreviated
browser
• Statements manipulate • Commands do not allow
data and table definitions manipulation of values in
in the database the database
iSQL*Plus Query results
commands • Runs on a browser
• Centrally loaded; does not
Formatted report have to be implemented
Client on each machine

SQL iSQL*Plus
statements commands

1 - 21 Copyright © 2006, Oracle. All rights reserved. 1 - 22 Copyright © 2006, Oracle. All rights reserved.

Overview of iSQL*Plus Logging In to iSQL*Plus

After you log in to iSQL*Plus, you can: From your browser environment:
• Describe table structures
• Enter, execute, and edit SQL statements
• Save or append SQL statements to files
• Execute or edit statements that are stored in saved
script files

1 - 23 Copyright © 2006, Oracle. All rights reserved. 1 - 24 Copyright © 2006, Oracle. All rights reserved.
iSQL*Plus Environment Displaying Table Structure
8 9

Use the iSQL*Plus DESCRIBE command to display the


7 structure of a table:

DESC[RIBE] tablename

1
6

2 3 4 5

1 - 25 Copyright © 2006, Oracle. All rights reserved. 1 - 26 Copyright © 2006, Oracle. All rights reserved.

Displaying Table Structure Interacting with Script Files

DESCRIBE employees

SELECT last_name, hire_date, salary


FROM employees; 1

1 - 27 Copyright © 2006, Oracle. All rights reserved. 1 - 28 Copyright © 2006, Oracle. All rights reserved.
Interacting with Script Files Interacting with Script Files

1 - 29 Copyright © 2006, Oracle. All rights reserved. 1 - 30 Copyright © 2006, Oracle. All rights reserved.

Interacting with Script Files iSQL*Plus History Page

D:\TEMP\emp_data.sql

2 1
3

1 - 31 Copyright © 2006, Oracle. All rights reserved. 1 - 32 Copyright © 2006, Oracle. All rights reserved.
iSQL*Plus History Page Setting iSQL*Plus Preferences

3
1

2
4 3

1 - 33 Copyright © 2006, Oracle. All rights reserved. 1 - 34 Copyright © 2006, Oracle. All rights reserved.

Setting the Output Location Preference Summary

2
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 display more descriptive
column headings
• Use the iSQL*Plus environment to write, save, and
execute SQL statements and iSQL*Plus commands

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


FROM table;

1 - 35 Copyright © 2006, Oracle. All rights reserved. 1 - 36 Copyright © 2006, Oracle. All rights reserved.
Practice 1: Overview

This practice covers the following topics:


• Selecting all data from different tables
• Describing the structure of tables
• Performing arithmetic calculations and specifying
column names
• Using iSQL*Plus

1 - 37 Copyright © 2006, Oracle. All rights reserved. 1 - 38 Copyright © 2006, Oracle. All rights reserved.

1 - 39 Copyright © 2006, Oracle. All rights reserved. 1 - 40 Copyright © 2006, Oracle. All rights reserved.
1 - 41 Copyright © 2006, Oracle. All rights reserved. 1 - 42 Copyright © 2006, Oracle. All rights reserved.

You might also like