Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &

Slides:



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

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database Systems More SQL Database Design -- More SQL1.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Using Special Operators (LIKE and IN)
Creating Databases for web applications [Complete presentations] More SQL Class time: discuss final projects. Do posting if you have not done it.
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,
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
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.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
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.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Structured Query Language used for defining and manipulating data in Relational DBs aimed at: –reducing training costs –increasing productivity –improve.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
Lab 13 Databases and SQL.
Day 4 - Simple Queries & More on Tables
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
The Database Exercises Fall, 2009.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Database Queries.
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
The Relational Model Textbook /7/2018.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Database systems Lecture 3 – SQL + CRUD
SQL: Structured Query Language
CS122 Using Relational Databases and SQL
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Section 4 - Sorting/Functions
Projecting output in MySql
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Database SQL.
Relational Algebra Chapter 4 - part I.
Introduction to SQL Server and the Structure Query Language
Group Operations Part IV.
Presentation transcript:

Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation & Examples of Aggregation

CSE 498Day 5 - More Complexity With Queries2 / 12 What is a join? SQL allows you to cross reference information in tables. SQL allows you to cross reference information in tables. There are two methods to do this: There are two methods to do this: –Compare & Contrast: Compare values in a given table against another and get a matching pair result table from the data. –JOIN: Join the two tables into a temporary table based on some rules.

CSE 498Day 5 - More Complexity With Queries3 / 12 More Complex Movie DB A complex version of the Movie DB will be used for this set of slides. Here is the DB schema… A complex version of the Movie DB will be used for this set of slides. Here is the DB schema… Inventory (TapeID,MovieID) Inventory (TapeID,MovieID) Movies (MovieID, MovieName) Movies (MovieID, MovieName) Suppliers (SupplierID, SupplierName) Suppliers (SupplierID, SupplierName) MovieSupplier (SupplierID, MovieID, Price) MovieSupplier (SupplierID, MovieID, Price) Orders (OrderID, SupplierID, MovieID, Copies) Orders (OrderID, SupplierID, MovieID, Copies) This database is for a movie company. This database keeps track of the possible movies available on video. This database also keeps track of different suppliers in the company and the videos they have ordered. This database is for a movie company. This database keeps track of the possible movies available on video. This database also keeps track of different suppliers in the company and the videos they have ordered.

CSE 498Day 5 - More Complexity With Queries4 / 12 Compare & Contrast Tables This is done by putting a statement in the WHERE clause of a query. This is done by putting a statement in the WHERE clause of a query. This will simply compare the two tables and add the matching pairs to the result set. This will simply compare the two tables and add the matching pairs to the result set. Example: Give me the list of all the movies in the inventory. Example: Give me the list of all the movies in the inventory. SELECT DISTINCT M.MovieName SELECT DISTINCT M.MovieName FROM Movies M, Inventory I FROM Movies M, Inventory I WHERE M.MovieID = I.MovieID WHERE M.MovieID = I.MovieID

CSE 498Day 5 - More Complexity With Queries5 / 12 What about JOIN? There are three ways to use JOIN… There are three ways to use JOIN… –INNER JOIN –LEFT JOIN –RIGHT JOIN INNER JOIN INNER JOIN –Does the same thing as compare & contrast. –Syntax : INNER JOIN ON –Syntax : INNER JOIN ON –Best described as pulling together two tables that sit at the same level.

CSE 498Day 5 - More Complexity With Queries6 / 12 LEFT JOIN / RIGHT JOIN Idea of These Joins Idea of These Joins –The result table comes from two tables...and upper table and a lower table. Theory Behind Upper & Lower Tables Theory Behind Upper & Lower Tables –All the records of the upper table are included in the result table. –All matching instances with the lower table are included in the result table with the pair from the higher table. –All records in the upper table that have no matching instance in the lower table have their lower table values filled in with NULL.

CSE 498Day 5 - More Complexity With Queries7 / 12 LEFT / RIGHT cont. Syntax is similar to an INNER JOIN... Syntax is similar to an INNER JOIN... –LEFT JOIN Syntax LEFT JOIN ON LEFT JOIN ON –RIGHT JOIN Syntax RIGHT JOIN ON RIGHT JOIN ON Useful if you’re looking for non-matching pairs... Useful if you’re looking for non-matching pairs... –IS NULL This can be used in the WHERE clause to check if an attribute to some record has a null value. This can be used in the WHERE clause to check if an attribute to some record has a null value. –IS NOT NULL This also can be used in the WHERE clause... This also can be used in the WHERE clause...

CSE 498Day 5 - More Complexity With Queries8 / 12 Example data and Question Given... What movie titles are not in the movie inventory? First Query Try... SELECT * FROM Movie M LEFT JOIN Inventory I ON M.MovieID = I.MovieID; Gives the result... Inventory TapeIDMovieID Movies MovieIDMovieName 1Star Wars 2Empire Strikes Back 3Return of the Jedi MovieIDMovieNameTapeID 1Star Wars1 2Empire Strikes BackNULL 3Return of the Jedi2 3Return of the Jedi3

CSE 498Day 5 - More Complexity With Queries9 / 12 Answer & A New Question... What movie titles are not in the movie inventory? Second Query Try...only look for the null valued answers... SELECT * FROM Movie M LEFT JOIN Inventory I ON M.MovieID = I.MovieID WHERE I.TapeID IS NULL; Gives the result... Thus, just change the SELECT statement to what you want and you’re done! Try this query... Question: What are the names of the movies in the movie inventory? (Hint: Do I have to watch for duplicates?) MovieIDMovieNameTapeID 2Empire Strikes BackNULL

CSE 498Day 5 - More Complexity With Queries10 / 12 What is aggregation? Aggregation is the ability to do simple mathematical functions on groups of data in the result table. Aggregation is the ability to do simple mathematical functions on groups of data in the result table. Methods provided in MySQL... Methods provided in MySQL... –SUM : Adds up any grouped values –MIN : Finds the smallest value in the set given –MAX : Finds the largest value in the set given –AVG : Finds the mean value of the set given –COUNT : Counts up the number of records in the set given To understand how to use aggregation, you have to understand how a query is executed... To understand how to use aggregation, you have to understand how a query is executed...

CSE 498Day 5 - More Complexity With Queries11 / 12 Understanding a Query... Query Format Query Format SELECT SELECT FROM FROM WHERE WHERE GROUP BY GROUP BY HAVING HAVING Execution Order of the Query... Execution Order of the Query... –1st: It finds the tables/joined tables it needs in the FROM clause. –2nd: It evaluates row actions defined in the WHERE clause to produce a temporary result table. –3rd: It uses the GROUP BY and HAVING definitions to know what further actions to take. –4th: It uses the SELECT clause to know what rows to show.

CSE 498Day 5 - More Complexity With Queries12 / 12 Aggregation cont. This can be used in the SELECT or HAVING clause... This can be used in the SELECT or HAVING clause... Examples... Examples... Question: What is the average price of a movie? SELECT AVG(Price) AS Average_Price FROM MovieSupplier; Question: What movie suppliers have a movie that costs less than $9? SELECT DISTINCT S.SupplierName FROM MovieSupplier MS INNER JOIN Supplier S ON S.SupplierID = MS.SupplierID GROUP BY MS.SupplierName HAVING MIN(MS.Price) < 9.00;