Use an ANSI outer join and use the NVL function to convert nulls to the string "N/A". : Outer Joins « Table Joins « Oracle PL / SQL

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » Table Joins » Outer Joins 
Use an ANSI outer join and use the NVL function to convert nulls to the string "N/A".
  
SQL>
SQL>
SQL> create table product(
  2          product_id              integer           primary key
  3          ,price                  number(7,2)
  4          ,description            varchar2(75)
  5          ,onhand                 number(5,0)
  6          ,reorder                number(5,0)
  7          ,supplier_no            integer
  8  );

Table created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (1,2.50,'Oracle',100,20);

row created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (2,23.00,'SQL Server',null,null);

row created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (3,null,'MySQL',null,null);

row created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (4,1.50,'DB2',50,10);

row created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (5,10.50,'Java',100,20);

row created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (6,45.00,'C++',null,null);

row created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (7,19.99,'Javascript',3,5);

row created.

SQL> insert into product(product_id, price, description, onhand, reorder)values (8,4.50,'Ruby',null,null);

row created.

SQL>
SQL> select from product;
PRODUCT_ID      PRICE DESCRIPTION                                                                     ONHAND    REORDER SUPPLIER_NO
---------- ---------- --------------------------------------------------------------------------- ---------- ---------- -----------
         1        2.5 Oracle                                                                             100         20
         2         23 SQL Server
         3            MySQL
         4        1.5 DB2                                                                                 50         10
         5       10.5 Java                                                                               100         20
         6         45 C++
         7      19.99 Javascript                                                                           3          5
         8        4.5 Ruby

rows selected.

SQL>
SQL> create table product_supplier(
  2             product_id               integer
  3            ,supplier_no              integer
  4            ,price                    number(7,2)
  5            ,primary key (product_id, supplier_no)
  6  );

Table created.

SQL> insert into product_supplier values(1,10,2.25);

row created.

SQL> insert into product_supplier values(1,11,2.10);

row created.

SQL> insert into product_supplier values(1,12,2.85);

row created.

SQL> insert into product_supplier values(2,10,22.25);

row created.

SQL> insert into product_supplier values(2,11,22.00);

row created.

SQL> insert into product_supplier values(2,12,21.25);

row created.

SQL>
SQL> select from product_supplier;
PRODUCT_ID SUPPLIER_NO      PRICE
---------- ----------- ----------
         1          10       2.25
         1          11        2.1
         1          12       2.85
         2          10      22.25
         2          11         22
         2          12      21.25

rows selected.

SQL>
SQL> create table supplier(
  2          supplier_no             integer           primary key
  3          ,supplier_name          varchar2(50)
  4          ,address                varchar(30)
  5          ,city                   varchar(20)
  6          ,state                  varchar2(2)
  7          ,area_code              varchar2(3)
  8          ,phone                  varchar2(8)
  9  );

Table created.

SQL> insert into supplier(supplier_no, supplier_name)values(10,'ABC Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(11,'BCD Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(12,'WWW Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(13,'XYZ Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(14,'R Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(15,'D Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(16,'B Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(17,'W Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(18,'P Gift Supply Co.');

row created.

SQL> insert into supplier(supplier_no, supplier_name)values(19,'R Gift Supply Co.');

row created.

SQL>
SQL> select from supplier;

SUPPLIER_NO SUPPLIER_NAME                                      ADDRESS                        CITY         ST ARE PHONE
----------- -------------------------------------------------- ------------------------------ -------------------- -- --- --------
         10 ABC Gift Supply Co.
         11 BCD Gift Supply Co.
         12 WWW Gift Supply Co.
         13 XYZ Gift Supply Co.
         14 R Gift Supply Co.
         15 D Gift Supply Co.
         16 B Gift Supply Co.
         17 W Gift Supply Co.
         18 P Gift Supply Co.
         19 R Gift Supply Co.

10 rows selected.

SQL>
SQL>
SQL>
SQL> select supplier_name, nvl(description, 'N/A') AS description
  2  from supplier left outer join product_supplier using (supplier_no)
  3                  left outer join product using (product_id);
SUPPLIER_NAME                                      DESCRIPTION
-------------------------------------------------- ---------------------------------------------------------------------------
WWW Gift Supply Co.                                Oracle
BCD Gift Supply Co.                                Oracle
ABC Gift Supply Co.                                Oracle
WWW Gift Supply Co.                                SQL Server
BCD Gift Supply Co.                                SQL Server
ABC Gift Supply Co.                                SQL Server
B Gift Supply Co.                                  N/A
R Gift Supply Co.                                  N/A
P Gift Supply Co.                                  N/A
W Gift Supply Co.                                  N/A
R Gift Supply Co.                                  N/A
D Gift Supply Co.                                  N/A
XYZ Gift Supply Co.                                N/A

13 rows selected.

SQL>
SQL>
SQL>
SQL> drop table supplier;

Table dropped.

SQL> drop table product_supplier;

Table dropped.

SQL> drop table product;

Table dropped.

SQL>
SQL> --

   
  
Related examples in the same category
1. Outer Joins demo
2. Understanding Outer Joins: j.empno = e.empno (+)
3. Understanding Outer Joins: e.empno (+) = j.empno
4. Demo the ANSI outer join
5. Right outer join
6. Inner and Outer Joins(room and class)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.