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 ALL the default returns all qualified rows DISTINCT 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. 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 Grouping data Grouping data allows us to view aggregated data in reference to a field of interest. This makes for a powerful reporting tool.

11 GROUP BY clause Groups results by column name used with aggregate functions

12 Syntax SELECT col_1, max(col_2) FROM table GROUP BY col_1 column 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.

13 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

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

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

16 Formatting Text getDate() - returns today’s date SELECT getDate() char_length(data) - returns the length SELECT char_length(‘John’) upper(data) - returns the uppercase lower(data) - returns the lowercase concatonation 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