chapter 8 restricting rows and sorting data (from a single table)

47
Dr. Chen, Oracle Database System (Oracle) 1 HW Discussion Chapter 5 (#9) Why need two “deletes”? SQL> --9. SQL> SQL> DELETE FROM orderitems 2 WHERE order# = 1005; 1 row deleted. SQL> SQL> DELETE FROM orders 2 WHERE order# = 1005; 1 row deleted. Order# Customer # OrderDat e ShipDat e ShipStree t ShipCity ShipState ShipZip ShipCost NUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15 ) VARCHAR2(2) NUMBER(4) NUMBER(4,2) Order# Item# ISBN Quantity PaidEach NUMBER(4) NUMBER(2) VARCHAR2(10 ) NUMBER(3) NUMBER(5,2) ORDERS pk ORDERITEMS fk cpk

Upload: shelby

Post on 22-Feb-2016

105 views

Category:

Documents


1 download

DESCRIPTION

Chapter 8 Restricting Rows and Sorting Data (from a Single Table). Jason C. H. Chen , Ph.D. Professor of MIS School of Business Gonzaga University Spokane, WA 99258 USA [email protected]. Database Development. ORACLE (SQL Components). DDL. JL_D.B. DML. DCL. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 1

HW Discussion• Chapter 5 (#9)

– Why need two “deletes”?SQL> --9.SQL> SQL> DELETE FROM orderitems 2 WHERE order# = 1005;

1 row deleted.

SQL> SQL> DELETE FROM orders 2 WHERE order# = 1005;

1 row deleted.

Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCost

NUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)

Order# Item# ISBN Quantity PaidEach

NUMBER(4) NUMBER(2) VARCHAR2(10) NUMBER(3) NUMBER(5,2)

ORDERSpk

ORDERITEMS

fk

cpk

Page 2: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 2

Chapter 8Restricting Rows and Sorting

Data (from a Single Table)

Jason C. H. Chen, Ph.D.Professor of MIS

School of BusinessGonzaga University

Spokane, WA 99258 [email protected]

Page 3: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 3

Database Development

DDL

DML

DCL

JL_D.B.

ORACLE(SQL Components)

(Retrieve Data and Produce Information)

Page 4: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 4

Objectives

• Use a WHERE clause to restrict the rows returned by a query

• Create a search condition using mathematical comparison operators

• Use the BETWEEN…AND comparison operator to identify records within a range of values

• Specify a list of values for a search condition using the IN comparison operator

Page 5: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 5

Objectives (continued)

• Search for patterns using the LIKE comparison operator

• Identify the purpose of the % and _ wildcard characters

• Join multiple search conditions using the appropriate logical operator

• Perform searches for NULL values• Specify the order for the presentation of query

results using an ORDER BY clause• Extra GROUP BY and HAVING examples are

also introduced.

Page 6: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 6

Refresh the Database

• Run the following script file– Start c:\oradata\chapter8\JLDB_Build_8.sql

• Note that we will first study “Logical Operator” follows by “Comparison Operator”.

Page 7: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 7

WHERE Clause Syntax• A WHERE clause is used to retrieve rows based on a stated

condition• Requires:

– Column name– Comparison operator– Value or column for comparison

• Values are case sensitive (within ‘…’)

Figure 8-1 Syntax of the SELECT statement

SELECT [DISTINCT | UNIQUE] (*, field-1 [As alias], field-2, …)FROM tablename[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY columname];

Page 8: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 8

MORE ON SELECT

Figure 8-1 Syntax of the SELECT statement

SELECT [DISTINCT | UNIQUE] (*, field-1 [As alias], field-2, …)FROM tablename-1, tablename-2, …[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY columname];

SELECT [DISTINCT | UNIQUE] (*, field-1 [As alias], field-2, …)FROM tablename[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY columname];

What is the difference between these two

‘SELECT”

Single TableM

ultiple Tables

Page 9: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 9

WHERE Clause Example

• List WHERE clause after FROM clause• Enclose nonnumeric data in single quotes

Figure 8-3 Results of a state-based search

Page 10: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 10

Rules for Character Strings-- chapter 8, Figure 8-4; p.259SELECT *FROM customersWHERE customer# = 1010;

-- chapter 8, Figure 8-5; p.259SELECT *FROM booksWHERE isbn = 1915762492;

-- chapter 8, Figure 8-6; p.260SELECT *FROM booksWHERE pubdate = '21-JAN-05';

--******************************-- chapter 8, Figure 8-5(b); p.259SELECT *FROM booksWHERE isbn = '1915762492';

Page 11: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 11

Logical Operators

• Used to combine conditions• Evaluated in order of NOT, AND, OR

– NOT – reverses meaning– AND – both conditions must be TRUE– OR – at least one condition must be TRUE

Page 12: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 12

AND Logical Operator Example

Figure 8-24 Searching with multiple conditions and the AND logical operator

Page 13: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 13

OR Logical Operator Example

Figure 8-25 Searching with multiple conditions and the OR logical operator

Page 14: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 14

Multiple Logical Operators

• Resolved in order of NOT, AND, OR

Figure 8-26 Searching with both AND and OR operators (NOT A GOOD WAY – why? See next example)

Page 15: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 15

Multiple Logical Operators

• Use parentheses to override the order of evaluation

Figure 8-27 Using parentheses to control the evaluation order for logical operators

Page 16: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 16

Resolving Multiple Types of Operators

1. Arithmetic operators2. Comparison operators3. Logical operators

Page 17: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 17

Comparison Operators

• Indicate how the data should relate to the given search value

Figure 8-7 Searching for books with a retail price greater than $55

-- chapter 8, Figure 8-8; p.262SELECT titleFROM booksWHERE title > 'HO';

Page 18: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 18

Arithmetic Comparison Operators

Table 8-2 Comparison Operators

Page 19: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 19

Other Comparison Operators

Table 8-2 Comparison Operators (cont.)

Page 20: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 20

Practice …

• Figure 8-9 to Figure 8-13 (pp.263-266)-- chapter 8, Figure 8-9; p.263SELECT title, retail-cost profitFROM booksWHERE retail-cost < cost*.2;

-- chapter 8, Figure 8-10; p.264SELECT firstname, lastname, stateFROM customersWHERE state <= 'GA';

-- chapter 8, Figure 8-11; p.265SELECT firstname, lastname, stateFROM customersWHERE state >= 'GA';

-- chapter 8, Figure 8-12; p.266SELECT firstname, lastname, stateFROM customersWHERE state <> 'GA';

-- chapter 8, Figure 8-13; p.266SELECT order#, orderdateFROM ordersWHERE orderdate < '01-APR-09';

What is the interpretation on this SQL command?

Page 21: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 21

BETWEEN…AND Operator

• Finds values in a specified range

Figure 8-14 Searching Pubid with the BETWEEN … AND operator

Page 22: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 22

-- chapter 8, Figure 8-15(c); p.268SELECT titleFROM booksWHERE title BETWEEN ‘B' AND ‘H';

TITLE------------------------------BODYBUILD IN 10 MINUTES A DAYBUILDING A CAR WITH TOOTHPICKSDATABASE IMPLEMENTATIONCOOKING WITH MUSHROOMSE-BUSINESS THE EASY WAYBIG BEAR AND LITTLE DOVE

6 rows selected.

exclusive

TITLE------------------------------BODYBUILD IN 10 MINUTES A DAYREVENGE OF MICKEYBUILDING A CAR WITH TOOTHPICKSDATABASE IMPLEMENTATIONCOOKING WITH MUSHROOMSHOLY GRAIL OF ORACLEHANDCRANKED COMPUTERSE-BUSINESS THE EASY WAYPAINLESS CHILD-REARINGTHE WOK WAY TO COOKBIG BEAR AND LITTLE DOVEHOW TO GET FASTER PIZZAHOW TO MANAGE THE MANAGERSHORTEST POEMS

What is the output?How many records?

inclusive

-- chapter 8, Figure 8-15; p.268SELECT titleFROM books

SELECT titleFROM booksWHERE title >= ‘B' AND title <=‘H';

Page 23: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 23

-- chapter 8, Figure 8-15(d); p.268SELECT titleFROM booksWHERE title BETWEEN 'B' AND 'HO';

TITLE------------------------------BODYBUILD IN 10 MINUTES A DAYBUILDING A CAR WITH TOOTHPICKSDATABASE IMPLEMENTATIONCOOKING WITH MUSHROOMSHANDCRANKED COMPUTERSE-BUSINESS THE EASY WAYBIG BEAR AND LITTLE DOVE

7 rows selected.

TITLE------------------------------BODYBUILD IN 10 MINUTES A DAYREVENGE OF MICKEYBUILDING A CAR WITH TOOTHPICKSDATABASE IMPLEMENTATIONCOOKING WITH MUSHROOMSHOLY GRAIL OF ORACLEHANDCRANKED COMPUTERSE-BUSINESS THE EASY WAYPAINLESS CHILD-REARINGTHE WOK WAY TO COOKBIG BEAR AND LITTLE DOVEHOW TO GET FASTER PIZZAHOW TO MANAGE THE MANAGERSHORTEST POEMS

-- chapter 8, Figure 8-15; p.268SELECT titleFROM books

SELECT titleFROM booksWHERE title >= 'B' AND title <='HO';

What is the output?How many records?

Page 24: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 24

SELECT orderdate FROM orders WHERE orderdate BETWEEN '31-Mar-09' AND '02-Apr-09’;

ORDERDATE---------31-MAR-0931-MAR-0931-MAR-0901-APR-0901-APR-0901-APR-0901-APR-0902-APR-0902-APR-09

inclusive

SELECT * FROM EmployeeAddressTable WHERE LastName BETWEEN 'Ackerman' AND 'Scott‘;

Retrieve employees alphabetically between (and including) "Ackerman" and

exclusive "Scott".

Page 25: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 25

IN Operator and Example

• Returns records that match a value in a specified list

• List must be in parentheses

• Values are separated by commas

• What logical operator can be used to replace IN?

Figure 8-16 Searching Pubid with the IN operator

Page 26: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 26

Other Examples (IN, NOT IN)-- chapter 8, Figure 8-17; p.269SELECT firstname, lastname, stateFROM customersWHERE state IN ('CA', 'TX');

-- chapter 8, Figure 8-18; p.270SELECT firstname, lastname, stateFROM customersWHERE state NOT IN ('CA', 'TX');

-- chapter 8, Figure 8-17(b) an alternate waySELECT firstname, lastname, stateFROM customersWHERE state = 'CA‘ state = 'TX';OR

Page 27: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 27

LIKE Operator• Performs pattern searches• Used with wildcard characters

– Underscore (_) for exactly one character in the indicated position

– Percent sign (%) represents any number of characters

Figure 8-19 Searching with the LIKE operator and the % wildcard character

Page 28: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 28

More Examples

-- chapter 8, Figure 8-20; p.271SELECT customer#, lastname, firstnameFROM customersWHERE customer# LIKE '10_9';

-- chapter 8, Figure 8-21; p.272SELECT isbn, titleFROM booksWHERE isbn LIKE '_4%0';

-- chapter 8, Figure 8-22; p.273SELECT *FROM testing;

Page 29: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 29

Treatment of NULL Values• Absence of data• Requires use of IS NULL operator

Figure 8-28 Searching for NULL values with the IS NULL operator

-- chapter 8, Figure 8-29; p.278SELECT order#, shipdateFROM ordersWHERE shipdate IS NOT NUL

Page 30: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 30

Treatment of NULL Values (continued)

• A common error is using = NULL, which does not raise an Oracle error but also does not return any rows

Figure 8-30 Using the = NULL operator by mistake

Page 31: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 31

An extra table for GROUP BY and HAVING

loc_id bldg_code room capacityNUMBER(6), VARCHAR2(10) VARCHAR2(6) NUMBER(5)

location

CREATE TABLE location(loc_id NUMBER(6), bldg_code VARCHAR2(10), room VARCHAR2(6), capacity NUMBER(5), CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id));

You need to download (create a new folder of \NW_CW\) and run the following command to make the example work:@ c:\oradata\NW_CW\northwoods.sql

Page 32: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 32

GROUP BY Clauses – Extra ExamplesThe group by clause is used to form groups of rows of a resulting table based on column clauses. When the group by clause is used, all aggregate operations are computed on the individual groups, not on the entire table.

--Figure G-0 (wrong version)-- NOT a group by expression: -- ROOMSELECT bldg_code, room, SUM(capacity),AVG(capacity), MAX(capacity)FROM locationWHERE capacity >= 5GROUP BY bldg_code;

--Figure G-1-- right version of group bySELECT bldg_code, SUM(capacity),AVG(capacity), MAX(capacity)FROM locationWHERE capacity >= 5GROUP BY bldg_code;

Output from Figure G-1BLDG_CODE SUM(CAPACITY) AVG(CAPACITY) MAX(CAPACITY)---------- ------------- ------------- -------------BUS 167 41.75 55CR 260 65 150

Page 33: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 33

GROUP BY Clauses – Extra ExamplesThe group by clause is used to form groups of rows of a resulting table based on column clauses. When the group by clause is used, all aggregate operations are computed on the individual groups, not on the entire table.

--Figure G-0-- NOT a group by expression: ROOMSELECT bldg_code, room, SUM(capacity),AVG(capacity), MAX(capacity)FROM locationWHERE capacity >= 5GROUP BY bldg_code;

--Figure G-1-- right version of group bySELECT bldg_code, SUM(capacity),AVG(capacity), MAX(capacity)FROM locationWHERE capacity >= 5GROUP BY bldg_code;

Output from Figure G-1BLDG_CODE SUM(CAPACITY) AVG(CAPACITY) MAX(CAPACITY)---------- ------------- ------------- -------------BUS 167 41.75 55CR 260 65 150

Page 34: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 34

--Figure G-1-- the right version of group bySELECT bldg_code, SUM(capacity),AVG(capacity), MAX(capacity)FROM locationWHERE capacity >= 5GROUP BY bldg_code;

--Figure G-2-- the right version of group by w/ havingSELECT bldg_code, SUM(capacity) TOTAL_CAPACITY,AVG(capacity), MAX(capacity)FROM locationWHERE capacity >= 5GROUP BY bldg_codeHAVING AVG(capacity) >=42;

Output from Figure G-1BLDG_CODE SUM(CAPACITY) AVG(CAPACITY) MAX(CAPACITY)---------- ------------- ------------- -------------BUS 167 41.75 55CR 260 65 150

Output from Figure G-2BLDG_CODE TOTAL_CAPACITY AVG(CAPACITY) MAX(CAPACITY)---------- ------------- ------------- ------------- CR 260 65 150

The Group by and Having Clauses

The having clause is used to eliminate certain groups from further consideration. The following query will produce the same results but with the AVG capacity of at least 42.

Page 35: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 35

ORDER BY Clause Syntax

• The ORDER BY clause presents data in sorted order

• Ascending order is default• Use DESC keyword to override column default • 255 columns maximum

Figure 8-31 Syntax of the SELECT statement

Page 36: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 36

ORDER BY Clause Syntax Sort Sequence

• In ascending order, values will be listed in the following sequence:– Numeric values– Character values– NULL values

• In descending order, sequence is reversed

Page 37: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 37

Examples on ORDER BY

Figure 8-32, 34 Sorting results by publisher name in ascending and descending orders

Page 38: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 38

Figure 8-36 The default sort order for NULL values Figure 8-37 Using the NULL FIRST option in the ORDER BY clause

Page 39: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 39

Secondary Sort

Figure 8-38 Using primary and secondary sort columns

In the previous examples, only one column was specified in the ORDER BY clause, which is called a primary sort. In some cases, you might want to include a secondary sort, which specifies a second filed to sort b if an exact match occurs between two or more rows in the primary sort.

Page 40: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 40

ORDER BY Example

Figure 8-39 Sorting on the State and City columns

Page 41: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 41

ORDER BY Can Reference Column Position

Figure 8-40 Referencing positions of sort columns in the ORDER BY clause

Page 42: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 42

• Practice all the examples in the text.• A Script file is available on the Bb (file

name: Ch8Queries.sql)• After completing all examples, do the HW.

Page 43: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 43

Homework - Hands-On Assignments

Read and Practice all examples on Chapters 8• 1. Run the script files (in the folder \oradata\

chapter8\): JLDB_Build_8.sql• 2. Read Oracle assignment and create a script file

Oracle_ch8_Lname_Fname.sql for questions (all EVEN numbers; p.292) on “Hands-on Assignments”. Use appropriate COLUMN statements to produce readable outputs if needed.

• 3. Execute and test one problem at a time and make sure they are all running successfully.

• 4. When you done, spool the script files (see next slide for spooling instructions) and UPLOAD the source and spooled files (Oracle_ch8_Spool_Lname_Fname.txt) to Bb by the midnight Sunday.

• 5. Turn in a hardcopy (*.txt) next class.

Page 44: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 44

How to Spool your Script and Output FilesAfter you tested the script file of Oracle_ch6_Lname_Fname.sql successfully,

follow the instructions below to spool both script and output files:Step 0. Run the following script file from SQL*Plus (since you have created

JLDB tables)– Start c:\oradata\chapter6\JLDB_Build_6.sql

• 1. type the following on SQL>– Spool c:\oradata\Oracle_ch6_Spool_Lname_Fname.txt (make sure your name is entered)

• 2. open Oracle_ch6_Lname_Fname.sql that you already tested• 3. copy and paste all the SQL commands (including all comments) to the

SQL*PLUS • 4. type Spool Off on the SQL>The output should contain your personal information, all SQL commands and

their solution on the .txt file and saved in C: drive (oradata\ folder)

Upload the spooled file (*.txt) to the Bb (under “Assignments & Projects”) by the deadline.

Page 45: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 45

Summary

• The WHERE clause can be included in a SELECT statement to restrict the rows returned by a query to only those meeting a specified condition

• When searching a nonnumeric field, the search values must be enclosed in single quotation marks

• Comparison operators are used to indicate how the record should relate to the search value

• The BETWEEN...AND comparison operator is used to search for records that fall within a certain range of values

Page 46: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 46

Summary (continued)

• The LIKE comparison operator is used with the percent and underscore symbols (% and _) to establish search patterns

• Logical operators such as AND and OR can be used to combine several search conditions

• When using the AND operator, all conditions must be TRUE for a record to be returned in the results– However, with the OR operator, only one condition

must be TRUE• A NULL value is the absence of data, not a field

with a blank space entered

Page 47: Chapter 8 Restricting Rows and Sorting Data (from a Single Table)

Dr. Chen, Oracle Database System (Oracle) 47

Summary (continued)• Use the IS NULL comparison operator to match

NULL values; the IS NOT NULL comparison operator finds records that do not contain NULL values in the indicated column

• You can sort the results of queries by using an ORDER BY clause; when used, the ORDER BY clause should be listed last in the SELECT statement

• By default, records are sorted in ascending order; entering DESC directly after the column name sorts the records in descending order

• A column does not have to be listed in the SELECT clause to serve as a basis for sorting