1 / 18

CpSc 3220 The Language of SQL

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:

ledell
Download Presentation

CpSc 3220 The Language of SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Topics • Self Joins and Views • Subqueries • Set Logic

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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 ;

  9. 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

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

  11. 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

  12. 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

  13. 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)

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

  15. 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;

  16. 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);

  17. 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

  18. 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’

More Related