Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the...

23
Writing Basic Writing Basic SQL Statements SQL Statements

Transcript of Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the...

Page 1: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Writing Basic Writing Basic SQL StatementsSQL Statements

Page 2: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

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

Page 3: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Capabilities of SQL SELECT Statements

SelectionSelection ProjectionProjection

TableTable 11 TableTable 22

TableTable 11 TableTable 11JoinJoin

Page 4: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Basic SELECT Statement

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

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

SELECT identifies what columns FROM identifies which table

Page 5: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

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. Tabs and indents are used to enhance

readability.

Page 6: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Selecting All Columns

SQL> SELECT * 2 FROM product_t;

PRODUCT_ID PRODUCT_DESCRIPTION

PRODUCT_FINISH STANDARD_PRICE

PRODUCT_LINE_ID

1 End Table Cherry 175 10001

2 Coffee Table Natural Ash 200 20001

3 Computer Desk Natural Ash 375 20001

4 Entertainment Center

Natural Oak 650 30001

5 Writers Desk Cherry 325 10001

6 8-Drawer Dresser White Ash 750 20001

7 Dining Table Natural Ash 850 20001

8 Computer Desk Walnut 250 30001

Page 7: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Selecting Specific Columns

SQL> SELECT product_id, product_description 2 FROM product_t;

PRODUCT_ID PRODUCT_DESCRIPTION

1 End Table

2 Coffee Table

3 Computer Desk

4 Entertainment Center

5 Writers Desk

6 8-Drawer Dresser

7 Dining Table

8 Computer Desk

Page 8: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Column Heading Defaults

Default justification Left: Date and character data Right: Numeric data

Default display: Uppercase

Page 9: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Arithmetic Expressions

Create expressions on NUMBER and DATE data by using arithmetic operators.

Operator

+

-

*

/

Description

Add

Subtract

Multiply

Divide

Page 10: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Using Arithmetic Operators

STANDARD_PRICE+300

475

500

675

950

625

1050

1150

550

PRODUCT_DESCRIPTION

End Table

Coffee Table

Computer Desk

Entertainment Center

Writers Desk

8-Drawer Dresser

Dining Table

Computer Desk

SQL> SELECT product_description, standard_price, standard_price+300FROM product_t;

STANDARD_PRICE

175

200

375

650

325

750

850

250

Page 11: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

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 12: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Operator Precedence

SQL> SELECT product_description, standard_price, 10*standard_price+300FROM product_t;

10*STANDARD_PRICE+300

2050

2300

4050

6800

3550

7800

8800

2800

PRODUCT_DESCRIPTION

End Table

Coffee Table

Computer Desk

Entertainment Center

Writers Desk

8-Drawer Dresser

Dining Table

Computer Desk

STANDARD_PRICE

175

200

375

650

325

750

850

250

Page 13: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Using Parentheses

SQL> SELECT product_description, standard_price, 10*(standard_price+300)FROM product_t;

10*(STANDARD_PRICE+300)

4750

5000

6750

9500

6250

10500

11500

5500

PRODUCT_DESCRIPTION

End Table

Coffee Table

Computer Desk

Entertainment Center

Writers Desk

8-Drawer Dresser

Dining Table

Computer Desk

STANDARD_PRICE

175

200

375

650

325

750

850

250

Page 14: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

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.

Page 15: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Null Values in Arithmetic Expressions

Arithmetic expressions containing a null value evaluate to null.

Page 16: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Defining a Column Alias

Renames a column heading Is useful with calculations Immediately follows column name;

optional AS keyword between column name and alias

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

Page 17: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Using Column Aliases

CUSTOMER ADDRESS

Contemporary Casuals 1355 S Hines Blvd

Value Furniture 15145 S. W. 17th St.

Home Furnishings 1900 Allard Ave

SQL> SELECT customer_name AS customer, customer_address address

FROM customer_t;

SQL> SELECT customer_name “Customer Name”, customer_address “Street Address”

FROM customer_t;

Customer Name Street Address

Contemporary Casuals 1355 S Hines Blvd

Value Furniture 15145 S. W. 17th St.

Home Furnishings 1900 Allard Ave

Page 18: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Concatenation Operator

Concatenates columns or character strings to other columns

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

character expression

Page 19: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Using the Concatenation OperatorSQL> SELECT CONCAT(product_description, product_finish)

AS “Product Finish"FROM product_t;

Product Finish

End TableCherry

Coffee TableNatural Ash

Computer DeskNatural Ash

Entertainment CenterNatural Oak

Writers DeskCherry

8-Drawer DresserWhite Ash

Dining TableNatural Ash

Computer DeskWalnut

Page 20: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Literal Character Strings

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

Date and character literal values must be enclosed within single quotation marks.

Each character string is output once for each row returned.

Page 21: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Using Literal Character Strings

SQL> SELECT CONCAT(product_description,'is a‘,product_finish) AS “Product Finish“FROM product_t;

Product Finish

End Table is a Cherry

Coffee Table is a Natural Ash

Computer Desk is a Natural Ash

Entertainment Center is a Natural Oak

Writers Desk is a Cherry

8-Drawer Dresser is a White Ash

Dining Table is a Natural Ash

Computer Desk is a Walnut

Page 22: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Duplicate Rows

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

SELECT product_line_idFROM product_t;

SELECT product_line_idFROM product_t;

PRODUCT_LINE_ID

10001

20001

20001

30001

10001

20001

20001

30001

Page 23: Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements.

Eliminating Duplicate Rows

Eliminate duplicate rows using the DISTINCT keyword in the SELECT clause

SELECT DISTINCT product_line_idFROM product_t;

PRODUCT_LINE_ID

10001

20001

30001