BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

BTM 382 Database Management Chapter 6: Normalization of Database Tables Chitu Okoli Associate Professor in Business Technology Management John Molson School.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
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.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
BTM 382 Database Management Chapter 4: Entity Relationship (ER) Modeling Chitu Okoli Associate Professor in Business Technology Management John Molson.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 3 Single-Table Queries
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Component 4/Unit 6f Topic VI: Create simple querying statements for the database The SELECT statement Clauses Functions Joins Subqueries Data manipulation.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
BTM 382 Database Management Chapter 7 Introduction to Structured Query Language (SQL) Chitu Okoli Associate Professor in Business Technology Management.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BTM 382 Database Management Chapter Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 12 Subqueries and Merge Statements
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Component 4: Introduction to Information and Computer Science Unit 6: Databases and SQL Lecture 6 This material was developed by Oregon Health & Science.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
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.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
BTM 382 Database Management Chapter 5: Advanced Data Modeling
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
BTM 382 Database Management Chapter 13: Business intelligence and data warehousing Chapter 14-4: Data analytics Chitu Okoli Associate Professor in Business.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Database Systems: Design, Implementation, and Management Tenth Edition
David M. Kroenke and David J
BTM 382 Database Management Chapter 1: Database systems
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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
Manipulating Data Lesson 3.
Presentation transcript:

BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia University, Montréal

Structure of BTM 382 Database Management  Week 1: Introduction and overview  ch1: Introduction  Weeks 2-6: Database design  ch3: Relational model  ch4: ER modeling  ch6: Normalization  ERD modeling exercise  ch5: Advanced data modeling  Week 7: Midterm exam  Weeks 8-10: Database programming  ch7: Intro to SQL  ch8: Advanced SQL  SQL exercises  Weeks 11-13: Database management  ch2,12: Data models  ch13: Business intelligence and data warehousing  ch9,14,15: Selected managerial topics

Review of Chapter 8: Advanced SQL  What are the main join operators in SQL?  How do you create a subquery?  What are some important Oracle SQL functions?  What are some important advanced SQL features to know about?

SQL Join Operators

Cross join SELECT * FROM CUSTOMER, AGENT

Inner joins: natural join, join using, join on SELECT * FROM CUSTOMER NATURAL JOIN AGENT

Outer joins

Left outer join, Right outer join, Full outer join CUSTOMER left outer join AGENT CUSTOMER right outer join AGENT CUSTOMER full outer join AGENT

Subqueries

Order of creating queries 1.SELECT column list (required) 2.Optional clauses:  WHERE conditions  GROUP BY aggregation  ORDER BY sort order 3.FROM tables (required)  If there is any clause that cannot be easily obtained, then perhaps you need a subquery  Rather than considering what type of subquery you’re dealing with, simply build the subquery in little pieces

Possible results from a SELECT query 0.NULL: No results found  Any of the examples from below might result in NULL 1.Single value: Intersection of one column and one row 2.List of same-type values: One column of values (same datatype)  Might be one column, multiple rows  Might be one column, one row (special case of #1 above)  NOT a row of values (that would be a special case of #3 below)  One column means that all values have the same datatype  One row means that each value has different datatypes 3.Virtual table of values: Multiple columns and multiple rows  Might be multiple columns, multiple rows  Might be multiple columns and only one row  Might be only one column and multiple rows (special case of #2 above)  Might be only one column and only one row (special case of #1 above)

Building subqueries the easy way 1.Don’t think about subqueries; just first try to build a query 2.If the query needs an unknown value from the database to complete it, then you need an inner query to get that missing value.  OK, so you need a subquery. No big deal. 3.What type of missing value do you need? This determines the kind of SELECT query you need for the inner query:  If you need a single value: Inner query returns one column with a unique row  e.g. using a WHERE or HAVING clause, or in an attribute list  If you need a list of values of the same type: Inner query returns one column with one or more rows  e.g. as the input to the IN, ALL, or ANY operators  If you need a virtual table: Inner query returns multiple columns and one or more rows  e.g. as the input to the FROM clause

Multirow subquery operators: ALL, ANY and SOME  Allow comparison of single value with a list of values using inequality comparison  “> ALL” means “greater than the maximum in list”  So, it’s clearer to use > MAX(SELECT …)  “< ALL” means “less than minimum in the list”  So, it’s clearer to use < MIN(SELECT …)  Using “= ANY” operator is equivalent to IN operator  So, it’s clearer to use IN  SOME is exactly the same thing as ANY  I recommend: don’t use ALL, ANY or SOME, since they have very particular meanings and might confuse people who read your code  But here’s a great, detailed explanation of how they worka great, detailed explanation of how they work

SQL Functions

Regular functions versus Aggregate functions  Regular functions (chapter 8)  Input: one or more single values (possibly various datatypes)  Output: one single value  E.g. ADD_MONTHS(OrderDate, 2), SUBSTR(Phone,1,3)  Aggregate functions (chapter 7)  Input: one single column of values (list all of the same datatype)  Output: either  One single value for an entire table; or  One single value per row, if GROUP BY clause is used  E.g. SUM(SalesTotal), AVG(Age)  COUNT  COUNT(*): Use this most of the time, to count all rows  COUNT(Attribute): Will count rows for that attribute only if they are not NULL

Case-insensitive search: select FIRSTNAME, LASTNAME from EMPLOYEE where LOWER(LASTNAME) = 'smith'; You could also use where UPPER(LASTNAME) = 'SMITH' Either of these would match 'Smith‘. Otherwise, they would not match, since string search is case-sensitive by default

Other Advanced SQL Elements

Views  A view is a named, saved query  A virtual table (multiple columns and rows): it can be used wherever a table is expected  Always up-to-date, because it is a query, not a real table  A view’s data is not saved to the database; rather, the SQL query for the view is executed each time it is called  Updatable view  “Updatable” means that it can receive all CRUD DML operations:  Create: INSERT  Read: SELECT (all views can receive the SELECT operation)  Edit: UPDATE  Delete: DELETE  Basic condition to be updatable: the view (saved query) includes a proper primary key and is in 1NF  But some other conditions must apply for a view to be updatable But some other conditions must apply for a view to be updatable  If a view does not meet these conditions, then it is not updateable, and it can only receive the SELECT operation

Stored procedures  Stored procedures and stored functions  Add 3 rd generation programming features to SQL (e.g. if- then, while loops, etc.)  Procedure vs. function: A function returns a value, whereas a procedure does not—that’s the only difference  Triggers  Stored procedure code that is automatically invoked by DBMS on DML events  Enables event-driven programming in the DBMS  Each DBMS has its own stored procedure language  Oracle: Procedural SQL (PL/SQL)  Microsoft Access: Visual Basic for Applications (VBA)  Microsoft SQL Server: Transact SQL (T-SQL)  MySQL: MySQL stored procedures

Oracle Sequences  Oracle sequences  Independent object in the database: you can create and delete them at any time  Very flexible tool for implementing automatic numbering  Autonumber in Oracle  Old way (before Oracle version 12c, including 11g): sequences + triggers sequences + triggers  New way (Oracle 12c): IDENTITY data typeIDENTITY data type

Summary of Chapter 8: Advanced SQL  The main join operators are the cross join, inner joins and outer joins  To create a subquery, you need to understand the possible results of a query and then know where to place them within a compound query  Among other Oracle SQL functions and features, it is important to know ||, TO_DATE, UPPER/LOWER, CASE and SWITCH  Views, stored procedures and Oracle sequences are some important advanced SQL features to know about

Sources  Many of the slides are adapted from Database Systems: Design, Implementation and Management by Carlos Coronel and Steven Morris. 11th edition (2015) published by Cengage Learning. ISBN 13: Database Systems: Design, Implementation and Management  Other sources are noted on the slides themselves