Chapter (7): Advanced SQL

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra) Query Formulation Exercise.
SQL: Queries.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
COP-5725 Practice Exercises
1 TRC vs DRC 한욱신. 2 Queries in TRC and DRC  TRC Q = {t| f(t)} where t is a (free) tuple variable and f(t) is a well-formed formula  DRC.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
SQL and Relational Algebra Zaki Malik September 02, 2008.
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 SQL: Structured Query Language (‘Sequel’) Chapter 5.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
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.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
RELATIONAL ALGEBRA (III) Prof. Sin-Min LEE Department of Computer Science.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
Exercises ReservesSailorsBoats Find names of sailors who’ve reserved boat #103 Basic operations: Selection ( σ ) gives a subset of rows. Projection ( π.
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.
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
Exercises Find the names of sailors who’ve reserved boat #103 { N |  S  Sailors (S.name = N.name   R  Reserves(S.sid = R.sid  R.bid = 103)) }
Relational Algebra.
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.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CMPT 258 Database Systems SQL Queries (Chapter 5).
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
CSCI 4333 Database Design and Implementation – Exercise (2) Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
Relational Algebra & Calculus
RELATIONAL ALGEBRA (II)
Database Systems (資料庫系統)
Basic SQL Queries Go over example queries, like 10 > ALL.
© פרופ' יהושע שגיב, האוניברסיטה העברית
CS4432: Database Systems II
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Relational Algebra Chapter 4, Part A
Basic SQL Lecture 6 Fall
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Database Systems 10/13/2010 Lecture #4.
Relational Algebra.
CS 405G: Introduction to Database Systems
CSCI 6315 Applied Database Systems – Exercise (3)
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 - part I.
SQL: The Query Language Part 1
CS4222 Principles of Database System
CENG 351 File Structures and Data Managemnet
Relational Algebra & Calculus
Relational Algebra Chapter 4 - part I.
Presentation transcript:

Chapter (7): Advanced SQL SELECT Statement Chapter (7): Advanced SQL

Find names of sailors who’ve reserved boat #103 Reserves SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103

Find names of sailors who’ve reserved boat #103 Reserves SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103

CROSS JOIN Sailors Reserves SELECT * FROM Sailors S, Reserves R FROM Sailors S CROSS JOIN Reserves R

INNER-JOIN Sailors Reserves SELECT * FROM Sailors S, Reserves R WHERE S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R ON S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R USING sid

EQUI-JOIN Sailors Reserves SELECT * FROM Sailors S, Reserves R WHERE S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R ON S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R USING sid

NATURAL-JOIN SELECT * FROM Sailors S INNER JOIN Reserves R USING sid FROM Sailors S NATURAL JOIN Reserves R USING sid

Report all the sailors with their reservations LEFT OUTER-JOIN Report all the sailors with their reservations SELECT * FROM Sailors S, Reserves R WHERE S.sid=R.sid SELECT * FROM Sailors S LEFT OUTER JOIN Reserves R WHERE S.sid=R.sid

Report all the sailors with their reservations LEFT OUTER-JOIN Report all the sailors with their reservations SELECT * FROM Sailors S, Reserves R WHERE S.sid=R.sid SELECT * FROM Sailors S NATURAL LEFT OUTER JOIN Reserves R WHERE S.sid=R.sid

Report all the boats with their reservations RIGHT OUTER-JOIN Report all the boats with their reservations Reserves Boats SELECT * FROM Reserves R RIGHT OUTER-JOIN Boats B WHERE R.bid=B.bid

Find sailors’ names who’ve reserved at least one boat Reserves SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid

Find names of sailors who’ve reserved a red or a green boat SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’) 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 WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

Find names of sailors who’ve reserved a red and a green boat SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ AND B.color=‘green’) 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 WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

Find names of sailors who’ve reserved a red and a green boat SELECT S.sid FROM Sailors S, 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 FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’) 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 WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

Find names of sailors who’ve reserved a red and not a green boat SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ AND S.sid NOT IN (SELECT S2.sid FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’) SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ EXCEPT WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

SELF-JOIN

VIEW CREATE VIEW Emp_Manager AS SELECT * FROM Emp_Manager SELECT MANAGER FROM Emp_Manager SELECT MANAGER FROM Emp_Manager WHERE EMPLOYEEID = 123 Security …