SELECT this.id
, this.activity
, this.date
, this.ref
FROM ( SELECT ref
FROM table
GROUP
BY ref
WHERE activity = 'email'
HAVING COUNT(*) = 0
) AS these
INNER
JOIN table AS this
ON this.ref = these.ref
The where clause is misplaced, it should come directly after the from clause, i.e.
SELECT this.id
, this.activity
, this.date
, this.ref
FROM ( SELECT ref
FROM table
WHERE activity = 'email'
GROUP
BY ref
HAVING COUNT(*) = 0
) AS these
INNER
JOIN table AS this
ON this.ref = these.ref