Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the...
-
Upload
anabel-jackson -
Category
Documents
-
view
221 -
download
1
Transcript of Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the...
Writing Basic Writing Basic SQL StatementsSQL 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
Capabilities of SQL SELECT Statements
SelectionSelection ProjectionProjection
TableTable 11 TableTable 22
TableTable 11 TableTable 11JoinJoin
Basic SELECT Statement
SELECT [DISTINCT] {*, column [alias],...}FROM table;
SELECT [DISTINCT] {*, column [alias],...}FROM table;
SELECT identifies what columns FROM identifies which table
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.
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
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
Column Heading Defaults
Default justification Left: Date and character data Right: Numeric data
Default display: Uppercase
Arithmetic Expressions
Create expressions on NUMBER and DATE data by using arithmetic operators.
Operator
+
-
*
/
Description
Add
Subtract
Multiply
Divide
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
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.
**** //// ++++ ____
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
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
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.
Null Values in Arithmetic Expressions
Arithmetic expressions containing a null value evaluate to null.
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
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
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
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
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.
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
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
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