Database Systems I. SQL Queries

Similar documents
SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

SQL: Queries, Programming, Triggers

Relational Algebra and SQL

Chapter 5. SQL: Queries, Constraints, Triggers

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Θεµελίωση Βάσεων εδοµένων

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle SQL. Course Summary. Duration. Objectives

Relational Database: Additional Operations on Relations; SQL

Relational Databases

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals

Introduction to Microsoft Jet SQL

Oracle Database: SQL and PL/SQL Fundamentals

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

6 QUERY-BY-EXAMPLE (QBE)

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Proposed solutions Project Assignment #1 (SQL)

Query-by-Example (QBE)

Lecture 4: More SQL and Relational Algebra

Oracle Database 12c: Introduction to SQL Ed 1.1

Introduction to database design

Oracle Database 11g SQL

Netezza SQL Class Outline

3. Relational Model and Relational Algebra

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Lecture 6: Query optimization, query tuning. Rasmus Pagh

SQL: QUERIES, CONSTRAINTS, TRIGGERS

3.GETTING STARTED WITH ORACLE8i

Oracle Database 10g: Introduction to SQL

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Introduction to SQL: Data Retrieving

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

SQL NULL s, Constraints, Triggers

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Oracle Database: SQL and PL/SQL Fundamentals NEW

Week 4 & 5: SQL. SQL as a Query Language

Structured Query Language (SQL)

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Database Security. Chapter 21

The Relational Model. Why Study the Relational Model?

Chapter 14: Query Optimization

Databases 2011 The Relational Model and SQL

Chapter 13: Query Optimization

Instant SQL Programming

IT2305 Database Systems I (Compulsory)

Programming with SQL

Lecture 6. SQL, Logical DB Design

Advance DBMS. Structured Query Language (SQL)

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

IT2304: Database Systems 1 (DBS 1)

Performing Queries Using PROC SQL (1)

Database Management Systems. Chapter 1

INFO/CS 330: Applied Database Systems

Financial Data Access with SQL, Excel & VBA

ICAB4136B Use structured query language to create database structures and manipulate data

SQL SELECT Query: Intermediate

Oracle Database: Introduction to SQL

unisys OS 2200 Relational Data Management System (RDMS 2200) and IPF SQL Interface End Use Guide imagine it. done. ClearPath OS

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Database Programming with PL/SQL: Learning Objectives

Introduction to Querying & Reporting with SQL Server

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

1 Structured Query Language: Again. 2 Joining Tables

Oracle Database: Introduction to SQL

Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354

Effective Use of SQL in SAS Programming

Semantic Errors in SQL Queries: A Quite Complete List

Oracle Database: Introduction to SQL

Using SQL Queries in Crystal Reports

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Information Systems SQL. Nikolaj Popov

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Chapter 1 Overview of the SQL Procedure

Querying Microsoft SQL Server

Comp 3311 Database Management Systems. 2. Relational Model Exercises

DBMS / Business Intelligence, SQL Server

Microsoft Access 3: Understanding and Creating Queries

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Advanced SQL. Lecture 3. Outline. Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views

Data Warehousing and Decision Support. Introduction. Three Complementary Trends. Chapter 23, Part A

Advanced Query for Query Developers

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Simple SQL Queries (3)

Course ID#: W 35 Hrs. Course Content

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

Relational Division and SQL

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Transcription:

Database Systems I SQL Queries CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Introduction We now introduce SQL, the standard query language for relational DBS. As RA, an SQL query takes one or two input tables and returns one output table. Any RA query can also be formulated in SQL, but in a more user-friendly manner. In addition, SQL contains certain features of great practical importance that go beyond the expressiveness of RA, e.g. sorting and aggregation functions. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 91

Example Instances R1 sid bid day 22 101 10/10/96 58 103 11/12/96 We will use these instances of the Sailors and Reserves tables in our examples. S1 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 S2 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 92 Basic SQL Query SELECT FROM WHERE [DISTINCT] target-list relation-list qualification relation-list: list of relation names (possibly with a tuple-variable after each name). target-list: list of attributes of relations in relation-list qualification : comparisons ( Attr op const or Attr1 op Attr2, where op is one of <, >, =,,, ) combined using AND, OR and NOT. DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated! CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 93

Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the Cartesian product of relation-list. Selection of the tuples satisfying qualifications. Projection onto the attributes that are in target-list. If DISTINCT is specified, eliminate duplicate rows. This strategy is not an efficient way to process a query! An optimizer will find more efficient strategies to compute the same answers. It is often helpful to write an SQL query in the same order (FROM, WHERE, SELECT). CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 94 Example Conceptual Evaluation SELECT S.sname, Reserves R WHERE S.sid=R.sid AND R.bid=103 (sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96 CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 95

Projection Expressed through the SELECT clause. Can specify any subset of the set of all attributes. SELECT sname, age FROM Sailors; * selects all attributes. FROM Sailors; Can rename attributes. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 96 Projection For numeric attribute values, can also apply arithmetic operators +, * etc. Can create derived attributes and name them, using AS or = : SELECT age AS age0, age1=age-5, 2*S.age AS age2 FROM Sailors; CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 97

Projection The result of a projection can contain duplicates (why?). To eliminate duplicates from the output, specify DISTINCT in the SELECT clause. SELECT DISTINCT age FROM Sailors; CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 98 Selection Expressed through the WHERE clause. Selection conditions can compare constants and attributes of relations mentioned in the FROM clause. Comparison operators: =, <>, <, >, <=, >= For numeric attribute values, can also apply arithmetic operators +, * etc. Simple conditions can be combined using the logical operators AND, OR and NOT. Default precedences: NOT, AND, OR. Use parentheses to change precedences. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 99

Selection FROM Sailors WHERE sname = Watson ; FROM Sailors WHERE rating >= age; FROM Sailors WHERE (rating = 5 OR rating = 6) AND age <= 20; CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 100 String Comparisons LIKE is used for approximate conditions (pattern matching) on string-valued attributes: string LIKE pattern Satisfied if pattern contained in string attribute. NOT LIKE satisfied if pattern not contained in string attribute. _ in pattern stands for any one character and % stands for 0 or more arbitrary characters. WHERE S.sname LIKE B_%B ; CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 101

Null Values Special attribute value NULL can be interpreted as: Value unknown (e.g., a rating has not yet been assigned), Value inapplicable (e.g., no spouse s name), Value withheld (e.g., the phone number). The presence of NULL complicates many issues: Special operators needed to check if value is null. Is rating>8 true or false when rating is equal to null? What about AND, OR and NOT connectives? Meaning of constructs must be defined carefully. E.g., how to deal with tuples that evaluate neither to TRUE nor to FALSE in a selection? CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 102 Null Values NULL is not a constant that can be explicitly used as an argument of some expression. NULL values need to be taken into account when evaluating conditions in the WHERE clause. Rules for NULL values: An arithmetic operator with (at least) one NULL argument always returns NULL. The comparison of a NULL value to any second value returns a result of UNKNOWN. A selection returns only those tuples that make the condition in the WHERE clause TRUE, those with UNKNOWN or FALSE result do not qualify. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 103

Truth Value Unknown Three-valued logic: TRUE, UNKNOWN, FALSE. Can think of TRUE = 1, UNKNOWN = ½, FALSE = 0. AND of two truth values: their minimum. OR of two truth values: their maximum. NOT of a truth value: 1 the truth value. Examples: TRUE AND UNKNOWN = UNKNOWN FALSE AND UNKNOWN = FALSE FALSE OR UNKNOWN = UNKNOWN NOT UNKNOWN = UNKNOWN CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 104 Truth Value Unknown FROM Sailors WHERE rating < 5 OR rating >= 5; Does not return all sailors, but only those with non-null rating. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 105

Ordering the Output Can order the output of a query with respect to any attribute or list of attributes. Add ORDER BY clause to the query: WHERE age < 20 ORDER BY rating; WHERE age < 20 ORDER BY rating, age; By default, ascending order. Use DESC to specify descending order. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 106 Cartesian Product Expressed in FROM clause. Forms the Cartesian product of all relations listed in the FROM clause, in the given order. FROM Sailors, Reserves; So far, not very meaningful. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 107

Join Expressed in FROM clause and WHERE clause. Forms the subset of the Cartesian product of all relations listed in the FROM clause that satisfies the WHERE condition: FROM Sailors, Reserves WHERE Sailors.sid = Reserves.sid; In case of ambiguity, prefix attribute names with relation name, using the dot-notation. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 108 Join Since joins are so common operations, SQL provides JOIN as a shorthand. FROM Sailors JOIN Reserves ON Sailors.sid = Reserves.sid; NATURAL JOIN produces the natural join of the two input tables, i.e. an equi-join on all attributes common to the input tables. FROM Sailors NATURAL JOIN Reserves; CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 109

Join Typically, there are some dangling tuples in one of the input tables that have no matching tuple in the other table. Dangling tuples are not contained in the output. Outer joins are join variants that do not loose any information from the input tables: LEFT OUTER JOIN includes all dangling tuples from the left input table with NULL values filled in for all attributes of the right input table. RIGHT OUTER JOIN includes all dangling tuples from the right input table with NULL values filled in for all attributes of the left input table. FULL OUTER JOIN includes all dangling tuples from both input tables. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 110 Tuple Variables Tuple variable is an alias referencing a tuple from the relation over which it has been defined. Again, use dot-notation. Needed only if the same relation name appears twice in the query. SELECT S.sname, Reserves R1, Reserves R2 WHERE S.sid=R1.sid AND S.sid=R2.sid AND R1.bid <> R2.bid It is good style, however, to use tuple variables always. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 111

A Further Example Find sailors who ve reserved at least one boat: SELECT S.sid, Reserves R WHERE S.sid=R.sid Would adding DISTINCT to this query make a difference? What is the effect of replacing S.sid by S.sname in the SELECT clause? What about adding DISTINCT to this variant of the query? CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 112 Set Operations SQL supports the three basic set operations. UNION: union of two relations INTERSECT: intersection of two relations EXCEPT: set-difference of two relations Two input relations must have same schemas. Can use AS to make input relations compatible. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 113

Set Operations Find sid s of sailors who ve reserved a red or a green boat. If we replace OR by AND in the first version, what do we get? What do we get if we replace UNION by EXCEPT? SELECT S.sid, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color= red OR B.color= green ); (SELECT S.sid, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red ) UNION (SELECT S.sid, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= green ); CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 114 Set Operations Find sid s of sailors who ve reserved a red and a green boat. Contrast symmetry of the UNION and INTERSECT queries with how much the other versions differ. SELECT S.sid, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color= red AND B2.color= green ); Key attribute! (SELECT S.sid, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red ) INTERSECT (SELECT S.sid, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= green ); CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 115

Subqueries A subquery is a query nested within another SQL query. Subqueries can return a a single constant that can be used in the WHERE clause, return a relation that can be used in the WHERE clause, appear in the FROM clause, followed by a tuple variable through which results can be referenced in the query. Subqueries can contain further subqueries etc., i.e. there is no restriction on the level of nesting. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 116 Subqueries The output of a subquery returning a single constant can be compared using the normal operators =, <>, >, etc. SELECT S.age WHERE S.age > (SELECT S.age WHERE S.sid=22); How can we be sure that the subquery returns only one constant? To understand semantics of nested queries, think of a nested loops evaluation: For each Sailors tuple, check the qualification by computing the subquery. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 117

Subqueries The output of a subquery R returning an entire relation can be compared using the special operators EXISTS R is true if and only if R is non-empty. s IN R is true if and only if tuple (constant) s is contained in R. s NOT IN R is true if and only if tuple (constant) s is not contained in R. s op ALL R is true if and only if constant s fulfills op with respect to every value in (unary) R. s op ANY R is true if and only if constant s fulfills op with respect to at least one value in (unary) R. Op can be one of >, <, =,,, CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 118 Subqueries EXISTS, ALL and ANY can be negated by putting NOT in front of the entire expression. SELECT R.bid FROM Reserves R WHERE R.sid IN (SELECT S.sid WHERE S.name= rusty ); 1 WHERE S1.age > ALL (SELECT S2.age 2 WHERE S2.name= rusty ); CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 119

Subqueries In a FROM clause, we can use a parenthesized subquery instead of a table. Need to define a corresponding tuple variable to reference tuples from the subquery output. FROM Reserves R, (SELECT S.sid WHERE S.age>60) OldSailors WHERE R.sid = OldSailors.sid; CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 120 Correlated Subqueries SELECT B.bid FROM Boats B WHERE NOT EXISTS ( FROM Reserves R WHERE R.bid=B.bid); The second subquery is correlated to the outer query, i.e. the execution of the subquery may return different results for every tuple of the outer query. Illustrates why, in general, subquery must be recomputed for each tuple of the outer query / tuple. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 121

A Further Example Find sid s of sailors who ve reserved both a red and a green boat: SELECT S.sid, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red AND S.sid IN (SELECT S2.sid 2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color= green ); Similarly, EXCEPT queries re-written using NOT IN. To find names (not sid s) of Sailors who ve reserved both red and green boats, just replace S.sid by S.sname in SELECT clause. (What about INTERSECT query?) CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 122 Division in SQL Find sailors who ve reserved all boats. With EXCEPT: SELECT S.sname WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)); CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 123

Division in SQL Find sailors who ve reserved all boats. Without EXCEPT: SELECT S.sname WHERE NOT EXISTS (SELECT B.bid FROM Boats B Sailors S such that... WHERE NOT EXISTS (SELECT R.bid FROM Reserves R there is no boat B without... WHERE R.bid=B.bid AND R.sid=S.sid)) a Reserves tuple showing S reserved B CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 124 Aggregation Operators Operators on sets of tuples. Significant extension of relational algebra. COUNT (*): the number of tuples. COUNT ( [DISTINCT] A): the number of (unique) values in attribute A. SUM ( [DISTINCT] A): the sum of all (unique) values in attribute A. AVG ( [DISTINCT] A): the average of all (unique) values in attribute A. MAX (A): the maximum value in attribute A. MIN (A): the minimum value in attribute A. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 125

Aggregation Operators (1) SELECT COUNT (*) ; (2) SELECT AVG (S.age) WHERE S.rating=10; (4) (3) SELECT COUNT (DISTINCT S.rating) WHERE S.sname= Bob ; SELECT S.sname WHERE S.rating= (SELECT MAX(S2.rating) 2); (5) SELECT AVG ( DISTINCT S.age) WHERE S.rating=10; CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 126 Aggregation Operators Find name and age of the oldest sailor(s). The first query looks correct, but is illegal. (We ll look into the reason a bit later, when we discuss GROUP BY.) The second query is a correct and legal solution. SELECT S.sname, MAX (S.age) ; SELECT S.sname, S.age WHERE S.age = (SELECT MAX (S2.age) 2); CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 127

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: For i = 1, 2,..., 10: SELECT MIN (S.age) WHERE S.rating = i; But in general, we don t know how many rating values exist, and what these rating values are. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 128 GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification A group is a set of tuples that have the same value for all attributes in grouping-list. The target-list contains (i) attribute names and (ii) terms with aggregation operations. The attribute list (i) must be a subset of grouping-list. Each answer tuple corresponds to a group, and output attributes must have a single value per group. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 129

Conceptual Evaluation The cross-product of relation-list is computed, tuples that fail qualification are discarded, unnecessary attributes are deleted, and the remaining tuples are partitioned into groups by the value of attributes in grouping-list. The group-qualification is then applied to eliminate groups that do not satisfy this condition. Expressions in group-qualification must have a single value per group! One answer tuple is generated per qualifying group. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 130 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) 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. (Use AS to name it.) sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 71 zorba 10 16.0 64 horatio 7 35.0 29 brutus 1 33.0 58 rusty 10 35.0 rating age 1 33.0 7 45.0 7 35.0 8 55.5 10 35.0 Answer relation rating 7 35.0 CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 131

GROUP BY and HAVING For each red boat, find the number of reservations for this boat. SELECT B.bid, COUNT (*) AS scount, 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? CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 132 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) WHERE S.age > 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) 2 WHERE S.rating=S2.rating); Shows HAVING clause can also contain a subquery. Compare this with the query where we considered only ratings with 2 sailors over 18! What if HAVING clause is replaced by: HAVING COUNT(*) >1 CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 133

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 WHERE S.age = (SELECT MIN (AVG (S2.age)) 2); Correct solution: SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG (S.age) AS avgage GROUP BY S.rating) AS Temp WHERE Temp.avgage = (SELECT MIN (Temp.avgage) FROM Temp); CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 134 Summary 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. CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 135