View Single Post
 
Old 11-09-2012, 04:53 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline Windows 7 32bit Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

Here are couple of ways which should get you started.

Code:
 
--load some example data into 
--a couple of table variables
--to demonstrate the answer
DECLARE @TClients TABLE
    (
      [ClientID] [SMALLINT] ,
      [ClientName] [VARCHAR](30)
    )
DECLARE @TOrders TABLE
    (
      [OrderID] [SMALLINT] ,
      [ClientID] [SMALLINT]
    )
INSERT  INTO @TClients
        ( ClientID, ClientName )
VALUES  ( 1, 'Smith' ),
        ( 2, 'Jones' ),
        ( 3, 'JillaPass' ),
        ( 4, 'King' )
INSERT  INTO @TOrders
        ( OrderID, ClientID )
VALUES  ( 1, 1 ),
        ( 2, 1 ),
        ( 3, 2 ),
        ( 4, 1 )
/*
In this example JillaPass and King have never made an order.
Either of the below queries are a good starting point
*/
--query1
SELECT  c.ClientID ,
        c.ClientName
FROM    @TClients c
        LEFT OUTER JOIN @TOrders o ON o.ClientID = c.ClientID
WHERE   o.ClientID IS NULL
--query2
SELECT  c.ClientID ,
        c.ClientName
FROM    @TClients c
WHERE   NOT EXISTS ( SELECT NULL
                     FROM   @TOrders o
                     WHERE  c.ClientID = o.ClientID )
Reply With Quote