26
Restricting and Sorting Data

Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Embed Size (px)

Citation preview

Page 1: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Restricting and Sorting Data

Page 2: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Objectives

After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query

Page 3: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Limiting Rows Using a Selection

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

"…retrieve all"…retrieve allproducts with products with product line id product line id

20001"20001"

PRODUCT_ID PRODUCT_DESCRIPTION

PRODUCT_FINISH

STANDARD_PRICE

PRODUCT_LINE_ID

2 Coffee Table Natural Ash 200 20001

3 Computer Desk

Natural Ash 375 20001

6 8-Drawer Dresser

White Ash 750 20001

7 Dining Table Natural Ash 850 20001

Page 4: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Limiting Rows Selected

Restrict the rows returned by using the WHERE clause.

The WHERE clause follows the FROM clause.

SELECT [DISTINCT] {*, column [alias], ...}FROM table[WHERE condition(s)];

Page 5: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the WHERE Clause

SELECT product_description, product_finish, standard_price

FROM product_tWHERE product_finish=‘Natural Ash';

PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE

Coffee Table Natural Ash 200

Computer Desk Natural Ash 375

Dining Table Natural Ash 850

Page 6: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Character Strings and Dates

Character strings and date values are enclosed in single quotation marks

Character values are case-sensitive and date values are format-sensitive

Default date format is 'DD-MON-YY'

SELECT product_description, product_finish, standard_price

FROM product_tWHERE product_finish=‘Cherry';

Page 7: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Comparison Operators

Operator

=

>

>=

<

<=

<>

Meaning

Equal to

Greater than

Greater than or equal to

Less than

Less than or equal to

Not equal to

Page 8: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the Comparison Operators

SELECT product_description, standard_priceFROM product_tWHERE standard_price<=500

PRODUCT_DESCRIPTION STANDARD_PRICE

End Table 175

Coffee Table 200

Computer Desk 375

Writers Desk 325

Computer Desk 250

Page 9: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Other Comparison Operators

Operator

BETWEEN

...AND...

IN(list)

LIKE

IS NULL

Meaning

Between two values (inclusive)

Match any of a list of values

Match a character pattern

Is a null value

Page 10: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the BETWEEN Operator

Use the BETWEEN operator to display rows based on a range of values.

SELECT product_description, standard_priceFROM product_tWHERE standard_price BETWEEN 200 AND 700;

Lowerlimit

Higherlimit

PRODUCT_DESCRIPTION

STANDARD_PRICE

Coffee Table 200

Computer Desk 375

Entertainment Center 650

Writers Desk 325

Computer Desk 250

Page 11: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the IN Operator

Use the IN operator to test for values in a list.

SELECT product_description, product_finish,standard_price

FROM product_tWHERE product_finish IN (‘Cherry’, ‘White Ash’, ‘Oak’);

PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE

End Table Cherry 175

Writers Desk Cherry 325

8-Drawer Dresser White Ash 750

Page 12: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the LIKE Operator

Use the LIKE operator to perform wildcard searches of valid search string values.

Search conditions can contain either literal characters or numbers. % denotes zero or many characters _ denotes one character

SELECT product_descriptionFROM product_tWHERE product_description LIKE ‘E%';

Page 13: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the LIKE Operator

You can combine pattern matching characters.

You can use the ESCAPE identifier to search for "%" or "_".

SELECT product_descriptionFROM product_tWHERE product_description LIKE '_n%';

PRODUCT_DESCRIPTION

End Table

Entertainment Center

Page 14: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the IS NULL Operator

Test for null values with the IS NULL operator

SELECT *FROM product_tWHERE standard_price IS NULL;

PRODUCT_ID PRODUCT_DESCRIPTION

PRODUCT_FINISH STANDARD_PRICE PRODUCT_LINE_ID

9 Corner Table Walnut 10001

Page 15: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Logical Operators

Operator

AND

OR

NOT

Meaning

Returns TRUE if both component

conditions are TRUE

Returns TRUE if either component

condition is TRUE

Returns TRUE if the following condition is FALSE

Page 16: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the AND Operator

AND requires both conditions to be TRUE.

SELECT product_description, product_finish, product_line_id

FROM product_tWHERE product_finish=‘Cherry’AND product_line_id=‘10001';

PRODUCT_DESCRIPTION PRODUCT_FINISH PRODUCT_LINE_ID

End Table Cherry 10001

Writers Desk Cherry 10001

Page 17: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the OR Operator

OR requires either condition to be TRUE.

SELECT product_description, product_finish, product_line_id

FROM product_tWHERE product_finish=‘Cherry’OR product_line_id=‘30001';

PRODUCT_DESCRIPTION PRODUCT_FINISH PRODUCT_LINE_ID

End Table Cherry 10001

Writers Desk Cherry 10001

Entertainment Center Natural Oak 30001

Computer Desk Walnut 30001

Page 18: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Using the NOT Operator

SELECT product_description, product_finishFROM product_tWHERE product_finish NOT IN (‘Cherry',‘Oak’);

PRODUCT_DESCRIPTION PRODUCT_FINISH

Coffee Table Natural Ash

Computer Desk Natural Ash

Entertainment Center Natural Oak

8-Drawer Dresser White Ash

Dining Table Natural Ash

Computer Desk Walnut

Page 19: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Rules of Precedence

Order Evaluated Operator

1 All comparison operators

2 NOT

3 AND

4 OR

Override rules of precedence using parentheses

Page 20: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Rules of Precedence

SELECT product_description, product_finish, standard_priceFROM product_tWHERE product_finish=‘Cherry'OR product_finish=‘Oak'AND standard_price>500;

PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE

End Table Cherry 175

Writers Desk Cherry 325

Page 21: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Rules of Precedence

Use parentheses to force priority

SELECT product_description, product_finish, standard_priceFROM product_tWHERE (product_finish=‘Cherry'OR product_finish=‘Natural Oak‘)AND standard_price>500;

PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE

Entertainment Center Natural Oak 650

Page 22: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

ORDER BY Clause Sort rows with the ORDER BY clause

ASC: ascending order, default DESC: descending order

The ORDER BY clause comes last in the SELECT statement.

SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description;

Page 23: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Sorting in Descending Order

SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description DESC;

PRODUCT_ID PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE PRODUCT_LINE_ID

5 Writers Desk Cherry 325 10001

4 Entertainment Center Natural Oak 650 30001

1 End Table Cherry 175 10001

7 Dining Table Natural Ash 850 20001

9 Corner Table Walnut   10001

3 Computer Desk Natural Ash 375 20001

8 Computer Desk Walnut 250 30001

2 Coffee Table Natural Ash 200 20001

6 8-Drawer Dresser White Ash 750 20001

Page 24: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Sorting by Column Alias

SELECT product_description pd, product_finish pfORDER BY pd;

PD PF

8-Drawer Dresser White Ash

Coffee Table Natural Ash

Computer Desk Natural Ash

Computer Desk Walnut

Corner Table Walnut

Dining Table Natural Ash

End Table Cherry

Entertainment Center Natural Oak

Writers Desk Cherry

Page 25: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Sorting by Multiple Columns The order of ORDER BY list is the order of sort.

You can sort by a column that is not in the SELECT list.

SQL> SELECT * 2 FROM product_t 3 ORDER BY product_description, product_finish;

ID PRODUCT_DESCRIPTION PRODUCT_FINISH STANDARD_PRICE PRODUCT_LINE_ID

6 8-Drawer Dresser White Ash 750 20001

2 Coffee Table Natural Ash 200 20001

3 Computer Desk Natural Ash 375 20001

8 Computer Desk Walnut 250 30001

9 Corner Table Walnut   10001

7 Dining Table Natural Ash 850 20001

1 End Table Cherry 175 10001

4 Entertainment Center Natural Oak 650 30001

5 Writers Desk Cherry 325 10001

Page 26: Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort

Summary

SELECT [DISTINCT] {*, column [alias], ...}FROM table[WHERE condition(s)][ORDER BY {column, expr, alias} [ASC|DESC]];