Most Basic SQL Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kerry2807
    New Member
    • Jul 2007
    • 3

    Most Basic SQL Question

    Hi, Am starting to teach myself SQL. I cannot figure out how to do the most simple of queries, and it is driving me nuts. Here's the basic scenario:

    Table: Customer
    custid
    firstname
    lastname

    Table: Transactions
    custid
    transdate
    transamount

    I want a query which will display ALL customers and any associated transactions. However, the query I used (below) only returns the customer if there is a transaction for it. For customers with no transactions, they are not being listed and I need help to show me how to structure the query to that the listing of the master table is not dependent on there being any child records in existence.

    [code=sql]select customer.custid , customer.firstn ame, customer.lastna me, transactions.tr ansdate, transactions.tr ansamount
    from customer, transactions where customer.custid =transactions.c ustid[/code]

    Please tell me where I am going wrong!!!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    You'll find SQL experts in the "Database" section under the "Forums" tab, above. There may not be an SQL expert who checks these Misc. Q.s, so try group that most closely matches the tools that you are using (the most popular RDBMSs are listed there).

    Good luck.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by kerry2807
      Hi, Am starting to teach myself SQL. I cannot figure out how to do the most simple of queries, and it is driving me nuts. Here's the basic scenario:

      Table: Customer
      custid
      firstname
      lastname

      Table: Transactions
      custid
      transdate
      transamount

      I want a query which will display ALL customers and any associated transactions. However, the query I used (below) only returns the customer if there is a transaction for it. For customers with no transactions, they are not being listed and I need help to show me how to structure the query to that the listing of the master table is not dependent on there being any child records in existence.

      [code=sql]select customer.custid , customer.firstn ame, customer.lastna me, transactions.tr ansdate, transactions.tr ansamount
      from customer, transactions where customer.custid =transactions.c ustid[/code]

      Please tell me where I am going wrong!!!
      Have you tried just leaving off the where clause?

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        You don't say what database system you are using so the syntax will vary a bit. You will need to use a LEFT JOIN or a LEFT OUTER JOIN. Something like the following

        [code=sql]
        SELECT customer.custid , customer.firstn ame, customer.lastna me, transactions.tr ansdate, transactions.tr ansamount
        FROM customer LEFT JOIN transactions
        ON customer.custid = transactions.cu stid
        [/code]

        However, as I said the syntax will vary. If you tell us the database system you are using we can try to help further.

        Comment

        Working...