Query Formulation with SQL

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

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Query Formulation with SQL
Database Systems: Design, Implementation, and Management Tenth Edition
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
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)
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 9 Advanced Query Formulation with SQL.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 7 Normalization of Relational Tables.
7-1 Normalization - Outline  Modification anomalies  Functional dependencies  Major normal forms  Practical concerns.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Chapter 9 Advanced Query Formulation with SQL. Outline Outer join problems Type I nested queries Type II nested queries and difference problems Nested.
Chapter 6 Developing Data Models for Business Databases.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 3 The Relational Data Model.
Introduction to SQL J.-S. Chou Assistant Professor.
SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Chapter 7 Normalization. Outline Modification anomalies Functional dependencies Major normal forms Relationship independence Practical concerns.
Chapter 7 Normalization. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Modification anomalies Functional dependencies.
Copyright © 2011 by Michael V. Mannino All rights reserved. Database Design, Application Development, and Administration, 5 th Edition Chapter 3 The Relational.
Chapter 2 The Relational Data Model. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Relational model basics Integrity.
Chapter 14 Data and Database Administration. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Organizational context.
Using Special Operators (LIKE and IN)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 10 Application Development with Views. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Creating.
Chapter 3 Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Background Getting started.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Advanced Query Formulation with SQL. 9-2 Outline  Outer join problems  Type I nested queries  Type II nested queries and difference problems  Nested.
Chapter 9 Advanced Query Formulation with SQL. McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Outline Outer join problems.
9 Advanced Query Formulation with SQL (Chapter 9).
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.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
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.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Query Formulation with SQL.
SQL – Simple Queries and JOIN MGMT 360 Database Management.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored.
1 Welcome to the World of Databases! Database technology: crucial to the operation and management of modern organisations Major transformation in computing.
McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Chapter 10 Application Development with Views.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 3 Query Formulation with SQL. Outline Background Getting started Joining tables Summarizing tables Reasoning tools Advanced problems Data manipulation.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 2 The Relational Data Model. Outline Relational model basics Integrity rules Rules about referenced rows Relational Algebra.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Application Development with Views
Relational Database Design
The Relational Data Model
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Database Management
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
SQL 101.
STRUCTURED QUERY LANGUAGE
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 7 Introduction to Structured Query Language (SQL)
A Guide to SQL, Eighth Edition
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
IST 318 Database Administration
Presentation transcript:

Query Formulation with SQL Welcome to Chapter 3 on query formulation with SQL Query formulation is an important skill in application development Everyone involved in the application dev. must be competent in query formulation Most students will be involved (at least initially) in application development rather than in a role as a database specialist. Database specialists must also understand query formulation and SQL. Objectives: - Query formulation: problem statement into a database representation - SELECT statement: syntax and patterns for subset operations, joins, summarization, traditional set operators, and data manipulation operations - Write English descriptions to document SQL statements - Need lots of practice with query formulation and SQL Asst.Prof.Intiraporn Mulasastra

Outline Background Getting started Joining tables Summarizing tables Reasoning tools Advanced problems Data manipulation statements Background: - SQL history - SQL usage contexts Getting started: - SQL syntax - Single table problems - Grouping problems Join styles: - Cross product style - Join operator style - Joins and grouping Reasoning tools: - Conceptual process - Critical questions Traditional set operators: - Union, intersection, difference - Union compatibility: requirement for using the operators - Data manipulation statements: INSERT, UPDATE, DELETE Include slides on join and traditional set operators: use these if covering relational algebra as part of SQL Asst.Prof.Intiraporn Mulasastra

What is SQL? Structured Query Language Language for database definition, manipulation, and control International standard Standalone and embedded usage Intergalactic database speak Pronunciation: sequel due to its original name Comprehensive database language - Database definition: CREATE TABLE - Manipulation: retrieval and modification of rows - Control: integrity and security constraints Standards: - American National Standards Institute (ANSI), International Standards Organization (ISO) - SQL-86: 1986 (1989 revision to SQL-89) - SQL-92: 1992 - SQL:1999 current standard Usage contexts: - Write and execute statements using a specialized editor (standalone) - Embed statements into a procedural language (embedded) Michael Stonebraker called SQL “intergalactic database speak” Asst.Prof.Intiraporn Mulasastra

SQL Statements Definition: CREATE TABLE, ALTER TABLE, CREATE VIEW, CREATE SCHEMA Manipulation: SELECT, INSERT, UPDATE, DELETE, COMMIT, ROLLBACK Control: GRANT, REVOKE, CREATE ASSERTION Other statements: SET , CREATE TRIGGER, CREATE DOMAIN Asst.Prof.Intiraporn Mulasastra

SELECT Statement Overview SELECT <list of column expressions> FROM <list of tables and join operations> WHERE <list of logical expressions for rows> GROUP BY <list of grouping columns> HAVING <list of logical expressions for groups> ORDER BY <list of sorting specifications> Expression: combination of columns, constants, operators, and functions Conventions: - Upper case: keywords - Angle brackets: supply data Expression examples: - StdFirstName: student first name - FacSalary * 1.1 : inflate salary by 10% Logical expression: - T/F value - AND, OR, NOT - Logical expressions can be rather complex (nested queries); will not discuss complexities until Unit 4 Rows vs. Groups: distinction will be made clear as lecture proceeds Show examples in Access SQL and Oracle SQL - Some important differences - Most vendors implement a super/subset of SQL2 - Vendors now support some parts of SQL3: Chapter 16 Asst.Prof.Intiraporn Mulasastra

นิสิต อาจารย์ อาจารย์1 วิชา UNIVERSITY DATABASE ลงทะเบียน การเปิดสอน Visual representation is easier to comprehend than CREATE TABLE statements 1 and  symbols: - 1-M relationships - Student can have many enrollments - Student is the parent (1) table - Enrollment is the child (M) table - Foreign key is shown near the  symbol Meaning of the Faculty_1 table - Access representation for a self referencing relationship - Faculty_1 is not a real table (placeholder for self referencing relationship) วิชา Asst.Prof.Intiraporn Mulasastra

First SELECT Examples Example 1 เลือกทุกแถว ทุกคอลัมน์(*) SELECT * FROM Faculty รหัส ชื่อ นามสกุล ตำแหน่ง เงินเดือน หัวหน้า Example 1: - Retrieves all rows and columns - * in the SELECT clause evaluates to all columns of the FROM tables Asst.Prof.Intiraporn Mulasastra

First SELECT Examples Example 2 (Access) เลือกเฉพาะแถว ทุกคอลัมน์(*) FROM Faculty WHERE FacSSN = '543210987’ รหัส ชื่อ นามสกุล ตำแหน่ง เงินเดือน หัวหน้า Example 1: - Retrieves all rows and columns - * in the SELECT clause evaluates to all columns of the FROM tables Example 2: - Retrieves a single faculty row (subset of rows) - Relational algebra: restrict operation (row subset) - Oracle: use hyphens in FacSSN constant (543-21-0987) Asst.Prof.Intiraporn Mulasastra

First SELECT Examples Example 3 เลือกทุกแถว เฉพาะคอลัมน์ SELECT FacFirstName, FacLastName, FacSalary FROM Faculty รหัส ชื่อ นามสกุล ตำแหน่ง เงินเดือน หัวหน้า Example 3: - Retrieves a subset of columns - Relational algebra: subset of columns Example 4: - Retrieves a subset of rows and columns - Sequence of restrict and project operations Asst.Prof.Intiraporn Mulasastra

First SELECT Examples Example 4 เลือกบางแถว บางคอลัมน์ SELECT FacFirstName, FacLastName, FacSalary FROM Faculty WHERE FacSalary > 65000 AND FacRank = 'PROF' รหัส ชื่อ นามสกุล ตำแหน่ง เงินเดือน หัวหน้า Example 3: - Retrieves a subset of columns - Relational algebra: subset of columns Example 4: - Retrieves a subset of rows and columns - Sequence of restrict and project operations Asst.Prof.Intiraporn Mulasastra

Using Expressions ค้นหาอาจารย์ที่เข้าทำงานหลังปี 1996 คำนวณเงินเดือนเพิ่ม 10% Example 5 (Access) SELECT FacFirstName, FacLastName, FacCity, FacSalary*1.1 AS IncreasedSalary, FacHireDate FROM Faculty WHERE year(FacHireDate) > 1996 เงินเดือนคูณ 1.1 Example 5: - Retrieves faculty hired after 1991 - Inflates salary by 10% Different functions by DBMS: need to carefully study documentation Asst.Prof.Intiraporn Mulasastra

Inexact Matching Match against a pattern: LIKE operator Use meta characters to specify patterns Wildcard (* or %) Any single character (? or _) Example 6 (Access) SELECT * FROM Offering WHERE CourseNo LIKE ‘IS*' Common patterns: - Strings with specified endings - Strings with specified beginnings - Strings containing a substring Meta characters: - Special meaning when using the LIKE operator - Many others available: study DBMS documentation Example 6: - Retrieves offerings of IS course numbers Asst.Prof.Intiraporn Mulasastra

Join Operator Most databases have many tables Combine tables using the join operator Specify matching condition Can be any comparison but usually = PK = FK most common join condition Relationship diagram useful when combining tables Chapter 2 material: can present again for review Can instead present material in Chapter 3 and skip when initially covering chapter 2 Most joins follow relationship diagram - PK-FK comparisons - What tables can be combined directly versus indirectly Asst.Prof.Intiraporn Mulasastra

ไม่มีเงื่อนไขในการเชื่อมโยง Cartesian Product A B C D E ตาราง r, s: a 1 c 1 a 2 c 2 ไม่มีเงื่อนไขในการเชื่อมโยง r s A B C D E r x s a 1 c 1 a 1 c 2 a 2 c 1 a 2 c 2 Asst.Prof.Intiraporn Mulasastra

Asst.Prof.Intiraporn Mulasastra

Cross Product Style List tables in the FROM clause List join conditions in the WHERE clause Example 10.1 (Access) SELECT OffYear,OffTerm,CourseNo, FacFirstName,FacLastName FROM Offering, Faculty WHERE Faculty.FacSSN = Offering.FacSSN Meaning: details of offerings and assigned faculty for fall 2002 IS courses taught by assistant professors Cross Product Style: - Name comes from derivation of the join operator - Join is equivalent of a cross product, selection (retain just matching rows) Extension for multiple tables: - Add tables to the FROM clause - Add join conditions to the WHERE clause Order of tables and join conditions is NOT order dependent Oracle version: use % instead of * details of offerings and assigned faculty for all course offerings Asst.Prof.Intiraporn Mulasastra

Cross Product Style Example 10.2 (Access) SELECT OfferNo, CourseNo, FacFirstName, FacLastName FROM Offering, Faculty WHERE OffTerm = 'FALL' AND OffYear = 2007 AND FacRank = 'ASST' AND CourseNo LIKE‘IS*' AND Faculty.FacSSN = Offering.FacSSN Meaning: details of offerings and assigned faculty for fall 2002 IS courses taught by assistant professors Cross Product Style: - Name comes from derivation of the join operator - Join is equivalent of a cross product, selection (retain just matching rows) Extension for multiple tables: - Add tables to the FROM clause - Add join conditions to the WHERE clause Order of tables and join conditions is NOT order dependent Oracle version: use % instead of * details of offerings and assigned faculty for fall 2007 IS courses taught by assistant professors Asst.Prof.Intiraporn Mulasastra

GROUP BY Examples Example 12: Grouping on a single column SELECT FacRank, AVG(FacSalary) AS AvgSalary FROM Faculty GROUP BY FacRank รหัส ชื่อ นามสกุล ตำแหน่ง เงินเดือน หัวหน้า Example 12: - Row summary because output uses AVG function - Rename output column when using aggregate expressions - Retrieves a one row per faculty rank (ASST, ASSC, PROF) Example 13: - Summarize majors of upperclass students by average gpa; only include majors with avggpa > 3.1 - Row condition: cannot use aggregate function - Group condition: uses aggregate functions - Do not use a condition in HAVING unless the condition involves an aggregate function Asst.Prof.Intiraporn Mulasastra

GROUP BY Examples Example 12: Grouping on a single column SELECT FacRank, AVG(FacSalary) AS AvgSalary FROM Faculty GROUP BY FacRank FacRank AvgSalary ASST 37500 PROF 92500 ASSC 72500 Example 12: - Row summary because output uses AVG function - Rename output column when using aggregate expressions - Retrieves a one row per faculty rank (ASST, ASSC, PROF) Example 13: - Summarize majors of upperclass students by average gpa; only include majors with avggpa > 3.1 - Row condition: cannot use aggregate function - Group condition: uses aggregate functions - Do not use a condition in HAVING unless the condition involves an aggregate function Asst.Prof.Intiraporn Mulasastra 19

GROUP BY Examples Example 13: Row and group conditions SELECT StdMajor, AVG(StdGPA) AS AvgGPA FROM Student WHERE StdClass IN ('JR', 'SR') GROUP BY StdMajor HAVING AVG(StdGPA) > 3.1 Example 12: - Row summary because output uses AVG function - Rename output column when using aggregate expressions - Retrieves a one row per faculty rank (ASST, ASSC, PROF) Example 13: - Summarize majors of upperclass students by average gpa; only include majors with avggpa > 3.1 - Row condition: cannot use aggregate function - Group condition: uses aggregate functions - Do not use a condition in HAVING unless the condition involves an aggregate function  Asst.Prof.Intiraporn Mulasastra 20

SELECT StdMajor, AVG(StdGPA) AS AvgGPA. FROM Student SELECT StdMajor, AVG(StdGPA) AS AvgGPA FROM Student WHERE StdClass IN ('JR', 'SR') GROUP BY StdMajor StdMajor AvgGPA ACCT 3.5 FIN 2.5 3.2 2.7 IS 2.2 4 3.6 2.8 StdMajor AvgGPA ACCT 3.5 FIN 2.8 IS 3.15 Asst.Prof.Intiraporn Mulasastra

GROUP BY Examples StdMajor AvgGPA ACCT 3.5 IS 3.15 Example 13: Row and group conditions SELECT StdMajor, AVG(StdGPA) AS AvgGPA FROM Student WHERE StdClass IN ('JR', 'SR') GROUP BY StdMajor HAVING AVG(StdGPA) > 3.1 Example 12: - Row summary because output uses AVG function - Rename output column when using aggregate expressions - Retrieves a one row per faculty rank (ASST, ASSC, PROF) Example 13: - Summarize majors of upperclass students by average gpa; only include majors with avggpa > 3.1 - Row condition: cannot use aggregate function - Group condition: uses aggregate functions - Do not use a condition in HAVING unless the condition involves an aggregate function StdMajor AvgGPA ACCT 3.5 IS 3.15 Asst.Prof.Intiraporn Mulasastra 22

Summarization and Joins Powerful combination List join conditions in the WHERE clause Example 14: List the number of students enrolled in each fall 2003 offering. SELECT Offering.OfferNo, COUNT(*) AS NumStudents FROM Enrollment, Offering WHERE Offering.OfferNo = Enrollment.OfferNo AND OffYear = 2005 GROUP BY Offering.OfferNo Why combine: - Relational databases have many tables - Data for decision making is often spread across many tables OfferNo NumStudents 1234 6 4321 Asst.Prof.Intiraporn Mulasastra

Joining Three Tables มี Key เชื่อมโยง SELECT * FROM Faculty, Offering, Course WHERE Faculty.FacSSN=Offering.FacSSN AND Offering.CourseNo=Course.CourseNo Results of 2 tables joined 1 List Leonard Vince’s teaching schedule in fall 1999. For each course, list the offering number, course number, number of units, days, location, and time. 2 tables 2 3 Asst.Prof.Intiraporn Mulasastra Table 3 3 tables joined

Joining Three Tables Example 16: List Leonard Vince’s teaching schedule in fall 2005. For each course, list the offering number, course number, number of units, days, location, and time. SELECT OfferNo, Offering.CourseNo, OffDays, CrsUnits, OffLocation, OffTime FROM Faculty, Offering, Course WHERE Faculty.FacSSN = Offering.FacSSN AND Offering.CourseNo = Course.CourseNo AND OffYear = 2007 AND OffTerm = 'FALL' AND FacFirstName = 'Leonard' AND FacLastName = 'Vince' List Leonard Vince’s teaching schedule in fall 1999. For each course, list the offering number, course number, number of units, days, location, and time. Asst.Prof.Intiraporn Mulasastra

Multiple Column Grouping Example 20: List the course number, the offering number, and the number of students enrolled. Only include courses offered in spring 2006. SELECT CourseNo, Enrollment.OfferNo, Count(*) AS NumStudents FROM Offering, Enrollment WHERE Offering.OfferNo = Enrollment.OfferNo AND OffYear = 2006 AND OffTerm = 'SPRING' GROUP BY Enrollment.OfferNo, CourseNo After studying Example 3.37, you might be confused about the necessity to group on both OfferNo and CourseNo. One simple explanation is that any columns appearing in SELECT must be either a grouping column or an aggregrate expression. However, this explanation does not quite tell the entire story. Grouping on OfferNo alone produces the same values for the computed column (NumStudents) because OfferNo is the primary key. Including non-unique columns such as CourseNo adds information to each result row but does not change the aggregate calculations. If you do not understand this point, use sample tables to demonstrate it. When evaluating your sample tables, remember that joins occur before grouping. สมาชิกของกลุ่มต้องมีค่า 2 ค่านี้เหมือนกัน Asst.Prof.Intiraporn Mulasastra

คำสั่งอื่นๆ ตรวจสอบการคงอยู่ของข้อมูล การเป็นสมาชิก EXIST, NOT EXIST เช่น รายชื่อนักเรียนที่ไม่เคยลงเรียนวิชา ภาษาอังกฤษ เลย การเป็นสมาชิก IN, NOT IN นักเรียนที่เป็นสมาชิกทุกชมรม Asst.Prof.Intiraporn Mulasastra

INSERT Example Example 24: Insert a row into the Student table supplying values for all columns. INSERT INTO Student (StdSSN, StdFirstName, StdLastName, StdCity, StdState, StdZip, StdClass, StdMajor, StdGPA) VALUES ('999999999','JOE','STUDENT','SEATAC', 'WA','98042-1121','FR','IS', 0.0) In the first format, one row at a time can be added. You specify values for each column with the VALUES clause. You must format the constant values appropriate for each column. Refer to the documentation of your DBMS for details about specifying constants especially string and date constants. Specifying a null value for a column is also not standard across DBMSs. In some systems, you simply omit the column name and the value. In other systems, you specify a particular symbol for a null value. Of course, you must be careful that the table definition permits null values for the column of interest. Otherwise, the INSERT statement will be rejected. Asst.Prof.Intiraporn Mulasastra

UPDATE Example Example 25: Change the major and class of Homer Wells. UPDATE Student SET StdMajor = 'ACCT', StdClass = 'SO' WHERE StdFirstName = 'HOMER' AND StdLastName = 'WELLS' The UPDATE statement allows one or more rows to be changed. Any number of columns can be changed, although typically only one column at a time is changed. When changing the primary key, update rules on referenced rows may not allow the operation. Asst.Prof.Intiraporn Mulasastra

DELETE Example Example 26: Delete all IS majors who are seniors. DELETE FROM Student WHERE StdMajor = 'IS' AND StdClass = 'SR' The DELETE statement allows one or more rows to be removed. DELETE is subject to the rules on referenced rows. For example, a Student row cannot be deleted if related Enrollment rows exist and the deletion action is restrict. Asst.Prof.Intiraporn Mulasastra

DELETE Example Use Type I nested queries to test conditions on other tables Use for UPDATE statements also Example 8: Delete offerings taught by Leonard Vince. DELETE FROM Offering WHERE Offering.FacSSN IN ( SELECT FacSSN FROM Faculty WHERE FacFirstName = 'Leonard' AND FacLastName = 'Vince' ) Type I nested query good for complex deletions: - Test conditions on other tables - Portable across most DBMSs Asst.Prof.Intiraporn Mulasastra

SQL Asst.Prof.Intiraporn Mulasastra