Create a view combining data from multiple tables

You can create a view combining information from more than one table by selecting more than one table in the work area of the New View window. You can create a simple view from more than one table by selecting the columns that you want to include from different tables and clicking OK. However, this example shows how to create a view that joins information from two different tables and returns only those rows that you want to see, much like using a WHERE clause.

In this example, you create a view that contains only those item numbers for suppliers that can supply an item at lower cost than the current unit cost. This requires selecting ITEM_NUMBER and UNIT_COST from the INVENTORY_LIST table and joining them with SUPPLIER_NUMBER and SUPPLIER_COST from the SUPPLIERS table. A WHERE clause is used to limit the number of rows returned.

To create a view called LOWER_COST:
  1. Navigate to schema LIBRARY1. Right-click Views and select New.
  2. Select INVENTORY_LIST from SAMPLELIB and SUPPLIERS from LIBRARY1.
  3. Click OK. Both tables should appear in the working area of the window.
  4. Select ITEM_NUMBER and UNIT_COST from INVENTORY_LIST.
  5. Select SUPPLIER_NUMBER and SUPPLIER_COST from SUPPLIERS.
  6. To define the join, select ITEM_NUMBER from INVENTORY_LIST and drag it to ITEM_NUMBER in SUPPLIERS. A line is drawn from one column to the other and the Join window opens.
  7. On the Join window, select Return rows with a matching condition (Inner Join).
  8. Click OK.
  9. Click Select Rows to create a WHERE clause for the view. Double-click LIBRARY1.SUPPLIERS.SUPPLIER_COST, then double-click the < operator and finally double-click SAMPLELIB.INVENTORY_LIST.UNIT_COST . As you click the items, they appear in the window. You can also type this in directly.
  10. Click OK to create the view, LOWER_COST.


screen capture of new library window

Note: You can view the SQL used to create this view by selecting Show SQL. You can also edit the SQL by selecting Edit SQL. Edit SQL starts Run SQL Scripts, where you can edit your SQL statement. Be aware, however, that if you change the SQL, you will need to run the statement from Run SQL Scripts rather than returning to the New View window. If you return to the New View window, your changes are not saved.

To display the contents of this new view, right-click LOWER_COST and select View Contents. The rows that you see through this view are only those rows that have a supplier cost that is less than the unit cost.

ITEM_NUMBER UNIT_COST SUPPLIER_NUMBER SUPPLIER_COST
153047 10.00 9988 8.00
153047 10.00 2424 9.00
229740 1.50 1234 1.00
303476 2.00 3366 1.50
073956 20.00 3366 17.00