Download presentation
Presentation is loading. Please wait.
Published byDwain Reed Modified over 9 years ago
1
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries
2
SQL advanced select using Oracle 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries
3
SQL advanced select using Oracle 3 Join Cartesian product –select * from tableA, tableB all rows in tableA combined with all rows from tableB huge result! seldom useful!! Different types of joins –equi-join –non-equi-join –outer join –self-join
4
SQL advanced select using Oracle 4 Equi-join Joining 2 tables using common attributes [usually primary and foreign keys] Structure select … from tableA, tableB where tableA.PK = tableB.FK Comparison using equality (=) is equi-join Examples select * from student, city where city.postcode = student.postcode select * from student, course, studentCourse where student.ID = studentCourse.studentID and studentCourse.courseID = course.ID
5
SQL advanced select using Oracle 5 Non-equi-joins Where clause using operations other than equality Example: SELECT e.Lname || ’,’ e.Firstname AS EMPLOYEE FROM employee e, emplevel l WHERE e.Salary BETWEEN l.LowSalary AND l.HighSalary
6
SQL advanced select using Oracle 6 Outer joins Ordinary join –if a value in one table has no matching value in the other table, the row is not included in the result. Outer join –values without matching values are included in the result. Oracle has 2 syntaxes –Special syntax Special (old) Oracle syntax –New syntax SQL99 standard syntax
7
SQL advanced select using Oracle 7 Outer join, old syntax Old / special Oracle syntax SELECT … FROM tableA, tableB WHERE tableA.col1 (+)= tableB.col2 rows from tableA with no matching attribute in tableB is included in the result Example: SELECT s.First || ‘ ‘ || s.Last AS STUDENT f.Name AS ADVISOR FROM faculty f, student s WHERE s.FacultyId (+)= f.facultyId
8
SQL advanced select using Oracle 8 Outer join, new syntax New / SQL 99 standard syntax SELECT d.department_id, e.last_name FROM departments d LEFT OUTER JOIN employees e ON d.department_id = e.department_id ORDER BY d.department_id Result –Department with and without employees are included in the result New in Oracle 9i (not present in Oracle 8i)
9
SQL advanced select using Oracle 9 Self-join Joining a table with itself. –ER recursive relationship –Rel. model foreign key to another attribute in the same relation. –same table used twice in the from clause we need to name the two instances of the table (table aliases) –select … from employee e, employee s where e.supervisorID = s.ID –Fig. 7-9 page 165
10
Self-join - Example SELECT e.Lname || ’,’ || e.Firstname AS Employee, s.Lname || ’,’ || s.Firstname AS Manager FROM employee e, employee s WHERE s.Supervisor = s.EmployeeId SQL advanced select using Oracle 10
11
SQL advanced select using Oracle 11 Set operators Tables may be used in set operations –uniontableA union tableB all rows in tableA and all rows in tableB –union alltableA union all tableB as union, but includes duplicates –intersecttableA intersect tableB all rows that appears in both tableA and tableB –minustableA minus tableB all rows in tableA, but not in tableB –Tables must be "union compatible" same number + types + order of attributes
12
SQL advanced select using Oracle 12 Sub-query The WHERE clause in a select statement may contain another select statement (sub-query)! Example SELECT Lname, Fname, Salary, DeptId FROM employee WHERE DeptId = (SELECT DeptId FROM dept WHERE UPPER (DeptName) = ‘FINANCE’); (UPPER converts each letter to uppercase)
13
Sub-query (cont’d) Two types of sub-queries single row –returning a single row of data, and most often a single data item multiple row –returning more than on row of data SQL advanced select using Oracle 13
14
SQL advanced select using Oracle 14 Single row query Inner query is executed first, returning a value that is used in the outer query. select … from tableA where attributA operator (sub-query) –Operator may be: =, >, <, etc.
15
SQL advanced select using Oracle 15 Sub-queries in create, insert, update and delete create table tableName as select query –pure redundancy! insert into tableName select query –pure redundancy! update tableName set (columnNames) = (query) where condition delete from tableName where columnName = (query)
16
SQL advanced select using Oracle 16 Multiple-row sub-queries Sub-query returning more than one row of data. select … from … where attribute operator (sub-query) –Operator may be in all any, =any
17
Multiple-row sub-queries (cont’d) Example SELECT StudentId, Last, First, FacultyId FROM student WHERE FacultyId IN (SELECT FacultyId FROM faculty WHERE DeptId = 1); SQL advanced select using Oracle 17
18
SQL advanced select using Oracle 18 Final remarks SQL (specially select) is like programming. –There's only one place to learn it - at the keyboard! Make advanced select statements incrementally –Starting from the innermost sub-query. –Monitor the results Take advantage of advanced select statements –Don't try to program the same features in your application.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.