Search code examples
sqloracle-databaseddlsql-view

Is there a way to add a column to a view in ORACLE SQL?


I have a two tables

1.TABLE_STOCK with columns Product_ID(primary key) and Product_unit_price

2.TABLE_SALES with columns Product_ID(foreign key) and Sales_unit_price

Now I wanted to create a view joining the two tables based on Product_ID and add a column PROFIT(which should be the difference between Sales_unit_price and Product_unit_price) to the view.

Is there a way to add a column(PROFIT) to a view?


Solution

  • You can do it as the way you've already described.

    CREATE OR REPLACE VIEW V_PRODUCT
    AS
       SELECT K.PRODUCT_ID,
          K.PRODUCT_UNIT_PRICE,
          S.SALES_UNIT_PRICE,
          S.SALES_UNIT_PRICE - K.PRODUCT_UNIT_PRICE AS PROFIT
     FROM TABLE_STOCK K
          INNER JOIN TABLE_SALES S ON S.PRODUCT_ID = K.PRODUCT_ID