Each row in the left hand table (LHT) can be associated with 0 or more rows in the right hand table (RHT).
This query returns the max value from RHT or NULL.
LHT:
account
|------------|
|id | name |
|------------|
|1 | Chris |
|2 | Mark |
|3 | Robert|
|------------|
|------------|
|id | name |
|------------|
|1 | Chris |
|2 | Mark |
|3 | Robert|
|------------|
RHT:
payment
|------------------------------|
|id | accountid | paymentdate |
|------------------------------|
|1 | 1 | 2012-01-01 |
|2 | 1 | 2012-02-01 |
|3 | 2 | 2012-01-01 |
|------------------------------|
|------------------------------|
|id | accountid | paymentdate |
|------------------------------|
|1 | 1 | 2012-01-01 |
|2 | 1 | 2012-02-01 |
|3 | 2 | 2012-01-01 |
|------------------------------|
The query:
SELECT a.name,MAX(paymentdate) FROM account a LEFT JOIN payment p ON a.id=p.accountid GROUP BY a.id;
Result:
|--------------------------|
|name | MAX(paymentdate) |
|--------------------------|
|Chris | 2012-02-01 |
|Mark | 2012-01-01 |
|Robert | NULL |
|--------------------------|
|name | MAX(paymentdate) |
|--------------------------|
|Chris | 2012-02-01 |
|Mark | 2012-01-01 |
|Robert | NULL |
|--------------------------|
Thanks to Mark Simmons for this one!