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.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
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.
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).
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.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
1 Lecture 12: Further relational algebra, further SQL
SQL Review.
ICS 624 Spring 2011 Overview of DB & IR Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/12/20111Lipyeow.
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.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
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 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
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.
SQL Queries and Subqueries Zaki Malik September 04, 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.
10/20/20151 Introduction to SQL and the Relational Model ICOS Big Data Summer Camp June 2, 2015 Mike Anderson (slides from Mike Cafarella)
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.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
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.
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.
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.
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.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
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.
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.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
SQL: Queries, Constraints, Triggers
SQL: Structured Query Language
SQL: The Query Language (Part III)
Presentation transcript:

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 Lim -- University of Hawaii at Manoa

Bag Semantics in SQL SELECT-FROM-WHERE statements preserve duplicates by default, unless DISTINCT is given. Set operators UNION, INTERSECT, EXCEPT use set semantics by default! To use bag semantics: UNION ALL, INTERSECT ALL, EXCEPT ALL. 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa2 (SELECT title, year FROM Movies) UNION ALL (SELECT movieTitle AS title, movieYear AS year FROM StarsIn)

Aggregate Operators SQL supports 5 aggregation operators on a column, say A, 1.COUNT ( * ), COUNT ( [DISTINCT] A ) 2.SUM ( [DISTINCT] A ) 3.AVG ( [DISTINCT] A ) 4.MAX ( A ) 5.MIN ( A ) 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa3

Aggregation Queries Q25: Find the average age of all sailors 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa4 SELECT AVG(S.age) FROM Sailors S Q28: Count the number of sailors SELECT COUNT (*) FROM Sailors S Find the age of the oldest sailor SELECT MAX (S.age) FROM Sailors S

Q27: Find the name and age of the oldest sailor If there is an aggregation operator in the SELECT clause, then it can only have aggregation operators unless the query has a GROUP BY clause -- first query is illegal. 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa5 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 )

Queries with GROUP BY and HAVING The target-list contains (i) attribute names (ii) terms with aggregate operations (e.g., MIN (S.age)). – The list of attribute names in (i) must be a subset of grouping-list. – Intuitively, each answer tuple corresponds to a group, and these attributes must have a single value per group. – A group is a set of tuples that have the same value for all attributes in grouping-list. 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa6 SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification

Conceptual Evaluation Strategy with GROUP BY and HAVING [Same as before] The cross-product of relation-list is computed, tuples that fail qualification are discarded, `unnecessary’ fields are deleted The remaining tuples are partitioned into groups by the value of attributes in grouping-list. The group-qualification is then applied to eliminate some groups. Expressions in group-qualification must have a single value per group! – In effect, an attribute in group-qualification that is not an argument of an aggregate op also appears in grouping-list. (SQL does not exploit primary key semantics here!) Aggregations in target-list are computed for each group One answer tuple is generated per qualifying group 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa7

Q32: Find age of the youngest sailor with age >= 18, for each rating with at least 2 such sailors 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa8 SELECT S.rating, MIN(S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation: Sailors instance:

Conceptual Evaluation for Q32 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa9 Partition or GROUP BY Eliminate groups Using HAVING clause Perform aggregation on each group

EVERY and ANY in HAVING clauses EVERY: every row in the group must satisfy the attached condition ANY: at least one row in the group need to satisfy the condition 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa10 SELECT S.rating, MIN(S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 AND EVERY ( S.age <=60 )

Conceptual Evaluation with EVERY 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa11 Partition or GROUP BY Eliminate groups Using HAVING clause Perform aggregation on each group HAVING COUNT (*) > 1 AND EVERY (S.age <=60) What is the result of changing EVERY to ANY?

Find age of the youngest sailor for each rating with at least 2 sailors between 18 and 60 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa12 SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 AND S.age <= 60 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation: Sailors instance:

Outer Joins Regular join on sid: Sailor Lubber gets dropped. Outer join: Sailor rows without a matching Reserves row appear exactly once in the result, with the columns inherited from Reserves taking null values. Left Outer Join : Sailor rows w/o matching reservations appear in the result, but not vice versa Right Outer Join: Reservations w/o matching reservations appear in the result, but not vice versa 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa13 sidsnameratingage 22Dustin Lubber Rusty sidbidday /10/ /12/96 S1R1

Example of outer join 3/14/2011Lipyeow Lim -- University of Hawaii at Manoa14 sidsnameratingage 22Dustin Lubber Rusty sidbidday /10/ /12/96 S1 R1 SELECT S1.*, R1.* FROM Sailors S1 NATURAL OUTER JOIN Reserves R1 sidsnameratingagesidbidday 22Dustin /10/96 31Lubber855.5NULL 58Rusty /12/96 Result Note the nulls