Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider.

Slides:



Advertisements
Similar presentations
Relational operators 1 Lecture 7 Relational Operators.
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Joining Relations in SQL Objectives of the Lecture : To consider the Natural & Generalised Joins using the SQL1 standard; To consider the Natural & Generalised.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Relational Databases.  So far we have looked at retrieving data from multiple tables and the different ways to join the relations/tables that are required.
Query Design Objectives of the Lecture : To learn a strategy for designing queries. To learn how to use relational algebra concepts to implement the strategy.
LIAL HORNSBY SCHNEIDER
1 1.1 © 2012 Pearson Education, Inc. Linear Equations in Linear Algebra SYSTEMS OF LINEAR EQUATIONS.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.
1.2 – Evaluate and Simplify Algebraic Expressions A numerical expression consists of numbers, operations, and grouping symbols. An expression formed by.
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Prerequisites: Fundamental Concepts of Algebra
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
Dr Gordon Russell, Napier University Unit SQL 2 - V2.0 1 SQL 2 Unit 1.3.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Built-in Data Structures in Python An Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
6. 1 Multiplication with Exponents and Scientific Notation 6
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Relational Algebra Operators
Set Analysis. Agenda 1. What is Set Analysis? 2. Why do we use it? 3. How do we use it (syntax)? 4. Examples.
Normalisation Lecture 3 Akhtar Ali 12/16/ Learning Objectives 1.To consider the process of Normalisation 2.To consider the definition and application.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Week 1 Real Numbers and Their Properties (Section 1.6, 1.7, 1.8)
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Relational Algebra - Part 1
Operators Expression Trees Bag Model of Data
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
CS4222 Principles of Database System
Database Systems: Design, Implementation, and Management Tenth Edition
Objectives of the Lecture :
SQL: Structured Query Language
Decision Making Using the IF and EVALUATE Statements
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider the Extend operator & its implementation in SQL.

Example : Whole Relation as a Group EMPLOYEE GroupBy[ ] With[ Total  Bag[ Sal ] Sum ] Get the total salary bill. Total ,500 33E177Robson 111E3555Smith 22E566Mitchell 444E6888Blake E8Jones ENoEName E2Atkins E4Fenwick E7Watson 66D 222S 44M 888M D M-S M S D Sal 66 32, , , ,000 18,000 24,000 40,000 28,000 EMPLOYEE Sal 66 32, , , ,000 18,000 24,000 40,000 28, No attribute

Whole-Relation ‘GroupBy’ Procedure 1.Remove irrelevant attribute(s). AS USUAL. 2.Add an empty set of attributes to the operand as the grouping attributes. It does not alter the operand, since it is empty ! The empty ‘attribute’ has the same value (i.e. the empty set) in every tuple. Group on this attribute; this yields only one group. 3.Create one result tuple / group consisting of the grouping attribute. Thus there is one tuple initially with no attributes, i.e. an attribute containing the empty set. 4.Carry out aggregations. AS USUAL. This will add aggregation result attribute(s) to the one tuple.

SQL : Whole-Relation Group The SQL syntax for a whole-relation GroupBy is : Select “aggregation” As Result-Name FromRELATION_NAME ; Repeat this for each aggregation. The syntax of “aggregation“ remains unchanged. Example : “Get the total salary bill”. (Previous example). SelectSum( All Sal ) As Total FromEMPLOYEE ; The Group By phrase is omitted - no grouping attributes to put in it. No grouping attributes in the Select phrase.

After a GroupBy ? l Sometimes it is useful to carry out another operation on the result of a GroupBy. l There is no need to do a Projection on the result since it can be incorporated into the GroupBy. l To do a Restriction on the result, SQL provides a Having phrase. This is because SQL’s built-in sequence of operations has already executed all Restrictions. There is no way to do another without introducing a special phrase, i.e. the Having phrase. l The Having restriction condition is limited to using : l a grouping attribute, l an aggregation results. Because the result of a GroupBy contains nothing but these.

Executing an SQL ‘Select’ Statement Order By Where Group By Having From Select Restrictions done here. Another Restriction here ! Grouping done here. The phrases are executed in the following order. Joins / Cartesian Products done here. Sequencing done here. Projections done here.

‘Having’ Phrase The SQL syntax for a Group By with a Having phrase is : Select GroupingAttribute(s), “aggregation” As Result_Name From RELATION_NAME Group By GroupingAttribute(s) Havingcondition ; The Having phrase must immediately follow a Group By phrase, and not be anywhere else. “condition” can contain only grouping attribute(s) and aggregate function applications in a Boolean combination of comparisons.

Example of the ‘Having’ Phrase (1) “Get the total salary paid to each marital-status group, for groups where the total exceeds £50,000.” Select M-S, Sum( All Sal ) As Total From EMPLOYEE Group By M-S Having Sum( Sal ) > ; EMPLOYEE GroupBy[ M-S ] With[ Total  Bag[ Sal ] Sum ] Restrict[ Total > ] A GroupBy followed by a Restriction. Cannot use the name Total.

Example of the ‘Having’ Phrase (2) “How many different shipment sizes are there per supplier, for suppliers who ship more than 2 shipment sizes ?” Select SNo, Count( Distinct Qty ) As Sizes From SHIP Group By SNo Having Count( Distinct Qty ) > 2 ; SHIP GroupBy[ SNo ] With[ Sizes  Project[ Qty ] Count ] Restrict[ Sizes > 2 ]

‘Before’ and ‘After’ Restrictions It is possible to write algebraic expressions like : ( … expression … ) Restrict[ … ] GroupBy[.. ] With[ … ] Restrict[ … ] The equivalent in SQL is : Select … From … Where … Group By … Having … ; Before After Before

Example of Extend EuroMaxB O/DAmt /23A 10/29C 32/14X 47/18G A/C BANK BANK Extend[ Euro  Amt/0.65 ; MaxB  Amt + O/D ] Amt 10/23A 10/29C 32/14X 47/18G A/C O/D Amt /23A 10/29C 32/14X 47/18G A/C BANK

Definition of ‘Extend’ l Creates a new relation formed from its operand by adding one or more named attributes to it. l Each new attribute(s) contain values specified in a ‘scalar expression’. The expression’s type is the type of the new attribute. l An expression can contain one or more of : l a constant; l an attribute name : this yields that attribute’s value in the same tuple; l scalar functions & operators (of the relevant data type); l parentheses to control the execution sequence.

SQL : Extend The SQL equivalent of an Extension has the syntax :- Select RELATION_NAME.*, calculation As Result_Name From RELATION_NAME ; This yields all the relation’s existing attributes. Repeat for each new attribute. An ‘*’ on its own is only allowed if there is nothing else in the Select phrase.

SQL Examples l The previous example is written in SQL as follows : Select BANK.*, Amt/0.65 As Euro, Amt + O/D As MaxB From BANK ; l “In addition to the details of all bank accounts, show how much the amount in each account would be above an overdraft limit that was 5 times greater than the current overdraft limit”. Select BANK.*, Amt + (5 * O/D) As Possible From BANK ;

Combining Algebra Operations l SQL treats an Extension as the reverse of a Projection, by putting both in the Select phrase. l As the Select phrase is the last to be executed by SQL, then an Extension is always the last operation to be executed, in parallel with a Projection. l This applies even when there is a Group By (with / without Having). In this case, since Extend applies scalar expressions and an aggregation can be embedded in a scalar expression, the Extension comes after the GroupBy.