Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 4 - Sorting/Functions

Similar presentations


Presentation on theme: "Section 4 - Sorting/Functions"— Presentation transcript:

1 Section 4 - Sorting/Functions
You can Sort the results of your SQL Statements You can eliminate duplicate result rows You can use aggregate functions to get summary information about the data 1

2 The Order By Clause The Order By clause lets you sort result lists by any column(s) or expression Sorts can be in Ascending or Descending order 2

3 Example - No Sort SELECT price, title, pub_id FROM titles;
Results do not come back in a meaningful order. (They come back in the order they were entered in the database) 3

4 Example - Sorted SELECT price, title, pub_id FROM titles ORDER BY price; Results are in lowest Price to highest Price order 4

5 Order By Syntax SELECT [ALL | DISTINCT] select_list
FROM {table_name | view_name} [,{table_name | view_name}]... [WHERE search_conditions] [ORDER BY {column_name | select_list_number} [ASC |DESC] [{,column_name | select_list_number [ASC|DESC]]...]} 5

6 Nested Sorts Now that the books are in price order, you might want them sorted in publisher order within price. SELECT price, title, pub_id FROM titles ORDER BY price, pub_id; 6

7 Column Order ‘Order by’ columns do not have to be in the same order as the columns in the Select-list SELECT price, pub_id, title FROM titles ORDER BY pub_id, price; 7

8 Sort Order Ascending or Descending ASC for ascending
DESC for ascending Ascending is the default - You do not have to specify the ASC 8

9 Sorting Expression Sort by the position that a column appears in the select list SELECT price * 2, title_id FROM titles ORDER BY 1; Sort by Heading SELECT price * 2 "COST", title_id FROM titles ORDER BY cost; 9

10 SORTING & NULLs NULLs will either sort before or after everything depending on the collating sequence of your computer. Using ASC or DESC makes no difference! The Nulls will always float to the top or bottom 10

11 Exercise List the prices and the doubled advances for each book in the database. Put in doubled advances order showing the largest advance first. Sort each book title in alphabetical order within an advance group. Eliminate any books from the list where the cost or advance group is unknown. 11

12 Discussion First the select list... SELECT price, 2 * advance FROM titles; The add the advance group sort SELECT price, 2 * advance FROM titles ORDER BY 2;

13 Discussion Then add the the title sort: SELECT price, 2 * advance, title FROM titles ORDER BY 2, title; Now add the elimination of NULLS: SELECT price, 2 * advance, title FROM titles WHERE NOT (price IS NULL OR advance IS NULL) ORDER BY 2, title;

14 The DISTINCT Clause ALL vs. DISTINCT
ALL returns all the result rows [ALL is the default so you don't have to specify it] DISTINCT returns only the unique rows SELECT ALL au_id FROM titleauthors; SELECT DISTINCT au_id FROM titleauthors; 12

15 DISTINCT Applies to All Columns
The DISTINCT applies to all columns in the Select list, try out these statements SELECT DISTINCT pub_id FROM titles ORDER BY pub_id; SELECT DISTINCT type FROM titles ORDER BY type; SELECT DISTINCT pub_id, type FROM titles ORDER BY pub_id, type; 13

16 DISTINCT & NULLs NULL values are unknown and are never equal to each other, but...... For the DISTINCT clause they are presented as one NULL group Try this: SELECT DISTINCT advance FROM titles; 14

17 Exercise List the publisher id, the title, and the type. Sort in publisher id order. 15

18 Answer SELECT pub_id, title, type FROM titles ORDER BY pub_id;

19 Exercise Show the unique publisher ids, unique titles, and unique types.

20 Discussion You need three separate Select statements
SELECT DISTINCT pub_id FROM titles; SELECT DISTINCT title FROM titles; SELECT DISTINCT types FROM titles;

21 Using Aggregate Functions
Aggregate functions are used to present summary information They apply to sets of rows The sets can be all rows in a table or groups of rows created with a GROUP BY clause (this will be covered in the next section) 16

22 Single Value Returned Aggregate functions always return a single value for each set of rows. Try these statements: SELECT ytd_sales FROM titles; SELECT SUM(ytd_sales) FROM titles; 17

23 Listing Aggregates with Columns
In the select list, you cannot mix columns using aggregate functions with columns that do not use aggregate functions. Try this: SELECT price, sum(price) FROM titles; 18

24 AGGREGATE Syntax SELECT aggregate_function( [DISTINCT] expression FROM <table list>; 19

25 Aggregate Functions Listed
SUM Total value of numeric expressions AVG Average value of numeric expressions COUNT Number of Non-NULL values COUNT(*) - Number of selected rows MAX The highest value of expression MIN The lowest value of expression 20

26 Distinctions DISTINCT may be used within any of the aggregate functions except COUNT(*) But it is not useful with the MIN or MAX functions 21

27 Examples SELECT AVG(price * 2) FROM titles;
SELECT COUNT(price) FROM titles; SELECT COUNT(*) FROM titles; 22

28 Aggregates & Datatypes
SUM & AVG can be used with Numeric data types only MIN, MAX, COUNT, and COUNT(*) work with all datatypes SELECT MIN(au_lname) FROM authors; 23

29 DISTINCT Aggregates Most useful with SUM, AVG, & COUNT
Eliminates duplicate values before calculating functions. SELECT COUNT(price) FROM titles; SELECT COUNT(DISTINCT price) FROM titles; 24

30 WHERE & Aggregates Cannot Use Aggregate Functions in WHERE Clause
Can use the WHERE clause to restrict the rows for the Aggregate calculation 25

31 EXAMPLES SELECT AVG(price), SUM(ytd_sales) FROM titles;
SELECT AVG(price), SUM(ytd_sales) FROM titles WHERE type = 'business'; 26

32 NULL Values & Aggregates
NULL values are ignored for the Aggregate EXCEPTION: The COUNT(*) which always returns all rows for the query 27

33 Return Values The COUNT function returns zero if no rows are found
ALL other functions return NULL SELECT COUNT(DISTINCT title) FROM titles WHERE type = 'poetry'; SELECT AVG(advance) FROM titles WHERE type = 'poetry'; 28

34 Exercise Show the number of books, their average price, and total year-to-date sales for all books in the titles table 29

35 Discussion SELECT COUNT(title), AVG(price), SUM(ytd_sales) FROM titles;

36 Exercise Find the smallest number of authors that would have to collaborate on a book to be the book with the most number of authors collaborating on it.

37 Discussion First find the largest number of authors we have now SELECT Max(au_ord) FROM titleauthors;

38 Discussion So the highest number of authors collaborating on a book is 3 The smallest number greater than that is 4 SELECT MAX(au_ord) + 1 FROM titleauthors;

39 Section 4 - Last Slide Please complete assignment 3 31


Download ppt "Section 4 - Sorting/Functions"

Similar presentations


Ads by Google