Upcoming Work Homework #3 due today by 4:30 Homework #4 due Wed Nov 22. Midterm #2 on Wed Nov 29. It will be open book/note (no computers) If you get a.

Slides:



Advertisements
Similar presentations
Multiple Table Queries
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Relational Database Operators
Copyright © 2004 Pearson Education, Inc.. Chapter 15 Algorithms for Query Processing and Optimization.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Murach's MySQL, C3© 2012, Mike Murach & Associates, Inc.Slide 1.
Greg Riccardi Florida State University. Using SQL to Manipulate Database Content and Structure How to create queries in SQL –Simple select statements.
A DVANCED SQL Joe Meehean 1. SQL S ET O PERATIONS Syntax SELECT column1, column2, … FROM table1… WHERE conditions SET_KEYWORD SELECT column1, column2,
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
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.
SQL SQL (Structured Query Language) is used to define, query, and modify relational databases Every relational database system understands SQL SQL is standard:
SQL Basics Based on the relational algebra we just learned. Nonprocedural language – what to be done not how Simple, powerful language Used for both data.
Another Join? Find videoIds and storeIds for all copies of Annie Hall which aren’t reserved. We can do a join of movie and video on movieId where title.
Your name here The lecture notes are based on using Microsoft Access interactively as part of the lecture.
SQL conventions Writing SQL Statements: conventions vs. requirements We try to write SQL statements so they are readable. That is why we try to put SELECT,
Relational Algebra. Manipulating Databases To access information in a database we use a query Ex: How many customers have the first name = `John’? Good.
Sets and Set Operations A set is an unordered collection of objects called elements Explicit definition Implicit definition Two sets A and B are equal.
Aggregates on Groups of Rows If you want to use aggregate functions to return more than one row of data you can use the GROUP BY clause. You use the GROUP.
CS 157B Midterm 2 Revision Prof. Sin Min Lee. Q #1 (01) 1. Characterize the difference between the following pairs of terms a. Entity and entity class.
Chapter 3 Section 3.4 Relational Database Operators
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Programming using C# Joins SQL Injection Stored Procedures
Lecture8:Data Manipulation in SQL Advanced SQL queries Ref. Chapter5 Lecture8 1.
Today Collection of unrelated stuff Questions? HW –4 -5 & drop lowest –emphasize project Projects Functional Dependencies? –Someone asked why: Keys, Normal.
Relational Algebra References: Databases Illuminated by Catherine Ricardo, published by Jones and Bartlett in 2004 Fundamentals of Relational Databases.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Chapter 4 Multiple-Table Queries
CpSc 3220 The Language of SQL The Language of SQL Chapters
Techniques for Manipulating Relational Data By Herbert A. Evans.
Relational Algebra – Part 2
CSc340 1b1 The Relational Model Chapter 2 Database Schema Keys Schema Diagrams Relational Query Languages Relational Operations.
Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
Copyright © 2003 Addison-Wesley Timeline for Database Systems Developments before 1960 transition from punched card and tape 1960s, from file management.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 7: SQL, the Structured Query Language Instructor’s name and.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
Advanced Relational Algebra & SQL (Part1 )
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
April 2002Information Systems Design John Ogden & John Wordsworth SQL2: 1 Database Design SQL (2) John Wordsworth Department of Computer Science The University.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL – join.
Day 4 - Simple Queries & More on Tables
Lecture 5 Relational Algebra and Calculus SQL Syntax
LECTURE 3: Relational Algebra
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
The Relational Algebra
CS 3630 Database Design and Implementation
SQL Fundamentals in Three Hours
CMPT 354: Database System I
Data Manipulation using Relational Algebra
CMPT 354: Database System I
The Relational Algebra
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
SQL set operators and modifiers.
Unit Relational Algebra 1
Chapter 4 Relational Algebra
Relational Database Operators
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

Upcoming Work Homework #3 due today by 4:30 Homework #4 due Wed Nov 22. Midterm #2 on Wed Nov 29. It will be open book/note (no computers) If you get a better score on the second than the first then that score will replace your first midterm score. Final Project Due Dec 7 SQL chapters 7-10 this week Mike Stafford guest lecture Mon Nov 20.

SQL Set operations Remember union, intersection, and difference from relational algebra? We can do those same set operations with SQL. The results are unique sets of rows. With Access, we have a union (and union all) but not intersect or except (difference). We can create intersections and differences with IN and NOT IN predicates. Tables must be union compatible to be eligible for set operations. must have same number of columns must have compatible data types (number types, text types) You can do this by using whole tables or taking subsets of tables.

Union vs. Join What’s the difference between a union and a join? all columns compatible instead of just join column number columns must be equal with union duplicates removed with union Union is used to implement FULL OUTER JOIN in Access SELECT with left outer join UNION SELECT with right outer join

accountIdlastNamefirstNamestreetcitystatezipcodebalance 101BlockJane345 Randolph CircleApopkaFL30458-$ HamiltonCherry3230 Dade St.Dade CityFL30555-$ HarrisonKatherine103 Landis HallBrattFL30457-$ BreauxCarroll76 Main St.ApopkaFL30458-$ MorehouseAnita9501 Lafayette St.HoumaLA44099-$ DoeJane123 Main St.ApopkaFL30458-$ GreavesJoseph14325 N. Bankside St.GodfreyIL43580-$ DoeJaneCawthon Dorm, room 642TallahasseeFL32306-$ RiccardiGreg101 Thanet St.LondonFL33333-$ RiccardiGreg101 Thanet St.LondonFL33333-$ RiccardiGreg101 Thanet St.LondonFL33333-$ MylopoulosJanet4402 Elm St.ApopkaFL33455-$ MylopoulosJanet4402 Elm St.ApopkaFL33455-$ MylopoulosJanet4402 Elm St.ApopkaFL33455-$ O'ConnellJanet4402 Elm St.ApopkaFL33455-$ O'ConnellJanet4402 Elm St.ApopkaFL33455-$0.00 Customer ssnlastNamefirstName UnoJane ToulouseJie ThreatAyisha FortuneJulian FivozinskyBruce Employee

Union SELECT c.firstname, c.lastname FROM Customer c union SELECT e.firstName, e.lastName FROM Employee e; firstnamelastname AnitaMorehouse AyishaThreat BruceFivozinsky CarrollBreaux CherryHamilton GregRiccardi JaneBlock JaneDoe JaneUno JanetMylopoulos JanetO'Connell JieToulouse JosephGreaves JulianFortune KatherineHarrison

Union All firstnamelastname JaneBlock CherryHamilton KatherineHarrison CarrollBreaux AnitaMorehouse JaneDoe JosephGreaves JaneDoe GregRiccardi GregRiccardi GregRiccardi JanetMylopoulos JanetMylopoulos JanetMylopoulos JanetO'Connell JanetO'Connell JaneUno JieToulouse AyishaThreat JulianFortune BruceFivozinsky Duplicates not removed. SELECT c.firstname, c.lastname FROM Customer c union all SELECT e.firstName, e.lastName FROM Employee e;

INTERSECTION & DIFFERENCE Access doesn’t have SQL EXCEPT (DIFFERENCE) or INTERSECT so we have to use IN and NOT IN. Two sets A and B. Intersection of AB? SELECT FROM A WHERE also IN B A-B? SELECT FROM A WHERE NOT IN B

accountIdvideoIddateRenteddateDuecost /3/20021/4/2002$ /24/20025/2/2002$ /24/20024/30/2002$ /22/20022/25/2002$ /22/20022/25/2002$ /1/200112/31/2001$ /14/20022/16/2002$ /24/ /1/20021/8/2002$ /1/20021/4/2002$3.49 Rental accountIdvideoIddateRenteddateReturnedcost /9/200112/10/2001$ /13/20011/4/2001$ /15/2002 $ /1/200112/3/2001$ /4/200112/6/2001$ /1/20021/4/2002$ /9/200112/14/2001$ /14/20021/24/2002$3.35 PreviousRental

In predicate Find Customers (accountId, lastname and firstname) who have previously rented video number 101 using In predicate. SELECT c.accountId, c.lastName, c.firstName FROM Customer c WHERE c.accountId IN (SELECT p.accountId FROM previousRental p WHERE videoId=101); accountIdlastNamefirstName 101BlockJane 111DoeJane

NOT IN Find the customers who have not previously rented video 101. SELECT c.accountId, c.lastName, c.firstName FROM Customer c WHERE c.accountId NOT IN (SELECT p.accountId FROM previousRental p WHERE videoId=101); accountIdlastNamefirstName 102HamiltonCherry 103HarrisonKatherine 104BreauxCarroll 106MorehouseAnita 201GreavesJoseph 444DoeJane 445RiccardiGreg 446RiccardiGreg 447RiccardiGreg 448MylopoulosJanet 449MylopoulosJanet 450MylopoulosJanet 451O'ConnellJanet 452O'ConnellJanet

A more complicated query Find customers who have previously or are currently renting video 101. SELECT u.accountId, lastName, firstName FROM ((SELECT p.accountId, p.videoId FROM previousRental p UNION SELECT r.accountId, r.videoId FROM rental r) u INNER JOIN Customer c ON u.accountId=c.accountId) WHERE videoId=101; accountIdlastNamefirstName 101BlockJane 103HarrisonKatherine 111DoeJane

Same query with IN SELECT c.accountId, c.lastName, c.firstName FROM Customer c WHERE c.accountId IN (SELECT r.accountId FROM Rental r WHERE r.videoId=101) OR c.accountId IN (SELECT p.accountId FROM PreviousRental p WHERE p.videoId=101);