Skip to content
Advertisement

Insert data based on data count from another table

How can I insert the data based on data count from another table in SQL?

Store table:

StoreCode   StoreName
-----------------------
   HQ       HQ Mini Mart    
   BT       BT Mini Mart        

Stock table:

ID  | StkID | StockName | Price
----+-------+-----------+-------
 1  | 1001  | Avocado   | 10
 2  | 1002  | Apple     |  2

Expected result

StoreCode   StkID   StockName   Price
--------------------------------------
HQ            1     Avocado       10
BT            1     Avocado       10
HQ            2     Apple          2
BT            2     Apple          2

Advertisement

Answer

You can use cross join –

select StoreCode,ID,StockName,Price 
from Store cross join Stock 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement