How to get group details along with members details and common friends (present in group)
<p>I have four tables as follows</p>

<ul>
<li><p>users Table </p>

<p><a href="https://i.stack.imgur.com/zkGoC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zkGoC.png" alt="enter image description here"></a></p></li>
<li><p>group_types table</p>

<p><a href="https://i.stack.imgur.com/ULZ2k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ULZ2k.png" alt="enter image description here"></a></p></li>
<li><p>groups Table </p>

<p><a href="https://i.stack.imgur.com/t2BQR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t2BQR.png" alt="enter image description here"></a></p></li>
<li><p>friends Table </p>

<p><a href="https://i.stack.imgur.com/DFc6Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DFc6Y.png" alt="enter image description here"></a></p></li>
<li><p>group_users Table</p>

<p><a href="https://i.stack.imgur.com/bpX0m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bpX0m.png" alt="enter image description here"></a></p></li>
</ul>

<p>And the Relation view as follows
<a href="https://i.stack.imgur.com/XbDUe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XbDUe.png" alt="enter image description here"></a></p>

<p>I would like to get the all group details ,with group names, group member names,group type, and the user friends count (the friends that are present in group)</p>

<blockquote>
  <p>SAMPLE JSON OUTPUT FOR USER_ID : 1</p>
</blockquote>

<pre><code>{
   group_name    : "second",
   group_type    : "private",
   group_members : "Minion,Ben",
   friends_count : "1"
}
{
   group_name    : "fourth",
   group_type    : "public",
   group_members : "Minion",
   friends_count : "0"
}
</code></pre>

<blockquote>
  <p>EXPLANATION</p>
</blockquote>

<ul>
<li><p>for user_id 1, there are three group_ids out which only two
(2,4)groups are active,and one (5) is inactive.So group names will be
SECOND,FOURTH and corresponding group types will be PRIVATE ,PUBLIC.</p></li>
<li><p>Group 2 has two active members (1 and 2)and group 4 has only one
member (1).</p></li>
<li><p>user 1 friends(minion friends)present in group 2 are(2,5)out of which
user_id 2 is active , so friends count will be 1.</p></li>
<li><p>similarly for group 4 , friends count will be 0.</p></li>
</ul>

<blockquote>
  <p>I tried following two queries</p>
</blockquote>

<p>first get group details</p>

<pre><code>select group_concat(uls.name) "username", gls.name "group name",gtp.name "group type",gus.status
from users uls
inner join groups gls
inner join group_types gtp
inner join group_users gus
on
uls.user_id=gus.user_id and
gls.group_id=gus.group_id and
gtp.type_id=gls.type_id
where gus.status='active' 
group by gls.name, gtp.name, gus.status
</code></pre>

<p>the get friends details with another query
Then filter both query results</p>

<blockquote>
  <p>QUESTION : How can I achieve this?
  <strong>Is there another way to do this using joins</strong></p>
  
  <p>SQL DUMP FILE</p>
</blockquote>

<pre><code>--
-- Table structure for table `friends`
--

CREATE TABLE `friends` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `friend_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `friends`
--

INSERT INTO `friends` (`id`, `user_id`, `friend_id`) VALUES
(1, 1, 2),
(2, 1, 3),
(3, 5, 1),
(4, 3, 5);

-- --------------------------------------------------------

--
-- Table structure for table `groups`
--

CREATE TABLE `groups` (
  `group_id` int(6) NOT NULL,
  `name` varchar(20) NOT NULL,
  `type_id` int(6) NOT NULL,
  `user_id` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `groups`
--

INSERT INTO `groups` (`group_id`, `name`, `type_id`, `user_id`) VALUES
(1, 'first', 1, 5),
(2, 'second', 2, 2),
(3, 'third', 1, 2),
(4, 'fourth', 1, 4),
(5, 'five', 1, 3);

-- --------------------------------------------------------

--
-- Table structure for table `group_type`
--

CREATE TABLE `group_type` (
  `type_id` int(6) NOT NULL,
  `name` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `group_type`
--

INSERT INTO `group_type` (`type_id`, `name`) VALUES
(1, 'public'),
(2, 'private');

-- --------------------------------------------------------

--
-- Table structure for table `group_users`
--

CREATE TABLE `group_users` (
  `user_id` int(6) NOT NULL,
  `group_id` int(6) NOT NULL,
  `status` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `group_users`
--

INSERT INTO `group_users` (`user_id`, `group_id`, `status`) VALUES
(1, 2, 'active'),
(5, 2, 'pending'),
(2, 2, 'active'),
(2, 3, 'pending'),
(2, 5, 'active'),
(1, 4, 'active'),
(1, 5, 'pending');

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `user_id` int(6) NOT NULL,
  `name` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`user_id`, `name`) VALUES
(1, 'Minion'),
(2, 'Ben'),
(3, 'Lakki'),
(4, 'Bob'),
(5, 'Ananth');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `friends`
--
ALTER TABLE `friends`
  ADD PRIMARY KEY (`id`),
  ADD KEY `inviter_id` (`user_id`),
  ADD KEY `invitee_id` (`friend_id`);

--
-- Indexes for table `groups`
--
ALTER TABLE `groups`
  ADD PRIMARY KEY (`group_id`),
  ADD KEY `type_id` (`type_id`);

--
-- Indexes for table `group_type`
--
ALTER TABLE `group_type`
  ADD PRIMARY KEY (`type_id`);

--
-- Indexes for table `group_users`
--
ALTER TABLE `group_users`
  ADD KEY `user_id` (`user_id`),
  ADD KEY `group_id` (`group_id`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`user_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `friends`
--
ALTER TABLE `friends`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `groups`
--
ALTER TABLE `groups`
  MODIFY `group_id` int(6) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `group_type`
--
ALTER TABLE `group_type`
  MODIFY `type_id` int(6) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `user_id` int(6) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
</code></pre>