SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 07-01-2007, 08:31 PM
kerry2807 kerry2807 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 1
kerry2807 is on a distinguished road
Default ANY dialect question:

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.

select customer.custid, customer.firstname, customer.lastname, transactions.transdate, transactions.transamount
from customer, transactions where customer.custid=transactions.custid

Please tell me where I am going wrong!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 07-02-2007, 06:30 PM
MapGeek MapGeek is offline
Junior Member
 
Join Date: Jun 2007
Location: Northern California
Posts: 5
MapGeek is on a distinguished road
Default ANY answer. Re: Most Basic SQL Question

Try a JOIN, specifically a LEFT [OUTER] JOIN (the OUTER keyword is optional). This will return all records in the left table (Customer), including those that have no transactions, along with records in the right table (Transactions) that match a record in the left table (which sounds like all the records in this case). Here's the syntax:

select customer.custid, customer.firstname, customer.lastname, transactions.transdate, transactions.transamount
from customer LEFT JOIN transactions ON customer.custid=transactions.custid

Note that in the records for customers that don't have a transaction, you'll get NULL for all the fields from the Transactions table.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:37 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8