CHAPTER 7: ADVANCED SQL.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Introduction to Structured Query Language (SQL)
© 2007 by Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 8 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Introduction to Structured Query Language (SQL)
IS 4420 Database Fundamentals Chapter 8: Advanced SQL Leon Chen
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
1 IS 4420 Database Fundamentals Chapter 7: Introduction to SQL Leon Chen.
Introduction to Structured Query Language (SQL)
Terms - data,information, file record, table, row, column, transaction, concurrency Concepts - data integrity, data redundancy, Type of databases – single-user,
SQL Tutorials To understand some of the topics please analyze the following tutorials: The following tutorials will help:
© 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 CHAPTER 7: ADVANCED SQL Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh,
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8 (Part b): Advanced SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer,
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Modern Database Management
Chapter 9 Joining Data from Multiple Tables
1 Chapter 8: Advanced SQL. Chapter 8 2 Processing Multiple Tables – Joins Join – a relational operation that causes two or more tables with a common domain.
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 Introduction to MySQL & SQL. MySQL Relational Database Management System Relational Database Management System Competitors Competitors –Oracle –IBM.
Dr. Chen, Data Base Management Chapter 7: Advanced SQL Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane,
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7: Advanced SQL Modern Database Management 10 th Edition Jeffrey A. Hoffer, V. Ramesh,
Using Special Operators (LIKE and IN)
Chapter 7: advanced sql Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
1 Chapter 8: Advanced SQL Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Modern Database Management Chapter 8: Advanced SQL.
Chapter 4 Multiple-Table Queries
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer, Mary B.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 7: advanced sql Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
SQL Overview Structured Query Language
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Chapter 4, Part A: Logical Database Design and the Relational Model
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Chapter 8 1 Lecture Advanced SQL. Chapter 8 2 Processing Multiple Tables–Joins Join – a relational operation that causes two or more tables with.
IFS180 Intro. to Data Management Chapter 10 - Unions.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Advanced SQL.
CompSci 280 S Introduction to Software Development
SQL Query Getting to the data ……..
Chapter 7: advanced SQL (Part 2 – Subqueries)
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Database Systems: Design, Implementation, and Management Tenth Edition
Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott,
Lecture 2 The Relational Model
Using the Set Operators
David M. Kroenke and David J
Using the Set Operators
Chapter 3 The Relational Database Model
CHAPTER 6: INTRODUCTION TO SQL
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Indexes.
Database Processing: David M. Kroenke’s Chapter Seven:
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Using the Set Operators
Displaying Data from Multiple Tables
Presentation transcript:

CHAPTER 7: ADVANCED SQL

Outline Processing Multiple Tables in SQL. Recommendations for building better SQL queries. Ensuring transaction integrity.

Processing Multiple Tables Joins Equi-join / Inner join Natural join Outer join (Left Outer join + Right Outer join) Self join Subqueries Unions

Processing Multiple Tables using Joins Join: a relational operation that causes two or more tables with a common domain to be combined into a single table or view. Equi-join / Inner join: a join in which the joining condition is based on equality between values in the common columns; common columns appear redundantly in the result table Natural join: an equi-join in which one of the duplicate columns is eliminated in the result table Outer join (left outer join + right outer join + full outer join): a join in which rows that do not have matching values in common columns are nonetheless included in the result table (as opposed to inner join, in which rows must have matching values in order to appear in the result table) Self join The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1:M relationships.

Example - EER Model

Example – Relational Model Customer(customerID, customerName, customerCity, customerState, customerPostalCode) Product(ProductID, productDescription, productFinish, productStandardPrice, productLineID, customerID) Order(orderID, orderDate) OrderLine(orderID, productID, quantity)

Customer_T and Order_T tables with pointers from customers to their orders These tables are used in queries that follow

1. EQUI-JOIN / Inner Join Customer ID appears twice in the result Question: For each customer who placed an order, what is the customer’s name and order number? SELECT Customer_T.CustomerID, Order_T.CustomerID, CustomerName, OrderID FROM Customer_T, Order_T WHERE Customer_T.CustomerID = Order_T.CustomerID ORDER BY OrderID; FROM Customer_T INNER JOIN Order_T ON Customer_T.CustomerID = Order_T.CustomerID An INNER join will only return rows from each table that have matching rows in the other.

2. NATURAL JOIN Question: For each customer who placed an order, what is the customer’s name and order number? SELECT Customer_T.CustomerID, CustomerName, OrderID FROM Customer_T NATURAL JOIN Order_T ON Customer_T.CustomerID = Order_T.CustomerID;

3. OUTER JOIN (left outer join + Right outer join + Full outer join) Question: List the customer name, ID number, and order number for all customers. Include customer information even for customers that do have an order. SELECT Customer_T.CustomerID, CustomerName, OrderID FROM Customer_T LEFT OUTER JOIN Order_T ON Customer_T.CustomerID = Order_T.CustomerID; LEFT OUTER JOIN clause causes customer data to appear even if there is no corresponding order data. Unlike INNER join, this will include customer rows with no matching order rows.

4. Self Join Question: What are the employee ID and name of each employee and the name of his or her supervisor (label the supervisor’s name as manager)? SELECT E.EmplyeeID, E.EmployeeName, M.EmployeenAME as Manager FROM Employee_T E, Employee_T M WHERE E.EmployeeSupervisor = M.EmplyeeID; The same table is used on both sides of the join; distinguished using table aliases Self-joins are usually used on tables with unary relationships.

MULTIPLE TABLE JOIN EXAMPLE Question: Assemble all information necessary to create an invoice for order number 1006. SELECT Customer_T.CustomerID, CustomerName, CustomerAddress, CustomerCity, CustomerState, CustomerPostalCode, Order_T.OrderID, OrderDate, OrderedQuantity, ProductDescription, StandardPrice, (OrderedQuantity * ProductStandardPrice) FROM Customer_T, Order_T, OrderLine_T, Product_T WHERE Order_T.CustomerID = Customer_T.CustomerID AND Order_T.OrderID = OrderLine_T.OrderID AND OrderLine_T.ProductID = Product_T.ProductID AND Order_T.OrderId = 1006;

Results from a four-table join (edited for readability) From CUSTOMER_T table From ORDER_T table From PRODUCT_T table

PROCESSING MULTIPLE TABLES USING SUBQUERIES Subquery: placing an inner query (SELECT statement) inside an outer query Options: In a condition of the WHERE clause. As a “table” of the FROM clause. Within the HAVING clause.

SUBQUERY EXAMPLE Question: Show all customers who have placed an order. SELECT CustomerName FROM Customer_T WHERE CustomerID IN (SELECT DISTINCT CustomerID From Order_T); The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery. Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query

JOIN VS. SUBQUERY Some queries could be accomplished by either a join or a subquery. Question: What are the name and address of the customer who placed order number 1008? Using JOIN: SELECT CustomerName, CustomerAddress, CustomerCity, CustomerState, CustomerPostalCode FROM Customer_T, Order_T WHERE Customer_T.CustomerID = Order_T.CustomerID AND OrderID =1008; Using Subquery: WHERE Customer_T.CustomerID = (SELECT Order_T.CustomerID FROM Order_T WHERE OrderID =1008);

PROCESSING MULTIPLE TABLES USING unions Combine the output (union of multiple queries) together into a single result table. With UNION queries, the quantity and data types of the attributes in the SELECT clauses of both queries must be identical.

TIPS FOR DEVELOPING QUERIES Be familiar with the data model (entities and relationships). Understand the desired results. Know the attributes desired in results. Identify the entities that contain desired attributes. Construct a WHERE equality for each link. Fine tune with GROUP BY and HAVING clauses if needed. Consider the effect on unusual data.

QUERY EFFICIENCY CONSIDERATIONS Instead of SELECT *, identify the specific attributes in the SELECT clause; this helps reduce network traffic of result set. Limit the number of subqueries; try to make everything done in a single query if possible. If data is to be used many times, make a separate query and store it as a view.

GUIDELINES FOR BETTER QUERY DESIGN Understand how indexes are used in query processing. Use compatible data types for fields and literals. Write simple queries. Break complex queries into multiple simple parts. Don’t combine a query with itself (if possible avoid self-joins). Create temporary tables for groups of queries. Retrieve only the data you need. Don’t have the DBMS sort without an index. Learn!

ENSURING TRANSACTION INTEGRITY Transaction: A discrete unit of work that must be completely processed or not processed at all. May involve multiple updates. If any update fails, then all other updates must be cancelled. SQL commands for transactions: BEGIN TRANSACTION/END TRANSACTION Marks boundaries of a transaction COMMIT Makes all updates permanent ROLLBACK Cancels updates since the last COMMIT An SQL Transaction sequence (in pseudocode)

DATA DICTIONARY FACILITIES System tables that store metadata Users usually can view some of these tables Users are restricted from updating them Some examples in Oracle 11g DBA_TABLES – descriptions of tables DBA_CONSTRAINTS – description of constraints DBA_USERS – information about the users of the system Examples in Microsoft SQL Server 2008 sys.columns – table and column definitions sys.indexes – table index information sys.foreign_key_columns – details about columns in foreign key constraints

EMBEDDED AND DYNAMIC SQL Embedded SQL Including hard-coded SQL statements in a program written in another language such as C or Java Dynamic SQL Ability for an application program to generate SQL code on the fly, as the application is running