COMP 430 Intro. to Database Systems

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 22, 2005.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Extended SQL & The Relational Calculus.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Introduction to SQL J.-S. Chou Assistant Professor.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
CHAPTER 8: MANAGING DATA RESOURCES. File Organization Terms Field: group of characters that represent something Record: group of related fields File:
CT214 – Logical Foundations of Computing Lecture 6 Predicate Calculus.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Indexes and Views Unit 7.
IST 210 The Relational Language Todd S. Bacastow January 2004.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Advanced Data Modeling. Heterogeneous Mapping Heterogeneous Mapping is the ability of MSTR7 tools to join on unlike column names. Heterogeneous Mapping.
Introduction.  Administration  Simple DBMS  CMPT 454 Topics John Edgar2.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
COMP 430 Intro. to Database Systems Grouping & Aggregation Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!
Chapter 13 Query Optimization Yonsei University 1 st Semester, 2015 Sanghyun Park.
Chapter 13: Query Processing
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Midterm Review. Main Topics ER model Relational model Relational Database Design (Theory)
SQL Unit 7 Set Operations
CS3220 Web and Internet Programming More SQL
Structured Query Language
Chapter 3 Introduction to SQL(2)
Slides are reused by the approval of Jeffrey Ullman’s
CpSc 3220 The Language of SQL
Introduction to Database Systems, CS420
COMP 430 Intro. to Database Systems
Chapter 12: Query Processing
CS 440 Database Management Systems
Chapter 2: Intro to Relational Model
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
SQL Structured Query Language 11/9/2018 Introduction to Databases.
More SQL Nested and Union queries, and more
CS 405G: Introduction to Database Systems
Introduction to Database Systems CSE 444 Lecture 03: SQL
January 19th – Subqueries 2 and relational algebra
Chapter Name SQL: Data Manipulation
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
MIS2502: Review for Exam 1 JaeHwuen Jung
CSC 453 Database Systems Lecture
Introduction to Database Systems CSE 444 Lecture 03: SQL
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
SQL Introduction Standard language for querying and manipulating data
CMPT 354: Database System I
MIS2502: Review for Exam 1 Aaron Zhi Cheng
Advance Database Systems
CMPT 354: Database System I
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
CSC 453 Database Systems Lecture
Database Management System
Topic 12 Lesson 2 – Retrieving Data with Queries
Geo-Databases: lecture 4 Complex Queries in SQL
Equivalence of Aggregate Queries in Conjunctive QL
Select-From-Where Statements Multirelation Queries Subqueries
CSC 453 Database Systems Lecture
Presentation transcript:

COMP 430 Intro. to Database Systems Quantification

Review of SQL conditions Compare elements of same record Combine with join to work with multiple records Compare aggregations Check membership – IN

Existential quantification EXISTS (subquery) returns a Boolean. SELECT * FROM Customer WHERE EXISTS (SELECT * FROM Order WHERE Customer.id = customer_id); {c | Customer(c)  ( o. Order(o)  c.id = o.customer_id)} Subquery’s SELECT irrelevant. Customer Order id name 1 Joe 4 Mary 3 Scott 6 Elizabeth id customer_id Item 105 4 Shoes 107 Pants 108 1 109 3 Tie id name 1 Joe 4 Mary 3 Scott

Existential quantification negated SELECT * FROM Customer WHERE NOT EXISTS (SELECT * FROM Order WHERE Customer.id = customer_id); {c | Customer(c)  (  o. Order(o)  c.id = o.customer_id)} Customer Order id name 1 Joe 4 Mary 3 Scott 6 Elizabeth id customer_id Item 105 4 Shoes 107 Pants 108 1 109 3 Tie id name 6 Elizabeth

Existential quantification – efficiency SELECT * FROM Customer WHERE EXISTS (SELECT * FROM Order WHERE Customer.id = customer_id); {c | Customer(c)  ( o. Order(o)  c.id = o.customer_id)} Executes subquery for each Customer record. However, can stop subquery at first satisfying Order.

Existential quantification negated SELECT * FROM Customer WHERE NOT EXISTS (SELECT * FROM Order WHERE Customer.id = customer_id); {c | Customer(c)  (  o. Order(o)  c.id = o.customer_id)} Executes subquery for each Customer record. However, can stop subquery at first non-satisfying Order.

Logical equivalence of quantifiers  x . R(x) = x.  R(x)  x . R(x) = x.  R(x)

Universal quantification Don’t confuse with slightly different ALL. (Coming soon.) No directly comparable FORALL! Generally less useful idea in SQL. E.g., find Customer that has placed all of the Orders. However, see today’s activity. x . R(x) =  x.  R(x)

EXISTS vs. other approaches SELECT * FROM Customer WHERE EXISTS (SELECT * FROM Order WHERE Customer.id = customer_id); SELECT * FROM Customer WHERE id IN (SELECT customer_id FROM Order); SELECT * FROM Customer WHERE (SELECT Count(*) FROM Order WHERE Customer.id = customer_id); SELECT DISTINCT Customer.id, name FROM Customer, Order WHERE Customer.id = customer_id;

Inconvenient if you want more attributes. EXISTS vs. INTERSECT SELECT id FROM Customer WHERE EXISTS (SELECT * FROM Order WHERE Customer.id = customer_id); SELECT id FROM Customer INTERSECT SELECT customer_id FROM Order; Inconvenient if you want more attributes.

Activity – Use EXISTS in queries Given: Store (id, name, type) City (id, name) CityStore (store_id, city_id) Warm up: The stores that are in at least one city each are of what types? The stores that are in no city each are of what types? The stores that are present in all cities are of what types?

Another form of existential quantification SELECT * FROM Customer WHERE EXISTS (SELECT * FROM Order WHERE Customer.id = customer_id); Can use any comparison operator. Syntactically strange, but think of ANY as part of the comparison operator. SELECT * FROM Customer WHERE id = ANY (SELECT customer_id FROM Order); Or SOME.

Another form of universal quantification SELECT * FROM Product p1 WHERE p1.price >= ALL (SELECT p2.price FROM Product p2); SELECT * FROM Product p1 WHERE NOT (p1.price < ANY (SELECT p2.price FROM Product p2));