1 SQL - Select Union / Intersect / Difference / Division.

Slides:



Advertisements
Similar presentations
Multiple Table Queries
Advertisements

Lesson 1.3 Unit 1, Lesson 3. Set:A collection of distinct objects Elements:All of the objects that make up a set.
SQL, RA, Sets, Bags Fundamental difference between theoretical RA and practical application of it in DBMSs and SQL RA  uses sets SQL  uses bags (multisets)
1 SQL - Select Join / Outer Join Sub queries Join Join Outer join Left outer join Right outer join.
SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Learn basic SQL statements.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
Structured Query Language NEU – CCIS – CSU430 Tony.
Spring 2003 ECE569 Lecture 06.1 ECE 569 Database System Engineering Spring 2003 Topic VIII: Query Execution and optimization Yanyong Zhang
MediaLoft Eastern Division 2003 Fiscal Year. Eastern Division Stores MediaLoft Boston MediaLoft New York MediaLoft Chicago MediaLoft Kansas City.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Venn Diagrams/Set Theory   Venn Diagram- A picture that illustrates the relationships between two or more sets { } are often used to denote members of.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Chapter 3 Section 3.4 Relational Database Operators
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
Fundamentals of Relational Database Operations
1. SQL Header of tables: –Head(Custoemrs) = (cid, cname, city, discnt) –Head(Orders) = (ordno, month, cid, pid, qty, dollars) –Head(Products) = (pid, pname,
1.6 Solving Compound Inequalities Understanding that conjunctive inequalities take intersections of intervals and disjunctive inequalities take unions.
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
Compound Inequalities “And” & “Or” Graphing Solutions.
Slant ( oblique) Asymptote. A slant asymptote for a function is a slant straight line ( that’s either straight line through the origin or a straight line.
Chapter 4 Multiple-Table Queries
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
From Relational Algebra to SQL CS 157B Enrique Tang.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
Texas State Technical College DISCOVER! Union and Union All Bringing things together.
1 Geog 357 – Introduction to GIS The Relational Language.
Advanced Relational Algebra & SQL (Part1 )
Set Notes Day 4. How are these two sets related? A={2,3,4,6} and B={5,1,2,4,3} They both have a 2, 3, and 4. This is the intersection. AB U.
Probability Formulas The probability of more than one outcome. This is a union of the probabilities. If events are disjoint: If events are not disjoint:
MIS 451 Building Business Intelligence Systems Logical Design (1)
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
SQL - miscellaneous D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 1.
1 SQL Insert Update Delete Create table Alter table.
1 2 Concepts of Database Management, 4 th Edition, Pratt & Adamski Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra.
Math 71A 4.2 – Compound Inequalities intersection.
SQL: Sets UNION INTERSECT MINUS D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Sets 1.
1 Database Systems SQL: Advanced Queries. 2 Union, Intersection, and Except (2)   Find the names of those people who are either a graduate student or.
Unions and Intersections of Sets Chapter 3 Section 8.
Solving Compound Inequalities When the word and is used, the solution includes all values that satisfy both inequalities. This is the intersection of the.
Practice SQL Ali Saeed Khan.
CO887 – Lecture 8 SQL 2.
Inequalities and Absolute Value
Confirm teams. Review of diagrams. SELECT problems.
Summary of Relational Algebra
Totals For December 2008.
Relational Algebra - Select & Project

Cse 344 April 4th – Subqueries.
COMP 430 Intro. to Database Systems
הקדמה היתרון במודל הטבלאי: המודל כקבוצה של יחסים (טבלאות)
January 19th – Subqueries 2 and relational algebra
January 17th – Subqueries
دور المجتمع المدني في ترشيد وتحسين كفاءة الطاقة آفاق جديدة ومتجددة...
SQL בסיסי – הגדרה אינדוקטיבית
EACH BOX REPRESENTS ONE SALE/UPGRADE
Data Control Language Grant, Revoke.
Lecture 33: The Relational Model 2
Linear Inequalities and Absolute Value
CS 405G: Introduction to Database Systems
--....,,,. _,-->r 1Jl oEUKAITf INTERNATIONAL T B RA 0 D 7 I 0 N b G.
Lesson 7.1 Solve Linear Systems by Graphing
SQL set operators and modifiers.
Set Operations Union Intersect Minus.
Prill\Meyers Industries

Data base.
Presentation transcript:

1 SQL - Select Union / Intersect / Difference / Division

2 3. Union / Intersect / Difference / Division Union Intersect Difference Division

3 3.1 Union List all cities where either a customer or an agent, or both is based. Customers[city] U Agents[city] Select city from customers union select city from agents Select city from customers union all select city from agents order by city

4 3.1 Union (cont) List all orders (ordno) that are placed by c001 or c002 Select distinct ordno from orders where cid = 'c001' or cid = 'c002‘ Select ordno from orders where cid = 'c001' union select ordno from orders where cid = 'c002'

5 3.2 Intersect Find customers (cid) who order both products p01 and p07. (Customers where pid = p01)[cid] n (Customers where pid = p07)[cid] Select cid from orders where pid = 'p01' and pid = 'p07‘ Select distinct o1.cid from orders o1 where o1.pid = 'p01' and cid in (select cid from orders o2 where o2.pid = 'p07')

6 3.3 Difference Find all customers (cid) where the customer does not place an order through agent a05 (Orders)[cid] - (Orders where aid = ‘a05’)[cid] Select distinct cid from orders where aid != 'a05' Select distinct cid from orders where cid not in (select cid from orders where aid = 'a05')

7 3.4 Division List customers (cid) who place orders with all agents based in New York. Orders[cid, aid] divideby (Agents where city = ‘New York’)[aid] Select cid from orders where aid = all (select aid from agents where city = 'New York')

8 3.4 Division (cont) R÷S = R[A 1..A n ] – ((R[A 1..A n ] X S) – R)[A 1..A n ] Condition 1: An agent based in New York that does not take an order for Customer.cid Condition 2: For customer.Cid, there is not agents that satisfy condition 1. Select cid from customers where condition 2.

9 3.4 Division (cont) Condition 1: An agent based in New York that does not take an order for Customer.cid a.city = ‘New York’ and not exists (select * from orders o where o.cid = c.cid and o.aid = a.aid)

Division (cont) Condition 2: For customer.Cid, there is not agents that satisfy condition 1. Not exists (select * from agents a where condition 1) Not exists (select * from agents a where a.city = ‘New York’ and not exists (select * from orders o where o.cid = c.cid and o.aid = a.aid))

Division (cont) Select cid from customers where condition 2. select c.cid, c.cname from customers c where not exists (select * from agents a where a.city = 'New York' and not exists (select * from orders o where o.cid = c.cid and o.aid = a.aid))

Division (cont) Get the aid values of agents in Duluth who place orders for all products costing more than a dollar.

Division (cont) Condition 1: An product (pid) costing over $1 is not order by an agent Condition 2: For aid, there is not product that satisfy condition 1. Select aid from agents where condition 2 + agents based in Duluth

Division (cont) Condition 1: An product (pid) costing over $1 is not order by an agent p.price > 1.00 and not exists (select * from orders o where o.pid = p.pid and x.aid = a.aid)

Division (cont) Condition 2: For aid, there is not product that satisfy condition 1. Not exists (select pid from products p where condition 1) Not exists (select pid from products p where p.price > 1.00 and not exists (select * from orders o where o.pid = p.pid and x.aid = a.aid))

Division (cont) Select aid from agents where condition 2 + agents based in Duluth select a.aid from agents a where a.city = 'Duluth' and not exists (select pid from products p where p.price > 1.00 and not exists (select * from orders o where o.pid = p.pid and o.aid = a.aid))