Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee.

Slides:



Advertisements
Similar presentations
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Advertisements

E-R Diagram for a Banking Enterprise
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL Database Management Systems I Alex Coman, Winter 2006.
Multimedia Information Systems CS Outlines Introduction to DMBS Relational database and SQL B + - tree index structure.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Aandachtspunten Deadlines – alles op tijd inleveren! n Lees goed wat van je verwacht wordt!
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
SQL Lecture 3 SQL Lecture 3. 2 SQL SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
Dr. Kalpakis CMSC 461, Database Management Systems URL: SQL.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 3: SQL.
-- Introduction to database principles Maoying Wu March 11, 2013 Chapter 3: Basic SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
International Computer Institute, Izmir, Turkey SQL Asst.Prof.Dr.İlker Kocabaş UBİ502 at
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections And then u Subqueries,
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL Bambang.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
SQL and Relational Algebra
Structured Query Language
Chapter 3: SQL.
Module 4: SQL.
CS 480: Database Systems Lecture 17 February 22, 2013.
Structured Query Language (Data Manipulation Language)
Chapter 3: SQL.
Structured Query Language (SQL)
Chapter 3: SQL.
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter 3: SQL.
Introduction to Database
Presentation transcript:

Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee

Terms For a query, in- tests for set membership select- chooses values not in - absence of set membership

Example 1: Find all customers who have both a loan and an account at a bank. Start with the bank account (select customer_name from depositor)

Find the list of customers who also have a loan account. combine using the outer select. select distinct customer_name from borrower where customer_name in (select customer_name from depositor) Example 1

Example 2: Find all customers who have a loan from the bank but not an account. select distinct customer_name from borrower where customer_name not in (select customer_name from depositor)

Example 2: Find the names of all customers who are not Smith nor Jones not in can also be used for enumerated sets. select distinct customer_name from borrower where customer_name not in (‘Smith’, ’Jones’)

Example 3: Find the names of all branches that have assets greater than those of at least one in Brooklyn Set comparison using comparing operators Use the rename operator as to compare elements in the same table select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = ‘Brooklyn’

Example 4: Another way using the “some” operator some- at least one select branch_name from branch where assets > some (select assets from branch where branch_city = ‘Brooklyn’)

Example 5: Find all branches that have an asset value greater than that of each branch in Brooklyn. = some - the same as in <> all - the same as not in select branch_name from branch where assets > all (select assets from branch where branch_city = ‘Brooklyn’)

Example 6: Find the branch with the highest average balance. SQL cannot compose of aggregate functions (function of a function), so it must be written in a certain way. select branch_name from account group by branch_name having avg (balance) >= all (select avg (balance) from account group by branch_name)

Example 7: find all customers who have both a loan and an account at the bank. exists - returns true if the argument in it is not empty select customer_name from borrower where exists (select * from depositor where depositor.customer_name = borrower.customer_name)

Example 8: not exists- superset operation. To show that relation A contains relation B, use not exists (B except A)