SQL – Subqueries.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Joining Multiple Tables
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
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Advanced SQL SMSU Computer Services Short Course.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
© 2007 by Prentice Hall6-1 Introduction to Oracle 10g Chapter 6 Creating Multitable Queries and Views James Perry and Gerald Post.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4 Multiple-Table Queries
9 Advanced Query Formulation with SQL (Chapter 9).
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making Chapter.
Joins, views, and subqueries CMSC 461 Michael Wilson.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Advanced Relational Algebra & SQL (Part1 )
Chapter 12 Subqueries and Merge Statements
SQL Select Statement IST359.
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.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
U:/msu/course/cse/103 Day 08, Slide 1 Debrief Homework What problems arose in trying to import the data from Classical_Music.xls?
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL – join.
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
CS122 Using Relational Databases and SQL
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Database Systems: Design, Implementation, and Management Tenth Edition
SQL Query Joins ASP.Net 2.0 – Visual Studio 2005
David M. Kroenke and David J
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL Fundamentals in Three Hours
Views.
Dead Patients Visiting
Database Systems: Design, Implementation, and Management Tenth Edition
Geo-Databases: lecture 4 Complex Queries in SQL
Manipulating Data Lesson 3.
Relational Database Operators
Trainer: Bach Ngoc Toan– TEDU Website:
CS122 Using Relational Databases and SQL
Presentation transcript:

SQL – Subqueries

Chinook Dataset chinookdatabase.codeplex.com It is a large (~1MB) of many tables representing a media library (a la iTunes and a linked employee database.

Union UNION combines the result sets of two queries. Column data types in the two queries must match. UNION combines by column position rather than column name. If we wanted all the names of Artists and Albums: SELECT Artist.Name FROM Artist UNION SELECT Album.Title FROM Album; http://msutoday.msu.edu/news/2015/msu-union-hours-to-change-for-summer-semester/

union all UNION performs a DISTINCT on the result set, eliminating any duplicate rows. UNION ALL does not remove duplicates, and it therefore faster to perform than UNION. If I wanted the Artists and Album names, but duplicates were okay: SELECT Artist.Name FROM Artist UNION ALL SELECT Album.Title FROM Album;

FULL OUTER JOIN (Take 2) If I wanted all the Artist and Album pairs (with NULL if unmatched): SELECT Artist.Name, Album.Title FROM Artist LEFT OUTER JOIN Album ON Artist.ArtistId = Album.ArtistId UNION ALL -- combining two left joins FROM Album LEFT OUTER JOIN Artist WHERE Artist.ArtistID IS NULL; -- exclude repeated inner join

Can you write a FULL Outer join now? Perhaps with a few minutes of time. Sure, it makes sense to me. Josh is a bastard.

When is a Left outer join the same as an inner join? Never. When the left rows always has a match. When the left rows don't have NULLs. Is this the set up of a joke?

http://www. codeproject http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg

Subqueries A subquery is a SQL query within a query. Subqueries are nested queries that provide data to the enclosing query. Subqueries can return individual values or a list of records Subqueries must be enclosed with parenthesis If I wanted all the Artists who released an Album with just their own name: SELECT Artist.Name FROM Artist WHERE Artist.Name IN (SELECT Album.Title FROM Album);

Exists WHERE EXISTS tests for the existence of any records in a subquery. EXISTS returns true if the subquery returns one or more records. EXISTS is commonly used with correlated subqueries (nested subqueries that use values in the outer query). If I want all the Tracks that appear in a Playlist: SELECT Track.Name FROM Track WHERE EXISTS (SELECT PlaylistTrack.TrackId FROM PlaylistTrack WHERE Track.TrackId = PlaylistTrack.TrackId);

Does the columns returned by an exists subquery matter? The first value returned matters, the rest don't Yes you can only have one column returned Nope Can you go back one slide?

All the same SELECT * FROM Track WHERE EXISTS (SELECT PlaylistTrack.TrackId FROM PlaylistTrack WHERE Track.TrackId = PlaylistTrack.TrackId); (SELECT * FROM PlaylistTrack (SELECT 1 FROM PlaylistTrack