How to find records that are present in one table not in another in sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vikram Raman
    New Member
    • Feb 2010
    • 24

    How to find records that are present in one table not in another in sql server

    Hello ,


    I have two tables that are Customer and Bill.

    Customer table has the following fields

    CustomerId,Name ,Address,PhoneN o

    Bill table has the following fields

    Billno,Customer Id,Date,Amount

    Now i am able to find the records who have paid the bill using the followin query.


    select *from Bill,CustomerId where Bill.CustomerId =Customer.Custo merId

    I am not able to find the records who have not paid the bill.... That is I have to fetch the records that are present in Customer table not in Bill table

    Please any one send me the query for this...

    Thank you
  • sashi
    Recognized Expert Top Contributor
    • Jun 2006
    • 1749

    #2
    Modify the below query as required.

    SELECT <field_1>, <field_2>, <field_n> FROM TABLE <table_name>
    WHERE <bill_flag_fiel d> <> 'Paid'
    Kindly refer to below attached link for more details;
    SQL Tutorial

    Comment

    • Vikram Raman
      New Member
      • Feb 2010
      • 24

      #3
      Yes Mr.Sashi. I tried it out.It shows error... and I tried the '''where not exists'' query..It works.... Anyway thank you very much....

      Comment

      • sashi
        Recognized Expert Top Contributor
        • Jun 2006
        • 1749

        #4
        Thanks for sharing. Good luck :)

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          Be careful with SQL's 3-Logic-Value.

          Also, read this...

          Happy Coding!!!

          ~~ CK

          Comment

          • raveengo
            New Member
            • Mar 2010
            • 4

            #6
            --try this which are the customers are not billed between to days

            select custid,custname from customer where custid not in(
            select custid from bill where billdate between @fromdate and @todate)

            Comment

            Working...