CpSc 3220 The Language of SQL The Language of SQL Chapters 13-14.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

© Abdou Illia MIS Spring 2014
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.
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)
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
View Sen Zhang. Views are very common in business systems users view of data is simplified a form of security - user sees only the data he/she needs to.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Introduction to SQL Structured Query Language Martin Egerhill.
Introduction to SQL J.-S. Chou Assistant Professor.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Introduction to Databases Chapter 7: Data Access and Manipulation.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
ITBIS373 Database Development
Oracle Database Administration Lecture 2 SQL language.
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.
Using Special Operators (LIKE and IN)
CS 1308 Computer Literacy and the Internet
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
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.
Indexes and Views Unit 7.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
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.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
(SQL - Structured Query Language)
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Instructor: Craig Duckett Lecture 11: Thursday, November 5 th, 2015 Assignment 2 DUE, Set Operations, Subqueries, Views 1 BIT275: Database Design (Fall.
Query Processing – Implementing Set Operations and Joins Chap. 19.
CpSc 3220 The Language of SQL Chapter 17 Modifying Data.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CpSc 3220 The Language of SQL
MySQL Subquery Source: Dev.MySql.com
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
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Basic Nested Queries and Views
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Databases.
Structured Query Language
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
Database Systems: Design, Implementation, and Management Tenth Edition
CSC 453 Database Systems Lecture
Presentation transcript:

CpSc 3220 The Language of SQL The Language of SQL Chapters 13-14

Topics Self Joins and Views Subqueries Set Logic

Self Joins A self join is the join of a table to itself Many tables are self-referencing Examples: A Personnel table contains a column that gives the ID of an employee’s manager who is also a employee in the Personnel table A Course table might contain a column that gives its prerequisite which is also an entry in the Course table

Implementing Self Joins Use the INNER JOIN to join a table to itself The AS word must be used to give each table a distinct name so that references to columns can be made unique Example: SELECT E.EmployeeName,M.EmployeeName FROM Personnel AS E INNER JOIN Personnel as M ON E.ManagerID = M.EmployeeID

Creating Views A View is a virtual table that can be saved and used in queries like a normal table This can be used to simplify other queries For example, if we have several queries to make on joined tables we can create a View of the joined tables and then use that virtual table directly for all the queries

Example of View Creation From the University Database we can create a View called InstructorData that contains information about an Instructor and the Department the instructor is assigned to CREATE VIEW InstructorData AS SELECT ID,name,Instructor.dept_name,salary,building,budget FROM Instructor INNER JOIN Department ON Instructor.dept_name = Department.dept_name Views can be used in queries as can Tables

Query from A View Views can be used in queries just as can Tables SELECT ID,name,Instructor.dept_name,building FROM Instructor INNER JOIN Department ON Instructor.dept_name = Department.dept_name SELECT ID,name,Instructor.dept_name,building,budget FROM InstructorData

Views Can Limit Access to Data From the University Database we can create a View called PublicInstructorData that contains non-sensitive information about an Instructor CREATE VIEW PublicInstructorData AS SELECT ID,name,dept_name FROM Instructor ;

Benefits of Views Can reduce complexity Can increase reusability Can format data Can create calculated columns Can rename columns Can create subsets of data Can enforce security restrictions

Modifying and Deleting Views Views can be changed with the ALTER VIEW and DROP VIEW statements

Subqueries It is possible for queries to contain other queries Using nested queries can be complex Can be used in SELECT, INSERT, UPDATE, and DELETE statements

Types of Subqueries General form of SELECT statement SELECT colList FROM tableList WHERE condition GROUP BY colList HAVING condition ORDER BY colList Subqueries can be used in colList, tableList, or condition sections

Subqueries as Data Sources An example from University Database We want a list of students and the courses they took in 2012 SELECT name,course_id FROM Student WHERE Student.ID IN (SELECT Takes.ID FROM Takes WHERE Takes.ID = Student.ID)

Subqueries in Selection Criteria SELECT CustomerName FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE OrderType = ‘Cash’);

Correlated Subqueries Related to the outer query Must be evaluated for each returned row Example: SELECT CustomerName FROM Customers AS C WHERE (SELECT SUM(OrderAmount) FROM Orders AS O WHERE C.CustomerID=O.CustomerID) > 20;

The EXISTS Operator Allows you to determine if data in a correlated subquery exists SELECT CustomerName FROM Customers AS C WHERE EXISTS (SELECT * FROM ORDERS WHERE C.CustomerID=O.CustomerID);

Subqueries as Calculated Columns SELECT CustomerName, (SELECT COUNT(OrderID) FROM Orders WHERE Customers.CustomerID=Orders.CustomerID)AS ‘No.of Orders) FROM Customers ORDER BY Customers.CustomerID

Set Logic Remember, Relational Databases are based on the assumption that the Tables are really mathematical relations (or sets) We can combine queries using Set Logic (UNION, INTERSECT, DIFFERENCE) select-statement1 UNION select-statement2 Combined relations must be of the same ‘type’