lecture 2

35
Lecture 2 Joins and sub-queries

Upload: peggy

Post on 05-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Lecture 2. Joins and sub-queries. Topics. Joins Simple; Outer Sub-queries aliases IN Operator NULL values Saving and running files. Joins. Joins are how to connect together the tables in a relational database - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 2

Lecture 2

Joins and sub-queries

Page 2: Lecture 2

2

Topics

Joins Simple; Outer

Sub-queries aliases

IN OperatorNULL valuesSaving and running files

Page 3: Lecture 2

3

Joins

Joins are how to connect together the tables in a relational database

To join tables we must have a column in each table that contains the same information

Two main types: simple, outer.

Page 4: Lecture 2

4

Joins

In our example tables (emp and dept), each contains a column: deptno (the name does not have to be the same).

This column contains the dept number for the employee in the emp table and the dept table has departmental information

Page 5: Lecture 2

5

Page 6: Lecture 2

6

Page 7: Lecture 2

7

Joins

To 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:

SELECT ename, dname FROM emp, deptWHERE emp.deptno = dept.deptno;

Page 8: Lecture 2

8

Page 9: Lecture 2

9

Joins (example2)

To find ALLEN’s location enter:

SELECT ename, locFROM emp, deptWHERE ename = ‘ALLEN’ANDemp.deptno = dept.deptno;

Page 10: Lecture 2

10

Page 11: Lecture 2

11

Joins

In general:

SELECT columnsFROM table1, table2, …WHERE join-condition;

The join-condition must join all the tables: for two tables we need a single condition, for three we require two conditions etc

Page 12: Lecture 2

12

Multiple Table Joins

SELECT columnsFROM tab1, tab2, tab3WHERE join-condition1 AND join-condition2;

Page 13: Lecture 2

13

Simple Joins

If a column appearing in SELECT has the same name in both tables, we MUST specify which one we require

SELECT ename, dname, dept.deptno

FROM emp, deptWHERE emp.deptno = dept.deptno;

Page 14: Lecture 2

14

Outer joins

Return rows from table which have no match in other table

SELECT columnsFROM table1, table2WHERE join-condition1 = join-condition2 (+);

Page 15: Lecture 2

15

Outer joins

SELECT dept.deptno, dname, sum(sal)FROM emp, deptWHERE emp.deptno (+) = dept.deptnoGROUP BY dept.deptno, dnameORDER BY dept.deptno

Append outer join symbol to table without matching rows

Page 16: Lecture 2

16

Page 17: Lecture 2

17

Subqueries

This is when one of the parts of WHERE clause is a query itself.

Consider the following question: list the name and salary of all employees who earn greater than the average salary?

We need to determine what the average salary is before we can ask who earns more than it.

Page 18: Lecture 2

18

Sub-queries

To determine the average salary:

SELECT AVG(sal) FROM emp;

now who earns more than this

SELECT ename, sal FROM empWHERE sal > (SELECT AVG(sal) FROM emp);

Page 19: Lecture 2

19

Page 20: Lecture 2

20

Sub-queries

The 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.

Queries can be composed where the sub-query is executed once for each row in the outer query

Page 21: Lecture 2

21

Sub-queries

List employees, department number and salary for those employees who earn more than their departmental average salary.

SELECT ename, deptno, sal FROM empWHERE sal > (average salary of candidate employee’s department);

You also need a subquery that calculates the average salary of each candidate employee’s department

Page 22: Lecture 2

22

SELECT AVG(SAL)FROM empWHERE deptno = (candidate row’s value of

DEPTNO)

As the main query considers each candidate row, it must invoke the subquery and ‘tell’ it the employee’s dept number.

The subquery must then compute the average salary for that employee’s dept.

The main query must then compare the employee’s salary to the department’s average salary.

Page 23: Lecture 2

23

Subqueries

How 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?

The trick is to alias the name of the table emp in the outer query

Page 24: Lecture 2

24

Sub-queries

SELECT ename, deptno, salFROM emp aliasempWHERE sal >

(SELECT AVG(sal) FROM emp WHERE aliasemp.deptno =

deptno);

Page 25: Lecture 2

25

Page 26: Lecture 2

26

More aliasing

Example: for each manager list their staff

SELECT manager.ename, worker.enameFROM emp manager, emp workerWHERE worker.mgr = manager.empno;

Manager here does not mean job = ‘MANAGER’

Page 27: Lecture 2

27

Page 28: Lecture 2

28

IN Operator

Matches any one in list.Example: list average salary of only

CLERKs and ANALYSTs

SELECT AVG(sal), job FROM empWHERE job IN (‘CLERK’, ‘ANALYST’)GROUP BY jobORDER BY job

Page 29: Lecture 2

29

Page 30: Lecture 2

30

NULL values

Care 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.

Page 31: Lecture 2

31

NULL values

We can use NULL in SQL commands:

SELECT * FROM empWHERE comm IS NOT NULL;

INSERT INTO empVALUES (7256, ‘GILES’, ‘CLERK’, 7788, ‘15-AUG-80’, 1100, NULL, 20);

Page 32: Lecture 2

32

SQLplus

Command filesInstead of typing commands directly in Oracle, they can be placed in a file (using Notepad).

Page 33: Lecture 2

33

SQLplus

The sequence of commands in a file can be run with the command:

START <filename>This means that complicated

sequences of commands (like reports) can be written outside of SQL*Plus and then run

Page 34: Lecture 2

34

SQLplus

We can save the current SQL command to a file with:

SAVE <filename>To load a file but not run it:

GET <filename>

Page 35: Lecture 2

35

Summary

Joins Simple; Outer

Sub-queries aliases

IN OperatorNULL valuesSaving and running files