110 likes | 126 Views
Learn about outer joins in database queries, including LEFT JOIN, RIGHT JOIN, FULL JOIN, and their implications on result sets. Understand the order of join, potential pitfalls, and union operations. Dive into reflexive joins and natural joins.
E N D
Advance Queries (cont.) INFSY 445 Fall 2005
Outer Joins • Describes what happens when values in one table do not exist in the second table • LEFT JOIN • RIGHT JOIN • FULL JOIN
Outer Joins • LEFT JOIN • All rows in left table displayed • When no matching value in table on right, NULL values inserted into output • RIGHT JOIN works same except right table takes priority
Outer Joins • Caution – joining 2 large tables could produce meaningless results • Order of join causes different results • *= is symbol for OUTER JOIN in databases not SQL 92 compliant
Union • Used to join rows of data from similar tables • The data from both table must match! SELECT employeeID, name, salary, ‘East’ AS Office FROM EmployeeEast UNION SELECT employeeID, name, salary, ‘West’ AS Office FROM EmployeeWest;
Union • Basic command eliminates duplicate rows • To keep duplicates, use UNION ALL
Reflexive Join • Join a table to itself • An employee has one boss; a boss can have many employees.
Reflexive Join • What would the tables look like?
Reflexive Join • SELECT Employee.number, Employee.Fname, Employee.manager, E2.Fname • FROM Employee INNER JOIN Employee AS E2 • ON Employee.manager = E2.number;
Joining Tables • NATURAL JOIN • Automatically join 2 tables with a commonly named and defined column • SELECT title, pubid, name • FROM publisher NATURAL JOIN books;