title: Postgresql select based on another select with order, limit and agregate
body: <p>I have a tables like:</p>
  
  <p>books:</p>
  
  <pre><code>id | title      | rating_avr | visible
  1  | 'Overlord' | 5          | true
  2  | 'Avengers' | 10         | false
  </code></pre>
  
  <p>tags_books:</p>
  
  <pre><code>tag_id | book_id | likes
  1      | 1       | 5
  2      | 1       | 25
  1      | 2       | 11
  </code></pre>
  
  <p>tags:</p>
  
  <pre><code>id | name
  1  | 'Adventure'
  2  | 'Drama'
  </code></pre>
  
  <p>Now i need to load books that have tag 'Drama' with LIMIT, ORDER and agregate tags for each book.</p>
  
  <p>I managed to achive this using query:</p>
  
  <pre><code>SELECT b.id, b.title, b.rating_avr, json_agg(json_build_object('id', tb2.tag_id)) as tags
  FROM books b
  LEFT JOIN tags_books tb ON tb.book_id = b.id
  LEFT JOIN tags_books tb2 ON tb2.book_id = b.id
  WHERE tb.tag_id = 1 AND b.visible=true 
  GROUP BY b.id ORDER BY b.rating_avr DESC LIMIT 5
  </code></pre>
  
  <p>What i'm curious about:</p>
  
  <p>1) Is it ok to join same table 2 times? First is for where clause and second to agregate tags.</p>
  
  <p>2) How can i order agregated tags based on likes?</p>
  
  <p>3) Is it a right approach, or maybe there is better way to do it?</p>