1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
SQL (2).
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
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.
SQL: The Query Language Part 1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
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.
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.
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
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.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
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: 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: 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 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
SQL: Queries, Constraints, Triggers
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Database Systems October 14, 2009 Lecture #5.
Basic SQL Lecture 6 Fall
SQL: Queries, Constraints, Triggers
SQL: The Query Language
SQL: Queries, Programming, Triggers
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Structured Query Language
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
Presentation transcript:

1 SQL: The Query Language (Part II)

2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. v AS and = are two ways to name fields in result. v LIKE is used for string matching. `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters. SELECT S.age, age1=S.age-5, 2*S.age AS age2 FROM Sailors S WHERE S.sname LIKE ‘B_%B’

3 Find sid’s of sailors who’ve reserved a red or a green boat v UNION : Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries). v Also available: EXCEPT (What do we get if we replace UNION by EXCEPT ?) SELECT S.sid FROM Sailors S, 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 FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ UNION SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

4 Find sid’s of sailors who’ve reserved a red and a green boat v INTERSECT : Can be used to compute the intersection of any two union- compatible sets of tuples. v Included in the SQL/92 standard, but some systems don’t support it. v Contrast symmetry of the UNION and INTERSECT queries with how much the other versions differ. SELECT S.sid FROM Sailors S, 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’) 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 SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ Key field!

5 Nested Queries v A very powerful feature of SQL: a WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses.) v To find sailors who’ve not reserved #103, use NOT IN. v To understand semantics of nested queries, think of a nested loops evaluation: For each Sailors tuple, check the qualification by computing the subquery. SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R WHERE R.bid=103) Find names of sailors who’ve reserved boat #103:

6 Nested Queries with Correlation v EXISTS is another set comparison operator, like IN. v If UNIQUE is used, and * is replaced by R.bid, finds sailors with at most one reservation for boat #103. ( UNIQUE checks for duplicate tuples; * denotes all attributes. Why do we have to replace * by R.bid ?) v Illustrates why, in general, subquery must be re- computed for each Sailors tuple. SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Find names of sailors who’ve reserved boat #103:

7 More on Set-Comparison Operators v We’ve already seen IN, EXISTS and UNIQUE. Can also use NOT IN, NOT EXISTS and NOT UNIQUE. v Also available: op SOME, op ALL, where op is v Find sailors whose rating is greater than that of some sailor called Horatio: SELECT * FROM Sailors S WHERE S.rating > SOME ( SELECT S2.rating FROM Sailors S2 WHERE S2.sname=‘Horatio’)

8 Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [ DISTINCT ] A) SUM ( [ DISTINCT ] A) AVG ( [ DISTINCT ] A) MAX (A) MIN (A) SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 SELECT COUNT (*) FROM Sailors S SELECT AVG ( DISTINCT S.age) FROM Sailors S WHERE S.rating=10 SELECT S.sname FROM Sailors S WHERE S.rating= ( SELECT MAX (S2.rating) FROM Sailors S2) single column SELECT COUNT ( DISTINCT S.rating) FROM Sailors S WHERE S.sname=‘Bob’

9 Find name and age of the oldest sailor(s) v The first query is illegal! (We’ll look into the reason a bit later, when we discuss GROUP BY.) v The third query is equivalent to the second query, and is allowed in the SQL/92 standard, but is not supported in some systems. 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) SELECT S.sname, S.age FROM Sailors S WHERE ( SELECT MAX (S2.age) FROM Sailors S2) = S.age

10 Summary v There are many ways to write a given query in SQL. v Target-list syntax can include arithmetic, naming v Set operations: UNION, EXCEPT, INTERSECT v Nested queries: –Uncorrelated: subquery can be executed once –Correlated: subquery executed once per “outer” tuple v Set comparisons: semantics can be tricky –It’s just first-order predicate logic ( , , , ,  ) v Aggregates: Useful, and not part of relational algebra.