Oracle PL/SQL/Table Joins/Left Outer Join

Материал из SQL эксперт
Перейти к: навигация, поиск

A left outer join:the outer join operator appears on the right of the equality operator

 

SQL> -- create demo table
SQL> create table Employee(
  2    empno              Number(3)  NOT NULL, -- Employee ID
  3    ename              VARCHAR2(10 BYTE),   -- Employee Name
  4    hireDate          DATE,                -- Date Employee Hired
  5    orig_salary        Number(8,2),         -- Orignal Salary
  6    curr_salary        Number(8,2),         -- Current Salary
  7    region             VARCHAR2(1 BYTE)     -- Region where employeed
  8  )
  9  /
Table created.
SQL>
SQL> create table job(
  2    empno              Number(3)  NOT NULL, -- Employee ID
  3    jobtitle           VARCHAR2(10 BYTE)    -- Employee job title
  4  )
  5  /
Table created.
SQL> -- prepare data for employee table
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(122,"Alison",to_date("19960321","YYYYMMDD"), 45000,       48000,       "E")
  3  /
1 row created.
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(123, "James",to_date("19781212","YYYYMMDD"), 23000,       32000,       "W")
  3  /
1 row created.
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(104,"Celia",to_date("19821024","YYYYMMDD"), 53000,       58000,        "E")
  3  /
1 row created.
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(105,"Robert",to_date("19840115","YYYYMMDD"), 31000,      36000,        "W")
  3  /
1 row created.
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(116,"Linda", to_date("19870730","YYYYMMDD"), 43000,       53000,       "E")
  3  /
1 row created.
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(117,"David", to_date("19901231","YYYYMMDD"), 78000,       85000,       "W")
  3  /
1 row created.
SQL> insert into Employee(empno,  ename,  hireDate,                       orig_salary, curr_salary, region)
  2                values(108,"Jode",  to_date("19960917","YYYYMMDD"), 21000,       29000,       "E")
  3  /
1 row created.
SQL>
SQL> -- prepare data for job table
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(101,   "Painter");
1 row created.
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(122,   "Tester");
1 row created.
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(123,   "Dediator");
1 row created.
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(104,   "Chemist");
1 row created.
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(105,   "Accountant");
1 row created.
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(116,   "Manager");
1 row created.
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(117,   "Programmer");
1 row created.
SQL>
SQL> insert into job(empno, jobtitle)
  2           values(108,   "Developer");
1 row created.
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /
     EMPNO ENAME      HIREDATE  ORIG_SALARY CURR_SALARY R
---------- ---------- --------- ----------- ----------- -
       122 Alison     21-MAR-96       45000       48000 E
       123 James      12-DEC-78       23000       32000 W
       104 Celia      24-OCT-82       53000       58000 E
       105 Robert     15-JAN-84       31000       36000 W
       116 Linda      30-JUL-87       43000       53000 E
       117 David      31-DEC-90       78000       85000 W
       108 Jode       17-SEP-96       21000       29000 E
7 rows selected.
SQL>
SQL> select * from job;
     EMPNO JOBTITLE
---------- ----------
       101 Painter
       122 Tester
       123 Dediator
       104 Chemist
       105 Accountant
       116 Manager
       117 Programmer
       108 Developer
8 rows selected.
SQL>
SQL>
SQL>
SQL>
SQL> -- A left outer join:the outer join operator appears on the right of the equality operator:
SQL>
SQL> SELECT e.ename, j.jobtitle
  2  FROM employee e, job j
  3  WHERE e.empno  = j.empno(+) ;
ENAME      JOBTITLE
---------- ----------
Alison     Tester
James      Dediator
Celia      Chemist
Robert     Accountant
Linda      Manager
David      Programmer
Jode       Developer
7 rows selected.
SQL>
SQL>
SQL>
SQL> drop table job;
Table dropped.
SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /
Table dropped.
SQL>
SQL>



Left join with plus sign

 
SQL>
SQL> create table emp
  2  ( empno      NUMBER(4)    constraint E_PK primary key
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , sal        NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2)    default 10
 11  ) ;
Table created.
SQL> insert into emp values(1,"Tom","N",   "Coder", 13,date "1965-12-17",  800 , NULL,  20);
1 row created.
SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20",  1600, 300,   30);
1 row created.
SQL> insert into emp values(3,"Wil","TF" ,  "Tester",6,date "1962-02-22",  1250, 500,   30);
1 row created.
SQL> insert into emp values(4,"Jane","JM",  "Designer", 9,date "1967-04-02",  2975, NULL,  20);
1 row created.
SQL> insert into emp values(5,"Mary","P",  "Tester",6,date "1956-09-28",  1250, 1400,  30);
1 row created.
SQL> insert into emp values(6,"Black","R",   "Designer", 9,date "1963-11-01",  2850, NULL,  30);
1 row created.
SQL> insert into emp values(7,"Chris","AB",  "Designer", 9,date "1965-06-09",  2450, NULL,  10);
1 row created.
SQL> insert into emp values(8,"Smart","SCJ", "Coder", 4,date "1959-11-26",  3000, NULL,  20);
1 row created.
SQL> insert into emp values(9,"Peter","CC",   "Designer",NULL,date "1952-11-17",  5000, NULL,  10);
1 row created.
SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28",  1500, 0,     30);
1 row created.
SQL> insert into emp values(11,"Ana","AA",  "Coder", 8,date "1966-12-30",  1100, NULL,  20);
1 row created.
SQL> insert into emp values(12,"Jane","R",   "Manager",   6,date "1969-12-03",  800 , NULL,  30);
1 row created.
SQL> insert into emp values(13,"Fake","MG",   "Coder", 4,date "1959-02-13",  3000, NULL,  20);
1 row created.
SQL> insert into emp values(14,"Mike","TJA","Manager",   7,date "1962-01-23",  1300, NULL,  10);
1 row created.
SQL>
SQL> create table departments
  2  ( deptno NUMBER(2)     constraint D_PK primary key
  3  , dname  VARCHAR2(10)
  4  , location VARCHAR2(8)
  5  , mgr    NUMBER(4)
  6  ) ;
Table created.
SQL>
SQL> insert into departments values (10,"ACCOUNTING","NEW YORK",7);
1 row created.
SQL> insert into departments values (20,"TRAINING",  "DALLAS",  4);
1 row created.
SQL> insert into departments values (30,"SALES",     "CHICAGO", 6);
1 row created.
SQL> insert into departments values (40,"HR",        "BOSTON",  9);
1 row created.
SQL>
SQL>
SQL> select d.deptno, d.location
  2  ,      e.ename, e.init
  3  from   emp e, departments d
  4  where  e.deptno(+) = d.deptno
  5  order  by d.deptno, e.ename;
    DEPTNO LOCATION ENAME    INIT
---------- -------- -------- -----
        10 NEW YORK Chris    AB
        10 NEW YORK Mike     TJA
        10 NEW YORK Peter    CC
        20 DALLAS   Ana      AA
        20 DALLAS   Fake     MG
        20 DALLAS   Jane     JM
        20 DALLAS   Smart    SCJ
        20 DALLAS   Tom      N
        30 CHICAGO  Black    R
        30 CHICAGO  Jack     JAM
        30 CHICAGO  Jane     R
        30 CHICAGO  Mary     P
        30 CHICAGO  Take     JJ
        30 CHICAGO  Wil      TF
        40 BOSTON
15 rows selected.
SQL>
SQL> drop table emp;
Table dropped.
SQL> drop table departments;
Table dropped.



left outer join between employee table and department table

  
SQL>
SQL> CREATE TABLE departments
  2  (department_id           number(10)            not null,
  3   department_name      varchar2(50)      not null,
  4   CONSTRAINT departments_pk PRIMARY KEY (department_id)
  5  );
Table created.
SQL>
SQL>
SQL>
SQL> insert into departments ( department_id, department_name )
  2                    values( 1,             "Data Group" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
  2                    values( 2,             "Purchasing" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
  2                    values( 3,             "Call Center" );
1 row created.
SQL>
SQL> insert into departments ( department_id, department_name )
  2                    values( 4,             "Communication" );
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE TABLE employees
  2  ( employee_id          number(10)      not null,
  3    last_name            varchar2(50)      not null,
  4    email                varchar2(30),
  5    hire_date            date,
  6    job_id               varchar2(30),
  7    department_id        number(10),
  8    salary               number(6),
  9    manager_id           number(6),
 10    CONSTRAINT           employees_pk PRIMARY KEY (employee_id),
 11    CONSTRAINT           fk_departments FOREIGN KEY (department_id) REFERENCES departments(department_id)
 12  );
Table created.
SQL>
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)
  2                values ( 1001, "Lawson", "lawson@g.ru", "01-JAN-2002","MGR", 30000,1 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                values ( 1002, "Wells", "wells@g.ru", "01-JAN-2002", "DBA", 20000,2, 1005 );
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                 values( 1003, "Bliss", "bliss@g.ru", "01-JAN-2002", "PROG", 24000,3 ,1004);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1004,  "Kyte", "tkyte@a.ru", SYSDATE-3650, "MGR",25000 ,4, 1005);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1005, "Viper", "sdillon@a .ru", SYSDATE, "PROG", 20000, 1, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)
  2                 values( 1006, "Beck", "clbeck@g.ru", SYSDATE, "PROG", 20000, 2, null);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1007, "Java", "java01@g.ru", SYSDATE, "PROG", 20000, 3, 1006);
1 row created.
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1008, "Oracle", "wvelasq@g.ru", SYSDATE, "DBA", 20000, 4, 1006);
1 row created.
SQL>
SQL> select * from employees;
EMPLOYEE_ID LAST_NAME                                          EMAIL                          HIRE_DATE JOB_ID                         DEPARTMENT_ID     SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
       1001 Lawson                                             lawson@g.ru                   01-JAN-02 MGR                                        1      30000       1004
       1002 Wells                                              wells@g.ru                    01-JAN-02 DBA                                        2      20000       1005
       1003 Bliss                                              bliss@g.ru                    01-JAN-02 PROG                                       3      24000       1004
       1004 Kyte                                               tkyte@a.ru                    13-JUN-98 MGR                                        4      25000       1005
       1005 Viper                                              sdillon@a .ru                 10-JUN-08 PROG                                       1      20000       1006
       1006 Beck                                               clbeck@g.ru                   10-JUN-08 PROG                                       2      20000
       1007 Java                                               java01@g.ru                   10-JUN-08 PROG                                       3      20000       1006
EMPLOYEE_ID LAST_NAME                                          EMAIL                          HIRE_DATE JOB_ID                         DEPARTMENT_ID     SALARY MANAGER_ID
----------- -------------------------------------------------- ------------------------------ --------- ------------------------------ ------------- ---------- ----------
       1008 Oracle                                             wvelasq@g.ru                  10-JUN-08 DBA                                        4      20000       1006
8 rows selected.
SQL>
SQL> select * from departments;
DEPARTMENT_ID DEPARTMENT_NA
------------- -------------
            1 Data Group
            2 Purchasing
            3 Call Center
            4 Communication
4 rows selected.
SQL>
SQL>
SQL>
SQL> select e.employee_id, e.last_name, d.department_name
  2        from employees e left outer join departments d
  3          on e.department_id = d.department_id
  4       where e.job_id = "MGR"
  5      /
EMPLOYEE_ID LAST_NAME                                          DEPARTMENT_NA
----------- -------------------------------------------------- -------------
       1001 Lawson                                             Data Group
       1004 Kyte                                               Communication
SQL>
SQL>
SQL> drop table employees cascade constraints;
Table dropped.
SQL> drop table departments cascade constraints;
Table dropped.
SQL>
SQL>



Left outer join demo

 
SQL> CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
  2                    ENAME VARCHAR2(10),
  3                    JOB VARCHAR2(9),
  4                    MGR NUMBER(4),
  5                    HIREDATE DATE,
  6                    SAL NUMBER(7, 2),
  7                    COMM NUMBER(7, 2),
  8                    DEPTNO NUMBER(2));
Table created.
SQL>
SQL> INSERT INTO EMP VALUES (7369, "SMITH", "CLERK",    7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7521, "WARD",  "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7566, "JONES", "MANAGER",  7839, TO_DATE("2-APR-1981",  "DD-MON-YYYY"), 2975, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES (7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES (7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7900, "JAMES", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES (7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES (7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
1 row created.
SQL>
SQL> CREATE TABLE DEPT (DEPTNO NUMBER(2),DNAME VARCHAR2(14),LOC VARCHAR2(13) );
Table created.
SQL>
SQL> INSERT INTO DEPT VALUES (10, "ACCOUNTING", "NEW YORK");
1 row created.
SQL> INSERT INTO DEPT VALUES (20, "RESEARCH", "DALLAS");
1 row created.
SQL> INSERT INTO DEPT VALUES (30, "SALES", "CHICAGO");
1 row created.
SQL> INSERT INTO DEPT VALUES (40, "OPERATIONS", "BOSTON");
1 row created.
SQL>
SQL>
SQL> select * from emp;
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
14 rows selected.
SQL>
SQL> select * from dept;
    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> select e1.ename "Employee",
  2             e2.ename "Reports To"
  3        from emp e1 left outer join emp e2
  4          on e1.MGR = e2.empno
  5       order by e1.empno;
Employee   Reports To
---------- ----------
SMITH      FORD
ALLEN      BLAKE
WARD       BLAKE
JONES      KING
MARTIN     BLAKE
BLAKE      KING
CLARK      KING
SCOTT      JONES
KING
TURNER     BLAKE
ADAMS      SCOTT
JAMES      BLAKE
FORD       JONES
MILLER     CLARK
14 rows selected.
SQL>
SQL>
SQL> drop table dept;
Table dropped.
SQL> drop table emp;
Table dropped.
SQL>
SQL>
SQL>



LEFT OUTER JOIN tableName ON joined columns

 
SQL>
SQL> CREATE TABLE emps (
  2    emp varchar(30)
  3   ,title    varchar(30)
  4  );
Table created.
SQL>
SQL> INSERT INTO emps VALUES ("Tom","Programmer");
1 row created.
SQL> INSERT INTO emps VALUES ("Jack","Tester");
1 row created.
SQL> INSERT INTO emps VALUES ("Mary","Technician");
1 row created.
SQL>
SQL> CREATE TABLE JobLevel (
  2    title     varchar(30)
  3   ,rank    varchar(30)
  4  );
Table created.
SQL>
SQL> INSERT INTO JobLevel VALUES ("Programmer","Level1");
1 row created.
SQL> INSERT INTO JobLevel VALUES ("Tester","Level2");
1 row created.
SQL> INSERT INTO JobLevel VALUES ("Technician","Level3");
1 row created.
SQL>
SQL> CREATE TABLE salary (
  2    rank     varchar(30)
  3   ,payment  DECIMAL(10,2)
  4  );
Table created.
SQL>
SQL> INSERT INTO salary VALUES ("Level1",2000.00);
1 row created.
SQL> INSERT INTO salary VALUES ("Level2",3000.00);
1 row created.
SQL> INSERT INTO salary VALUES ("Level3",5000.00);
1 row created.
SQL> INSERT INTO salary VALUES ("Level4",6000.00);
1 row created.
SQL>
SQL> select * from emps;

EMP
------------------------------
TITLE
------------------------------
Tom
Programmer
Jack
Tester
Mary
Technician

3 rows selected.
SQL> select * from JobLevel;

TITLE
------------------------------
RANK
------------------------------
Programmer
Level1
Tester
Level2
Technician
Level3
3 rows selected.
SQL> select * from salary;

RANK                               PAYMENT
------------------------------  ----------
Level1                                2000
Level2                                3000
Level3                                5000
Level4                                6000
4 rows selected.
SQL>
SQL>
SQL> SELECT salary.rank
  2  FROM salary LEFT OUTER JOIN JobLevel ON (salary.rank = JobLevel.rank)
  3  WHERE JobLevel.rank IS NULL;

RANK
------------------------------
Level4

1 row selected.
SQL>
SQL>
SQL> DROP TABLE emps;
Table dropped.
SQL> DROP TABLE JobLevel;
Table dropped.
SQL> DROP TABLE salary;
Table dropped.
SQL>
SQL>



LEFT OUTER JOIN vs RIGHT OUTER JOIN

 
SQL>
SQL> CREATE TABLE project (
  2    pro_id              NUMBER(4),
  3    pro_name            VARCHAR2(40),
  4    budget          NUMBER(9,2),
  5    CONSTRAINT project_pk   PRIMARY KEY (pro_id)
  6  );
Table created.
SQL>
SQL>
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1001, "A",12345);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1002, "ERP",23456);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1003, "SQL",34567);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1004, "CRM",45678);
1 row created.
SQL> INSERT INTO project(pro_id, pro_name, budget)VALUES (1005, "VPN",56789);
1 row created.
SQL>
SQL>
SQL>
SQL> CREATE TABLE server_usage (
  2    pro_id                   NUMBER(4),
  3    emp_id                  NUMBER,
  4    time_log_date                DATE,
  5    hours_logged                 NUMBER(8,2),
  6    dollars_charged              NUMBER(8,2),
  7    CONSTRAINT server_usage_pk  PRIMARY KEY (pro_id, emp_id, time_log_date)
  8  );
Table created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1001,101,to_date("4-Apr-2004","dd-mon-yyyy"),1123,222);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1002,102,to_date("4-Apr-2005","dd-mon-yyyy"),1124,223);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1003,103,to_date("4-Apr-2006","dd-mon-yyyy"),1125,224);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1004,104,to_date("4-Apr-2007","dd-mon-yyyy"),1126,225);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1005,105,to_date("4-Apr-2008","dd-mon-yyyy"),1127,226);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1001,106,to_date("4-Apr-2009","dd-mon-yyyy"),1128,227);
1 row created.
SQL> INSERT INTO server_usage(pro_id, emp_id, time_log_date, hours_logged, dollars_charged)
  2                    VALUES (1002,107,to_date("4-Apr-2010","dd-mon-yyyy"),1129,228);
1 row created.
SQL>
SQL>
SQL> SET ECHO ON
SQL> SELECT p.pro_name,  ph.time_log_date, ph.hours_logged
  2  FROM project p LEFT OUTER JOIN server_usage ph
  3       ON p.pro_id = ph.pro_id;
A
04-APR-04         1123
ERP
04-APR-05         1124
SQL
04-APR-06         1125
CRM
04-APR-07         1126
VPN
04-APR-08         1127
A
04-APR-09         1128
ERP
04-APR-10         1129

7 rows selected.
SQL>
SQL> SELECT p.pro_name,  ph.time_log_date, ph.hours_logged
  2  FROM server_usage ph RIGHT OUTER JOIN project p
  3       ON p.pro_id = ph.pro_id;
A
04-APR-04         1123
ERP
04-APR-05         1124
SQL
04-APR-06         1125
CRM
04-APR-07         1126
VPN
04-APR-08         1127
A
04-APR-09         1128
ERP
04-APR-10         1129

7 rows selected.
SQL>
SQL> drop table server_usage;
Table dropped.
SQL> drop table project;
Table dropped.