CMPT 258 Database Systems SQL Queries (Chapter 5).

Slides:



Advertisements
Similar presentations
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
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.
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
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
SQL Review.
111 A DMINISTRATIVE Exam Oct 19, :30pm in 3005 (usual room) No class on Oct 21 What topic to do for review? November 18 th away for conference.
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 Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
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.
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 CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
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.
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 (資料庫系統)
Basic SQL Lecture 6 Fall
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
Presentation transcript:

CMPT 258 Database Systems SQL Queries (Chapter 5)

2 Agenda Introduction Basic SQL Query Union, Intersection and Except Nested Queries Aggregate Operations

Examples Reserves Sailors Boats 3

Basic SQL Query relation-list A list of relation names (possibly with a range-variable after each name). target-list A 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 and OR. SELECT target-list FROM relation-list WHERE qualification

The query uses the optional keyword AS to introduce a range variable. 5

Basic SQL Query DISTINCT is an optional keyword indicating that the answer should not contain duplicates. ▫Default is that duplicates are not eliminated! SELECT [DISTINCT] target-list FROM relation-list WHERE qualification

7

8

Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: ▫ Compute the cross-product of relation-list. ▫ Discard resulting tuples if they fail qualifications. ▫ Delete attributes that are not in target-list. ▫ If DISTINCT is specified, eliminate duplicate rows. SELECT [DISTINCT] target-list FROM relation-list WHERE qualification

Reserves Sailors Boats

A Note on Range Variables Really needed only if the same relation appears twice in the FROM clause. The previous query can also be written as: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 It is good style, however, to use range variables always! OR

13 Query Examples Find the sids of sailors who have reserved a red boat

14 Query Examples Find the sids of sailors who have reserved a red boat Find the name of sailors who have reserved a red boat SELECT R.sid FROM Reserves R, Boats B WHERE R.bid=B.bid AND B.color = ‘red’

15 Query Examples Find the sids of sailors who have reserved a red boat Find the name of sailors who have reserved a red boat SELECT R.sid FROM Reserves R, Boats B WHERE R.bid=B.bid AND B.color = ‘red’ SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid = B.bid AND B.color = ‘red’

Find the color of the boats reserved by Lubber 16

Find the color of the boats reserved by Lubber 17 SELECT B.color FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid = B.bid AND S.name = ‘Lubber’

Find the names of sailors who’ve reserved at least one boat

SELECT DISTINCT S.name FROM Sailors S, Reserves R WHERE S.sid=R.sid

20 Find the names of sailors who have sailed two different boats on the same day.

21 Find the names of sailors who have sailed two different boats on the same day. SELECT S.sname FROM Sailors S, Reserves R1, Reserves R2 WHERE S.sid= R1.sid and R1.sid=R2.sid AND R1.day = R2.day AND R1.bid <> R2.bid

Expressions and Strings Find ages of sailors whose names begin and end with B and contain at least three characters. LIKE is used for string matching. ‘_’ stands for any one character and ‘%’ stands for 0 or more arbitrary characters (including blanks). ‘_AB%’ Comparison operators (=,, etc.) can be used for string comparison

Expressions and Strings Find ages of sailors whose names begin and end with B and contain at least three characters. SELECT S.age FROM Sailors S WHERE S.sname LIKE ‘B_%B’

Query FROM More than One Table Using JOINs A join combines the rows of two tables, based on a rule called a join condition; this compares values from the rows of both tables to determine which rows should be joined. Basic types of join: ▫inner join ▫outer join  LEFT OUTER JOIN  RIGHT OUTER JOIN  FULL OUTER JOIN 24

Inner Join select a, b from A, B where a=b 26

27

28

29

Reserves Sailors Boats

Union Intersect Except 31

Find sid’s of sailors who’ve reserved a red or a green boat

UNION : Can be used to compute the union of any two union- compatible sets of tuples (which are themselves the result of SQL queries). SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’)

Find sid’s of sailors who’ve reserved a red or a green boat UNION : Can be used to compute the union of any two union- compatible sets of tuples (which are themselves the result of SQL queries). If we replace OR by AND in the first version, what do we get? SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’) SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ UNION SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’

35 Find the sids of sailors who have a rating of 10 or reserved boat 104

36 Find the sids of sailors who have a rating of 10 or reserved boat 104 The default for UNION queries is that duplicates are eliminated. To retain duplicates, use UNION ALL SELECT S.sid FROM Sailors S WHERE S.rating = 10 UNION SELECT R.sid FROM Reserves R WHERE R.bid=104

Find sid’s of sailors who’ve reserved a red and a green boat INTERSECT : Can be used to compute the intersection of any two union-compatible sets of tuples. Some systems don’t support it. SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’

38 Find the sids of all sailors who have reserved red boats but not green boats SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ EXCEPT SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’

39 Find the names of sailors who’ve reserved a red and a green boat Is this query correct? Why SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

40 Find the names of sailors who’ve reserved a red and a green boat Is this query correct? Why SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ There are two sailors with the same name Horatio!

Readings Chapter 5