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.

Slides:



Advertisements
Similar presentations
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Advertisements

4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
Chapter 11 Group Functions (up to p.402)
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Oracle9i: SQL1 SQL Group Functions.
Structured Query Language Part I Chapter Three CIS 218.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Computer Science 101 Web Access to Databases SQL – Extended Form.
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.
1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.
SQL – Logical Operators and aggregation Chapter 3.2 V3.0 Napier University Dr Gordon Russell.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool.
INLS 623– S QL Instructor: Jason Carter. SQL SELECT DISTINCT SELECT DISTINCT column_name, column_name FROM table_name ;
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Grouping Data. GROUP BY clause Groups results by column name used with aggregate functions must come first when used with ORDER BY.
Copyright © Curt Hill Queries in SQL More options.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
SQL – Simple Queries and JOIN MGMT 360 Database Management.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Advanced Select Statements Select List Variations SELECT *SELECT * Column NamingColumn Naming Arithmetic ExpressionsArithmetic Expressions ConstantsConstants.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL SELECT Statements Lecture
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
SQL Query Getting to the data ……..
Structured Query Language
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Basic select statement
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Aggregating Data Using Group Functions
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Access: SQL Participation Project
Writing Basic SQL SELECT Statements
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Group Operations Part IV.
Presentation transcript:

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 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.

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

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.

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.

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

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

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.

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

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

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

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

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.

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

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

HAVING clause Same as WHERE clause except it allows aggregate functions

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)