Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.

Similar presentations


Presentation on theme: "SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries."— Presentation transcript:

1 SQL advanced select using Oracle 1

2 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! Generally not 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 –, etc.

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.

7 SQL advanced select using Oracle 7 Outer join Syntaz 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

8 SQL advanced select using Oracle 8 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

9 SQL advanced select using Oracle 9 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

10 SQL advanced select using Oracle 10 Sub-query The where clause in a select statement may contain another select statement (sub-query)! –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

11 SQL advanced select using Oracle 11 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.

12 SQL advanced select using Oracle 12 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)

13 Sub-queries in select Select …. Sub from … sub where … sub Example: Subquery in select clause –select d.*, (select avg(salary) from teacher where teacher.department_id = d.id) from department d; SQL advanced select using Oracle 13

14 SQL advanced select using Oracle 14 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 –Example: Teacher(s) with highest salary select * from teacher where salary >=all ( select salary from teacher);

15 SQL advanced select using Oracle 15 Top-N analysis MS Access SQL and SQL Server has a special keyword top –select top 10 attribute … –Oracle has no such feature, but it can be simulated using the pseudo-column rownum. –sub-query in from clause inline view [more on views later on] –Example: Teachers with highest salaries select rownum, name, salary from (select * from teacher order by salary desc) where rownum <= 5;

16 SQL advanced select using Oracle 16 Correlated subquery Ordinary subquery –The inner query does not reference columns from the outer query –The inner query can be “calculated” once and for all. Correlated subquery –The inner query references columns from the outer query –Like programming 2 loops, one inside the other Example: Teachers with a salary higher than the average salary in their department select * from teacher t1 where salary > (select avg(salary) from teacher t2 where t1.department_id = t2.department_id);

17 SQL advanced select using Oracle 17 The EXISTS operator Operator used with correlated sub-queries Result –False If the current result of the sub-query is empty –True If the current result of the sub-query is not empty Examples –Good students (has at least one course) select * from student where exists (select * from student_course where studentid = student.id); –Lazy students (no courses) select * from student where not exists (select * from student_course where studentid = student.id);

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.


Download ppt "SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries."

Similar presentations


Ads by Google