Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
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
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Functions.
Structured Query Language Part I Chapter Three CIS 218.
Microsoft Access 2010 Chapter 7 Using SQL.
Computer Science 101 Web Access to Databases SQL – Extended Form.
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 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 5: Functions.
CpSc 3220 The Language of SQL Chapters 4-6. Using Functions Function types – Character (string) – Numeric – Date and Time – Summary (aggregate)
 Continue queries ◦ You completed two tutorials with step-by-step instructions for creating queries in MS Access. ◦ Now must apply knowledge and skills.
Using Special Operators (LIKE and IN)
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
 Industry standard for interacting with relational databases  Statements to Enter, Retrieve, Modify, Remove & Display data stored in database tables.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Structured Query Language SQL Unit 2 An Introduction to Organizing and Retrieving Data with SQL.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
Introduction to Functions – Single Row Functions.
Access Queries Agenda 6/16/14 Review Access Project Part 1, answer questions Discuss queries: Turning data stored in a database into information for decision.
IFS180 Intro. to Data Management Chapter 13 – Grouping Data.
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.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Lab 13 Databases and SQL.
MYSQL DEFINITION MySQL, pronounced either "My S-Q-L" or "My Sequel," is an open source relational database management system. It is based on the structure.
Aggregating Data Using Group Functions
Tarik Booker California State University, Los Angeles October 21, 2014
Chapter 9 SQL and RDBMS Part C. SQL
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
(SQL) Aggregating Data Using Group Functions
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Chapter 11 Functions and Groups
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Aggregating Data Using Group Functions
SQL – Entire Select.
Built in Functions Massaging the data.
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Access: SQL Participation Project
SQL Aggregation.
CS122 Using Relational Databases and SQL
Reporting Aggregated Data Using the Group Functions
Query 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:

Chapter 11 Functions and Groups Part C. SQL Copyright 2005 Radian Publishing Co.

2/30 Contents Chapter 11 Functions and Groups 11.1 Using Functions in Queries 11.1 A. String Functions 11.1 B. Numeric Functions 11.1 C. Date Functions 11.1 D. Logical Functions 11.2 Aggregate Functions 11.2 A. The Function COUNT() 11.2 B. The Function SUM() 11.2 C. The Function AVG() 11.2 D. The Function MAX() 11.2 E. The Function MIN() 11.3 Group 11.3 A. Group and Aggregate Functions11.3 A. Group and Aggregate Functions 11.3 B. Grouping by Multiple Columns11.3 B. Grouping by Multiple Columns 11.3 C. Applying Filtering before Grouping11.3 C. Applying Filtering before Grouping 11.3 D. The HAVING clause 11.3 E. Combing HAVING and WHERE11.3 E. Combing HAVING and WHERE 11.3 F. Grouping by Non-column Names11.3 F. Grouping by Non-column Names 11.3 G. Using GROUP BY with ORDER BY11.3 G. Using GROUP BY with ORDER BY

Copyright 2005 Radian Publishing Co.3/30 Chapter 11 Functions and Groups We shall make use of the functions available in your DBMS to give more varied results from a database. A function accepts one or more input data through the arguments and returns a result (see textbook Appendix D).

Copyright 2005 Radian Publishing Co.4/ Using Functions in Queries The built-in functions available from DBMS are categorised into four main classes: String Numeric Date Logical (see Appendix D on the Textbook for more information)

Copyright 2005 Radian Publishing Co.5/ A. String Functions Common string functions include: LENGTH(String) UPPER(String), LOWER(String) LTRIM(String), RTRIM(String), TRIM(String), LEFT(String, n), RIGHT(String, n), MID(String, n, m), INSTR(String, SubStr) ASCII(String) CHAR(String) CONCAT(String1, String2,...), etc.

Copyright 2005 Radian Publishing Co.6/ B. Numeric Functions Common numeric functions include: ABS(X) SQRT(X) ROUND(X,D), TRUNCATE(X, 0) MOD(N,M) POW(X,Y) RAND() FORMAT(X, D), etc.

Copyright 2005 Radian Publishing Co.7/ C. Date Functions Common date functions include: CURDATE() DAY(Date), MONTH(Date), YEAR(Date) DAYOFWEEK(Date) DATE_ADD(Date) DAYNAME(Date), MONTHNAME(Date), etc. MS Access does not allow reserved words, like DAY, MONTH and YEAR, as column aliases.

Copyright 2005 Radian Publishing Co.8/ D. Logical Functions (1/3) The logical function is: IF(expr1, expr2, expr3). If expr1 is TRUE then IF() returns expr2, else it returns expr3.

Copyright 2005 Radian Publishing Co.9/ D. Logical Functions (2/3)

Copyright 2005 Radian Publishing Co.10/ D. Logical Functions (3/3) Note MS Access does not accept alias in the ORDER BY clause. Therefore, the expression IIF(…) has to be repeated. For Visual FoxPro, alias must be used in the ORDER BY clause

Copyright 2005 Radian Publishing Co.11/ Aggregate Functions Aggregate functions return a value based on multiple values in a column. Examples are: COUNT() SUM() AVG() MAX() MIN()

Copyright 2005 Radian Publishing Co.12/ A. The Function COUNT() (1/2) COUNT() returns the number of rows retrieved by a SELECT statement. Usually, it is used as COUNT(*). It does not make sense to mix an aggregate function with unrelated fields.

Copyright 2005 Radian Publishing Co.13/ A. The Function COUNT() (2/2)

Copyright 2005 Radian Publishing Co.14/ B. The Function SUM() SUM() adds the specified fields in a column and returns the sum.

Copyright 2005 Radian Publishing Co.15/ C. The Function AVG() AVG() function computes the average of a column.

Copyright 2005 Radian Publishing Co.16/ D. The Function MAX() MAX() returns the largest value in a column. The MAX() function can also work on dates and strings.

Copyright 2005 Radian Publishing Co.17/ E. The Function MIN() MIN() works returns the lowest number of a column.

Copyright 2005 Radian Publishing Co.18/ Group (1/2) Records which have the same value in a column can form a group. The syntax of SELECT is now extended to: SELECT [DISTINCT] SelectItems FROM TableName WHERE Conditions GROUP BY GroupColumn1 [,GroupColumn2 ] ORDER BY OrderItem1 [, OrderItem2... ;

Copyright 2005 Radian Publishing Co.19/ Group (2/2)

Copyright 2005 Radian Publishing Co.20/ A. Group and Aggregate Functions The power of GROUP BY is evident when it is used with aggregate functions. The aggregate function returns a value for each group.

Copyright 2005 Radian Publishing Co.21/ B. Grouping by Multiple Columns Similar to ORDER BY, you may put more than one field in the GROUP BY clause.

Copyright 2005 Radian Publishing Co.22/ C. Applying Filtering before Grouping Filtering of records is done by the WHERE clause.

Copyright 2005 Radian Publishing Co.23/ D. The HAVING clause (1/2) HAVING clause is designed to work with aggregate functions or group. The HAVING clause must be placed after GROUP BY and before ORDER BY.

Copyright 2005 Radian Publishing Co.24/ D. The HAVING clause (2/2)

Copyright 2005 Radian Publishing Co.25/ E. Combing HAVING and WHERE (1/2) Don't use HAVING for items that should be in the WHERE clause. WHERE clause filters records and HAVING clause filters groups. In combing the two, the WHERE clause is placed before the HAVING clause and will be performed first.

Copyright 2005 Radian Publishing Co.26/ E. Combing HAVING and WHERE (2/2)

Copyright 2005 Radian Publishing Co.27/ F. Grouping by Non-column Names (1/2) Expressions can be used in the GROUP BY clause.

Copyright 2005 Radian Publishing Co.28/ F. Grouping by Non-column Names (2/2)

Copyright 2005 Radian Publishing Co.29/ G. Using GROUP BY with ORDER BY (1/2) The order of execution is: WHERE, GROUP BY, HAVING, ORDER BY.

Copyright 2005 Radian Publishing Co.30/ G. Using GROUP BY with ORDER BY (2/2)