Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.

Similar presentations


Presentation on theme: "Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw."— Presentation transcript:

1

2 Sorting data and Other selection Techniques

3 Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw data, we can sort on one or many different criteria. This will allow us and/or the user to focus on the items of interest.

4 ORDER BY clause Simplest form: SELECT column FROM table ORDER BY column

5 Nested Ordering Ordering can occur on a number of levels. When you ORDER BY on more than one column the first column takes highest precedence and goes down the line. Example: SELECT author, price, title FROM books ORDER BY price, title, author The results would be ordered by price, then title and then author… in ascending order. Ascending order is the default.

6 Sort order We can change sort order using the DESC (for descending) and ASC (for ascending) keywords. Example: SELECT author, title, price FROM books ORDER BY price desc, author Note: author would still be sorted ascending. DESC only affects the price column.

7 Ordering Expressions When we want to order expressions we need to refer to the expression by its alias or its position. Example: SELECT price *.9 disc, author, title FROM books ORDER BY disc, title or ODER BY 1, title

8 Eliminating Duplicates ALLALL –the default –returns all qualified rows DISTINCTDISTINCT –returns only unique –when more than 1 item in SELECT list, returns only the unique combinations. Both can only occur once and only at the beginning of the SELCT list.Both can only occur once and only at the beginning of the SELCT list. SELECT DISTINCT book_store FROM stores

9 Non-select list Order By On systems that allow it, having a column listed in the ORDER BY and not in the SELECT list broadens the scope of the query to include that column as well. The column is NOT displayed in the results. Only the affected rows.

10 Aggregate Functions SUM ([DISTINCT] expr) The total sum of values in expression AVG ([DISTINCT] expr) The average of values in the expression COUNT ([DISTINCT] expr) The number of non-null values in the expression MAX (expr) The highest value in the expression MIN (expr) The lowest value in the expression

11 Syntax SELECT avg(price * 2) from titles returns the average of all prices * 2returns the average of all prices * 2 SELECT sum (price) from titles return the sum of the pricesreturn the sum of the prices

12 Grouping data Grouping data allows us to view aggregated data in reference to a field of interest. This makes for a powerful reporting tool.

13 GROUP BY clause Groups results by column nameGroups results by column name used with aggregate functionsused with aggregate functions

14 Syntax SELECT col_1, max(col_2) FROM table GROUP BY col_1 column in GROUP BY must be in SELECT listcolumn in GROUP BY must be in SELECT list There can be multiple columns in GROUP BY. Where their list order is their order of precedence.There can be multiple columns in GROUP BY. Where their list order is their order of precedence.

15 Limitations There must be only 1 value returned for each GROUP BY field. Should only use column names Due to these limitations, many vendors provide report generators

16 Grouping and Nulls Nulls are assigned a group of there own since they are, in a sense, their own datatype.

17 HAVING clause Same as WHERE clause except it allows aggregate functions

18 Formatting Text getDate() - returns today’s dategetDate() - returns today’s date SELECT getDate() char_length(data) - returns the lengthchar_length(data) - returns the length SELECT char_length(‘John’) upper(data) - returns the uppercaseupper(data) - returns the uppercase lower(data) - returns the lowercaselower(data) - returns the lowercase concatonationconcatonation –SELECT (name +` `+address+`,`+state)


Download ppt "Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw."

Similar presentations


Ads by Google