Question: I have a script to count a users points in a simple achievements type script, then display them on a web page in order. I have the following mySQL function, but if a user doesn't have any points as yet (i.e there is no 'pa.userid' to be found, then the user does not get fetched at all) 

```
SELECT u.userid,
SUM(IF(pa.plus = '1', pa.points_amount, 0)) - SUM(IF(pa.plus = '0', pa.points_amount, 0)) AS points
FROM awarded_points pa,
users u WHERE u.userid = pa.userid 
GROUP BY u.userid 
ORDER BY points DESC 
LIMIT 10

```

How would i go about making the above code include users that haven't yet got any points? Is it a complicated change?

Answer: 
Use a `left outer join`

```
SELECT u.userid,
       SUM(IF(pa.plus = '1', pa.points_amount, 0)) - SUM(IF(pa.plus = '0', pa.points_amount, 0)) AS points
FROM users u
LEFT OUTER JOIN awarded_points pa ON u.userid = pa.userid 
GROUP BY u.userid 
ORDER BY points DESC 
LIMIT 10

```

See that [great explanation of joins](http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html)
Use Left join

```
SELECT u.userid, 
SUM(IF(pa.plus = '1', pa.points_amount, 0)) - SUM(IF(pa.plus = '0', pa.points_amount, 0)) AS points 
FROM users as u left join awarded_points pa 
ON u.userid = pa.userid  
GROUP BY u.userid  
ORDER BY points DESC  
LIMIT 10 

```