1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
1 Database Systems ( 資料庫系統 ) October 22/24, 2007 Lecture #5.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting.
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 Table Alteration. 2 Altering Tables Table definition can be altered after its creation –Adding columns –Changing columns’ definition –Dropping columns.
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.
1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,
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.
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.
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.
The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.
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.
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.
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.
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.
Database Systems (資料庫系統)
Basic SQL Lecture 6 Fall
CS 405G: Introduction to Database Systems
SQL: The Query Language Part 1
קורס קבצים ובסיסי נתונים
CS4222 Principles of Database System
Presentation transcript:

1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem

2 Basic SQL Query SELECT [Distinct] target-list FROM from-list WHERE condition; from-list: A list of relation names (possibly with a range- variable after each name) target-list: A list of fields of relations in relation-list condition: A boolean condition DISTINCT: Optional keyword to delete duplicates

3 Basic SQL Query (continues) SELECT [Distinct] target-list FROM from-list WHERE condition; The result evaluation: 1.Compute the cross product of the tables in from-list. 2.Delete all rows that do not satisfy condition. 3.Delete all columns that do not appear in target-list. 4.If Distinct is specified eliminate duplicate rows.

4 Basic SQL Query (continues) SELECT Distinct A 1,…,A n FROM R 1,…,R m WHERE C; This translates to the expression in relational algebra:  A 1,…,A n (  C (R 1 x…x R m ))

5 Example Tables Used Reserves sidbidDay /10/02 11/12/02 Sailors sidsnameratingage Dustin Lubber Rusty Boats bidbnamecolor Nancy Gloria red green Key

6 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid

7 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid Reserves sidbidday /10/ /12/02 Boats bidbnamecolor Nancy Gloria red green Nancy Gloria red green

8 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid Reserves sidbidday /10/ /12/02 Boats bidbnamecolor 101Nancyred 103Gloriagreen

9 Boat Names and Reservation Dates SELECT bname,day FROM Boats,Reserves WHERE Boats.bid = Reserves.bid Reserves day 10/10/02 11/12/02 Boats bname Nancy Gloria

10 Sailors Who Reserved Boat 103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid = Reserves.sid and bid = 103;  sname (  Sailors.sid = Reserves.sid  bid = 103 (Sailors x Reserves))

11 SailorsReserves sidsnameratingagesidbidday 22Dustin /10/02 22Dustin /12/02 31Lubber /10/02 31Lubber /12/02 58Rusty /10/02 58Rusty /12/02 Sailors x Reserves Sailors Who Reserved Boat 103

12 SailorsReserves sidsnameratingagesidbidday 22Dustin /10/02 22Dustin /12/02 31Lubber /10/02 31Lubber /12/02 58Rusty /10/02 58Rusty /12/02 Sailors Who Reserved Boat 103  Sailors.sid = Reserves.sid  bid = 103

13 SailorsReserves sidsnameratingagesidbidday 22Dustin /10/02 22Dustin /12/02 31Lubber /10/02 31Lubber /12/02 58Rusty /10/02 58Rusty /12/02 Sailors Who Reserved Boat 103  sname

14 Range Variables SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid and R.bid = 103; Range variables are good style They are necessary if the same relation appears twice in the FROM clause

15 A Few SELECT Options Select all columns: SELECT * FROM Sailors S; Rename selected columns: SELECT S.sname AS Sailors_Name FROM Sailors S;

16 A Few SELECT Options Applying functions: Mathematical manipulations: SELECT (age-5)*2 FROM Sailors S; Aggregate functions: SELECT COUNT(*) FROM Sailors S; SELECT MAX(age) FROM Sailors S;

17 The WHERE Clause Numerical and string comparison: !=,<>,=,, >=, <=, between(between val1 AND val2) String comparisson is according to the alphabetical order! Logical components: AND, OR Null verification: IS NULL, IS NOT NULL Example: SELECT sname FROM Sailors WHERE age>=40 AND rating IS NOT NULL ;

18 The WHERE Clause (continues) The LIKE operator: A pattern matching operator Basic format: colname LIKE pattern Example: _ is a single character % is 0 or more characters SELECT sid FROM Sailors WHERE sname LIKE ‘B_%g’;

19 Sailors Who’ve Reserved a Boat SELECT sname FROM Sailors S, Reserves R WHERE S.sid = R.sid; Would adding DISTINCT give a different result?

20 Exercise Formulate a query that finds the names of sailors who reserved a yellow boat. Sailors sidsnameratingage Dustin Lubber Rusty Boats bidbnamecolor Nancy Gloria red green Reserves sidbidDay /10/02 11/12/02

21 A Harder Exercise 1.Formulate a query that finds the bid of boats that are reserved in at least two different days 2.Fix the query you formulated to find the names of these boats Hint: A relation may appear more than once in the FROM list…

22 Union, Intersect and Except Sqlplus supports the union, intersection and difference operators The syntax: query1 UNION query2; query1 INTERSECT query2; query1 MINUS query2;

23 Sailors who’ve reserved a red or green boat SELECT S.sname 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’);  sname (  color = ‘red’  color = ‘green’ (Sailors ⋈ Reserves ⋈ Boats)) What would happen if we replaced or by and ?

24 Sailors who’ve reserved red or green boat SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ UNION SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’;

25 The Second Version in Relational Algebra  sname (  color = ‘red’ (Sailors ⋈ Reserves ⋈ Boats))   sname (  color = ‘green’ (Sailors ⋈ Reserves ⋈ Boats))

26 Nested Queries A query may be nested within another through the following operators: (NOT) IN (NOT) EXISTS ANY ALL A query with nested queries is computed using nested loops

27 The IN Operator SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103); Names of sailors who’ve reserved boat 103: What would happen if we wrote NOT IN?

28 The EXISTS Operator SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 and S.sid = R.sid); Names of sailors who’ve reserved boat 103: What would happen if we wrote NOT EXISTS? Notice the correlation between the examined row and the inner query

29 Set-Comparison Queries: ANY,ALL SELECT * FROM Sailors S1 WHERE S1.age > ANY (SELECT S2.age FROM Sailors S2); Sailors who are not the youngest: We can also use op ALL (op is >, =, )

30 Another Exercise What does the following query compute? Using IN and INTERSECT operators, formulate a query that finds the names of the sailors who reserved both green and red boats. SELECT S.sname FROM Sailors S, Boats B, Reserves R 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’;

31 Another Exercise (continues) What does the following query compute? SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( (SELECT B.bid FROM Boats B) MINUS (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid) ) ;

32 For The Brave Ones… 1. Formulate a query that computes the sid of all sailors who have not reserved a green boat. 2. Prove formally that in the Sailors-Reserves-Boats database, the query of question 1 cannot be as simple as a query of the form: SELECT V j.sid FROM R1 V 1, R 2 V 2 …R n V n WHERE C Where C is a simple condition (contains only comparison clauses, AND and OR, and does not contain nested queries) Notice that the records in each table are arbitrary!