For the following example refer to these tables:
tbl_customer:
cust_ID, cust_name
1, Bob
2, Joe
3, Jason
tbl_purchase:
cust_id, item, qty
1, Blowdryer, 1
1, Bottle of Pepsi, 10
2, Bottle of Pepsi, 5
Question: Is a LEFT JOIN in sql the same as making the drag-connect join in
Access Design view?
No. Just dragging to connect a join in Design View is an "INNER
JOIN". An INNER JOIN selects data from both tables where a record
exists in each. If you joined these two tables on cust_id you would
be returned this:
cust_name, item, qty
Bob, Blowdryer, 1
Bob, Bottle of Pepsi, 10
Joe, Bottle of Pepsi, 5
The SQL for this looks like this:
SELECT tbl_customer.*, tbl_purchase.*
FROM tbl_customer
INNER JOIN tbl_purchase
ON tbl_customer.cust_id = tbl_purchase.cust_id
Now, a LEFT JOIN displayes all records from the leftmost table as well
as all the records from the other table where the value is equal.
With a LEFT JOIN you would get this:
cust_name, item, qty
Bob, Blowdryer, 1
Bob, Bottle of Pepsi, 10
Joe, Bottle of Pepsi, 5
Jason, ,
*Notice that the fields that didn't have matches in tbl_purchase are
null.
SQL for above:
SELECT cust_name, item, qty
FROM tbl_customer
LEFT JOIN tbl_purchase
ON tbl_customer.cust_id = tbl_purchase.cust_id
Now if I wanted to find the customers that didn't have purchases (such
as Jason above) I would use this LEFT JOIN and set the criteria for
the tbl_purchase.cust_id field to Is Null.
SQL:
SELECT cust_name
FROM tbl_customer
LEFT JOIN tbl_purchase
ON tbl_purchase.cust_id = tbl_customer.cust_id
WHERE tbl_purchase.cust_id Is Null
This returns:
cust_name
Jason
Cheers,
Jason Lepack