Use LEFT JOIN from orderdetail to the despatch tables.select od.*, isnull(dd.despqty, 0) as despqty, (od.ordqty - isnull(dd.despqty, 0)) as pendingqtyfrom orderdetail od left join ( despatch dp inner join despatchdetail dd on dp.despatchid = dd.despatchid ) on od.orderid = dp.orderid and od.prodid = dd.prodidwhere od.ordqty <> isnull(dd.despqty, 0)
----------------------------------'KH'everything that has a beginning has an end