Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.

Similar presentations


Presentation on theme: "Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files."— Presentation transcript:

1 Lecture 2 Joins and sub-queries

2 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files

3 3 Joins zJoins are how to connect together the tables in a relational database zTo join tables we must have a column in each table that contains the same information zTwo main types: simple, outer.

4 4 Joins zIn our example tables (emp and dept), each contains a column: deptno (the name does not have to be the same). zThis column contains the dept number for the employee in the emp table and the dept table has departmental information

5 5

6 6

7 7 Joins zTo join the two tables to select, say, the employee name and the name of their department we use a join-condition in the WHERE clause: SELECTename, dname FROMemp, dept WHEREemp.deptno = dept.deptno;

8 8

9 9 Joins (example2) To find ALLEN’s location enter: SELECTename, loc FROMemp, dept WHEREename = ‘ALLEN’ ANDemp.deptno = dept.deptno;

10 10

11 11 Joins zIn general: SELECT columns FROM table1, table2, … WHERE join-condition; zThe join-condition must join all the tables: for two tables we need a single condition, for three we require two conditions etc

12 12 Multiple Table Joins zSELECT columns FROM tab1, tab2, tab3 WHEREjoin-condition1 AND join-condition2;

13 13 Simple Joins zIf a column appearing in SELECT has the same name in both tables, we MUST specify which one we require SELECTename, dname, dept.deptno FROMemp, dept WHEREemp.deptno = dept.deptno;

14 14 Outer joins zReturn rows from table which have no match in other table SELECT columns FROM table1, table2 WHEREjoin-condition1 = join-condition2 (+);

15 15 Outer joins zSELECT dept.deptno, dname, sum(sal) FROMemp, dept WHEREemp.deptno (+) = dept.deptno GROUP BYdept.deptno, dname ORDER BYdept.deptno zAppend outer join symbol to table without matching rows

16 16

17 17 Subqueries zThis is when one of the parts of WHERE clause is a query itself. zConsider the following question: list the name and salary of all employees who earn greater than the average salary? zWe need to determine what the average salary is before we can ask who earns more than it.

18 18 Sub-queries zTo determine the average salary: SELECT AVG(sal) FROM emp; now who earns more than this SELECT ename, sal FROM emp WHERE sal > (SELECT AVG(sal) FROM emp);

19 19

20 20 Sub-queries zThe inner query is executed just once, i.e. the average salary is found and the outer query determines who earns more than the average salary. zQueries can be composed where the sub- query is executed once for each row in the outer query

21 21 Sub-queries zList employees, department number and salary for those employees who earn more than their departmental average salary. SELECT ename, deptno, sal FROM emp WHERE sal > (average salary of candidate employee’s department); zYou also need a subquery that calculates the average salary of each candidate employee’s department

22 22 SELECT AVG(SAL) FROMemp WHERE deptno = (candidate row’s value of DEPTNO) zAs the main query considers each candidate row, it must invoke the subquery and ‘tell’ it the employee’s dept number. zThe subquery must then compute the average salary for that employee’s dept. zThe main query must then compare the employee’s salary to the department’s average salary.

23 23 Subqueries zHow do we tie the department no in the inner query to the department no in the outer to get that individuals department’s average salary? zThe trick is to alias the name of the table emp in the outer query

24 24 Sub-queries zSELECT ename, deptno, sal FROM emp aliasemp WHERE sal > (SELECT AVG(sal) FROM emp WHERE aliasemp.deptno = deptno);

25 25

26 26 More aliasing zExample: for each manager list their staff SELECT manager.ename, worker.ename FROM emp manager, emp worker WHERE worker.mgr = manager.empno; zManager here does not mean job = ‘MANAGER’

27 27

28 28 IN Operator zMatches any one in list. zExample: list average salary of only CLERKs and ANALYSTs SELECT AVG(sal), job FROM emp WHERE job IN (‘CLERK’, ‘ANALYST’) GROUP BY job ORDER BY job

29 29

30 30 NULL values zCare must exercise when performing calculations that involve NULL (empty) values. A useful function is NVL(column, 0) which converts NULL values to 0 for the calculation.

31 31 NULL values zWe can use NULL in SQL commands: SELECT * FROM emp WHERE comm IS NOT NULL; INSERT INTO emp VALUES (7256, ‘GILES’, ‘CLERK’, 7788, ‘15-AUG-80’, 1100, NULL, 20);

32 32 SQLplus zCommand files Instead of typing commands directly in Oracle, they can be placed in a file (using Notepad).

33 33 SQLplus zThe sequence of commands in a file can be run with the command: START zThis means that complicated sequences of commands (like reports) can be written outside of SQL*Plus and then run

34 34 SQLplus zWe can save the current SQL command to a file with: SAVE zTo load a file but not run it: GET

35 35 Summary zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files


Download ppt "Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files."

Similar presentations


Ads by Google