M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
Advertisements

Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
1 "A mind once stretched by new thoughts can never regain its original shape.” -- Albert Einstein Prove that each positive integer has a multiple that.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
SQL Part II: Advanced Queries. 421B: Database Systems - SQL Queries II 2 Aggregation q Significant extension of relational algebra q “Count the number.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
SQL (2).
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
SQL Review.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Rewriting Minus Queries Using Not In SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ MINUS.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
SQL 2 Introduction Structured Query Language (SQL): the most widely used commercial relational database language Originally.
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Part III COSC 3480 Teaching Plan Part 1.SQL in 45 Minutes.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
SQL and Query Execution for Aggregation. Example Instances Reserves Sailors Boats.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
1 Relational Calculus ♦ Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). ♦ Calculus has variables, constants,
SQL: Queries, Constraints, Triggers
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Database Systems October 14, 2009 Lecture #5.
SQL: Queries, Constraints, Triggers
SQL: The Query Language
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: Structured Query Language
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: The Query Language (Part III)
SQL: Queries, Programming, Triggers
Presentation transcript:

M ATH IN SQL

222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all (unique) values in attribute A. AVG ( [DISTINCT] A): the average of all (unique) values in attribute A. SELECT AVG ( DISTINCT S.age) FROM Sailors S WHERE S.rating=10; SELECT AVG (S.age) FROM Sailors S;

333 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. MAX (A): the maximum value in attribute A. MIN (A): the minimum value in attribute A. SELECT S.sname FROM Sailors S WHERE S.rating= ( SELECT MAX (S2.rating) FROM Sailors S2); SELECT MAX (rating) FROM Sailors;

444 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. COUNT (*): the number of tuples. SELECT COUNT (*) FROM Sailors S

555 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. COUNT ( [DISTINCT] A): the number of (unique) values in attribute A. SELECT COUNT ( DISTINCT S.rating) FROM Sailors S WHERE S.sname=‘Bob’;

666 A GGREGATION O PERATORS Find name and age of the oldest sailor(s). The first query looks correct, but is illegal. Thoughts as to why? The second query is a correct and legal solution. SELECT S.sname, MAX (S.age) FROM Sailors S; SELECT S.sname, S.age FROM Sailors S WHERE S.age = ( SELECT MAX (S2.age) FROM Sailors S2);

777 GROUP BY AND HAVING So far, we’ve applied aggregation operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples. Find the age of the youngest sailor for each rating value. Suppose we know that rating values go from 1 to 10; we can write ten (!) queries that look like this: But in general, we don’t know how many rating values exist, and what these rating values are. Plus, it’s a waste of time to write so many queries SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i ; For i = 1, 2,..., 10:

888 GROUP BY AND HAVING A group is a set of tuples that have the same value for all attributes grouping-list. The target-list contains attribute names terms with aggregation operations. Attribute list must be a subset of grouping-list. Each answer tuple corresponds to a group, and output attributes must have a single value per group. SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification Notice the notation

999 C ONCEPTUAL E VALUATION Given: SELECT S.rating, MIN(S.age) as minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT(*) > 1 Step 1 The cross-product of relation-list is computed In this instance, it’s only Sailors

10 C ONCEPTUAL E VALUATION Given: SELECT S.rating, MIN(S.age) as minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT(*) > 1 Step 2 Tuples that fail qualification are discarded ‘unnecessary’ attributes are deleted

11 C ONCEPTUAL E VALUATION Given: SELECT S.rating, MIN(S.age) as minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT(*) > 1 Step 3 Remaining tuples are partitioned into groups by the value of attributes in grouping-list

12 C ONCEPTUAL E VALUATION Given: SELECT S.rating, MIN(S.age) as minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT(*) > 1 Step 4 The group-qualification is then applied to eliminate groups that do not satisfy this condition.

13 C ONCEPTUAL E VALUATION Given: SELECT S.rating, MIN(S.age) as minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT(*) > 1 Step 5 One answer tuple is generated per qualifying group by applying the aggregation operator.

14 GROUP BY AND HAVING Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors. SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1; Only S.rating and S.age are mentioned in the SELECT, GROUP BY or HAVING clauses; other attributes ` unnecessary ’. 2nd column of result is unnamed What to do? Answer relation

15 GROUP BY AND HAVING For each red boat, find the number of reservations for this boat. SELECT B.bid, COUNT (*) AS scount FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid; Grouping over a join of three relations. What do we get if we remove B.color=‘red’ from the WHERE clause and add a HAVING clause with this condition? What if we drop Sailors and the condition involving S.sid?

16 GROUP BY AND HAVING Find the age of the youngest sailor with age > 18, for each rating with at least 2 sailors (of any age). SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age > 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S2 WHERE S.rating=S2.rating); Shows HAVING clause can also contain a subquery. What if HAVING clause is replaced by: HAVING COUNT(*) >1

17 GROUP BY AND HAVING Find those ratings for which the average age is the minimum over all ratings. Aggregation operations cannot be nested! WRONG: SELECT S.rating FROM Sailors S WHERE S.age = (SELECT MIN (AVG (S2.age)) FROM Sailors S2); Correct solution: SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG (S.age) AS avgage FROM Sailors S GROUP BY S.rating) AS Temp WHERE Temp.avgage = (SELECT MIN (Temp.avgage) FROM Temp);

ORDERING & TOP/BOTTOM

19 ORDER BY The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword.

20 TOP/BOTTOM The TOP clause is used to specify the number of records to return. The TOP clause can be very useful on large tables with thousands of records Returning a large number of records can impact on performance Can ‘sample’ the table using TOP Not all database systems support the TOP clause or implement it in different fashion

21 TOP/BOTTOM SQL Server SELECT TOP number|percent column_name(s) FROM table_name Ex: SELECT TOP 5 * FROM Persons MySQL SELECT column_name(s) FROM table_name LIMIT number Ex: SELECT * FROM Persons LIMIT 5

22 TOP/BOTTOM Oracle SELECT column_name(s) FROM table_name WHERE ROWNUM <= number Ex: SELECT * FROM Persons WHERE ROWNUM <=5 DB2 SELECT column_name(s) FROM table_name FETCH FIRST number ROWS ONLY Ex: SELECT * FROM Persons FETCH FIRST 5 ROWS ONLY

23 TOP/BOTTOM Can specify Fixed number SELECT TOP 10 * … A percent SELECT TOP 10 PERCENT * …

24 TOP/BOTTOM How to return the oldest 5 rentals? How to return the newest 5 rentals?

25 TOP/BOTTOM How to return the 3 rd newest rental?

26 S UMMARY SQL was an important factor in the early acceptance of the relational model; more natural than earlier, procedural query languages. All queries that can be expressed in relational algebra can also be formulated in SQL. In addition, SQL has significantly more expressive power than relational algebra, in particular aggregation operations and grouping. Many alternative ways to write a query; query optimizer looks for most efficient evaluation plan. In practice, users need to be aware of how queries are optimized and evaluated for most efficient results.