180 likes | 298 Views
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:
E N D
CpSc 3220The 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’