Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following:  List the capabilities of SQL SELECT statements."— Presentation transcript:

1 Writing Basic SQL Statements

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

3 Capabilities of SQL SELECT Statements Selection Projection Table 1 Table 2 Table 1 Join

4 Basic SELECT Statement SELECT[DISTINCT] {*, column [alias],...} FROMtable; SELECT[DISTINCT] {*, column [alias],...} FROMtable; SELECT identifies what columns FROM identifies which table

5 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.

6 Selecting All Columns SQL> SELECT * 2 FROM product_t; PRODUCT_IDPRODUCT_DESC RIPTION PRODUCT_FINISHSTANDARD_PRIC E PRODUCT_LINE_I D 1End TableCherry17510001 2Coffee TableNatural Ash20020001 3Computer DeskNatural Ash37520001 4Entertainment Center Natural Oak65030001 5Writers DeskCherry32510001 68-Drawer DresserWhite Ash75020001 7Dining TableNatural Ash85020001 8Computer DeskWalnut25030001

7 Selecting Specific Columns SQL> SELECT product_id, product_description 2 FROM product_t; PRODUCT_IDPRODUCT_DESCRIPTION 1End Table 2Coffee Table 3Computer Desk 4Entertainment Center 5Writers Desk 68-Drawer Dresser 7Dining Table 8Computer Desk

8 Column Heading Defaults Default justification  Left: Date and character data  Right: Numeric data Default display: Uppercase

9 Arithmetic Expressions Create expressions on NUMBER and DATE data by using arithmetic operators. Operator + - * / Description Add Subtract Multiply Divide

10 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+300 FROMproduct_t; STANDARD_PRICE 175 200 375 650 325 750 850 250

11 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. ** //++__

12 Operator Precedence SQL> SELECT product_description, standard_price, 10*standard_price+300 FROMproduct_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

13 Using Parentheses SQL> SELECT product_description, standard_price, 10*(standard_price+300) FROMproduct_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

14 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.

15 Null Values in Arithmetic Expressions Arithmetic expressions containing a null value evaluate to null.

16 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

17 Using Column Aliases CUSTOMERADDRESS Contemporary Casuals1355 S Hines Blvd Value Furniture15145 S. W. 17th St. Home Furnishings1900 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 NameStreet Address Contemporary Casuals1355 S Hines Blvd Value Furniture15145 S. W. 17th St. Home Furnishings1900 Allard Ave

18 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

19 Using the Concatenation Operator SQL> SELECTCONCAT(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

20 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.

21 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

22 Duplicate Rows The default display of queries is all rows, including duplicate rows. SELECT product_line_id FROM product_t; SELECT product_line_id FROM product_t; PRODUCT_LINE_ID 10001 20001 30001 10001 20001 30001

23 Eliminating Duplicate Rows Eliminate duplicate rows using the DISTINCT keyword in the SELECT clause SELECT DISTINCT product_line_id FROM product_t; PRODUCT_LINE_ID 10001 20001 30001


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

Similar presentations


Ads by Google