les01.ppt

22
1 Writing Basic SQL SELECT Statements

Upload: daniel-alejandro-romero

Post on 08-Nov-2014

16 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Les01.ppt

1Writing Basic SQL SELECT Statements

Page 2: Les01.ppt

1-2

Objectives

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

• List the capabilities of SQL SELECT statements

• Execute a basic SELECT statement

• Differentiate between SQL statements and iSQL*Plus commands

Page 3: Les01.ppt

1-3

Capabilities of SQL SELECT Statements

SelectionProjection

Table 1 Table 2

Table 1Table 1

Join

Page 4: Les01.ppt

1-4

Basic SELECT Statement

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

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

• SELECT identifies what columns

• FROM identifies which table

Page 5: Les01.ppt

1-5

SELECT *FROM departments;

Selecting All Columns

Page 6: Les01.ppt

1-6

Selecting Specific Columns

SELECT department_id, location_idFROM departments;

Page 7: Les01.ppt

1-7

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.

Page 8: Les01.ppt

1-8

Arithmetic Expressions

Create expressions with number and date data by using arithmetic operators.

Operator

+

-

*

/

Description

Add

Subtract

Multiply

Divide

Page 9: Les01.ppt

1-9

Using Arithmetic Operators

SELECT last_name, salary, salary + 300FROM employees;

Page 10: Les01.ppt

1-10

Operator Precedence

• Multiplication and division take priority over addition and subtraction.

• Operators of the same priority are evaluated from left to right.

• Parentheses are used to force prioritized evaluation and to clarify statements.

**** //// ++++ ____

Page 11: Les01.ppt

1-11

Operator Precedence

SELECT last_name, salary, 12*salary+100FROM employees;

Page 12: Les01.ppt

1-12

Using Parentheses

SELECT last_name, salary, 12*(salary+100)FROM employees;

Page 13: Les01.ppt

1-13

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;

Page 14: Les01.ppt

1-14

SELECT last_name, 12*salary*commission_pctFROM employees;

Null Values in Arithmetic Expressions

Arithmetic expressions containing a null value evaluate to null.

Page 15: Les01.ppt

1-15

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 is case sensitive

Page 16: Les01.ppt

1-16

Using Column Aliases

SELECT last_name "Name", salary*12 "Annual Salary"FROM employees;

SELECT last_name AS name, commission_pct commFROM employees;

Page 17: Les01.ppt

1-17

Duplicate Rows

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

SELECT department_idFROM employees;

SELECT department_idFROM employees;

Page 18: Les01.ppt

1-18

Eliminating Duplicate Rows

Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.

SELECT DISTINCT department_idFROM employees;

Page 19: Les01.ppt

1-19

SQL Interaction

SQL statements

MySQLserver

Query resultsQuery results

Client

Formatted report

Internet Internet BrowserBrowser

SQL (PHP)SQL (PHP)

Page 20: Les01.ppt

1-20

Displaying Table Structure

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

DESC[RIBE] tablenameDESC[RIBE] tablename

Page 21: Les01.ppt

1-21

Displaying Table Structure

DESCRIBE employeesDESCRIBE employees

Page 22: Les01.ppt

1-22

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 column headings