title: SQL Multiple table JOINS, GROUP BY and HAVING
body: <p>I've a table structured somewhat similar to this:</p>
  
  <pre><code>CREATE TABLE `user`
   (`id` int, `name` varchar(7));
  CREATE TABLE `email`
    (`id` int, `email_address` varchar(50), `verified_flag` tinyint(1),`user_id` int);
  CREATE TABLE `social`
   (`id` int,`user_id` int);
  
  INSERT INTO `user`
   (`id`, `name`)
  VALUES
   (1,'alex'),
   (2,'jon'),
   (3,'arya'),
   (4,'sansa'),
   (5,'hodor')
  ;
  INSERT INTO `email`
       (`id`,`email_address`,`verified_flag`,`user_id`)
  VALUES
   (1,'alex@gmail.com','1',1),
   (2,'jon@gmail.com','0',1),
   (3,'arya@gmail.com','0',3),
   (4,'sansa@gmail.com','1',4),
   (5,'reek@gmail.com','0',3),
   (6,'hodor@gmail.com','0',5),
   (7,'tyrion@gmail.com','0',1)
  ;
  INSERT INTO `social`
       (`id`,`user_id`)
  VALUES
   (1,4),
   (2,4),
   (3,5),
   (4,4),
   (5,4)
  ;
  </code></pre>
  
  <p>What I want to get is all emails:</p>
  
  <ol>
  <li>which are not verified</li>
  <li>which belongs to a user who has no, i.e 0, verified emails</li>
  <li>which belongs to a user who has no, i.e 0, social records </li>
  </ol>
  
  <p>With the below query I'm able to apply the 1st and 3rd condition but not the 2nd one:</p>
  
  <pre><code>SELECT *
  FROM `email`
  INNER JOIN `user` ON `user`.`id` = `email`.`user_id`
  LEFT JOIN `social` ON `user`.`id` = `social`.`user_id`
  WHERE `email`.`verified_flag` = 0
  GROUP BY `email`.`user_id`,`email`.`email_address`
  HAVING COUNT(`social`.`id`) = 0
  </code></pre>
  
  <p>How can I achieve the result?
  Here's the <a href="http://sqlfiddle.com/#!9/938e88/3" rel="nofollow">sqlfiddle</a> as well</p>