How to select rows from Table X who share the least relationships with Table Y? <p>suppose we have the following two tables</p> <pre><code>TABLE: PEOPLE +----+------+ | id | name | +----+------+ | 1 | john | +----+------+ | 2 | mike | +----+------+ | 3 | derp | +----+------+ TABLE: Images +----+-----------+----------+ | id | person_id | image | +----+-----------+----------+ | 1 | 3 | img1.jpg | +----+-----------+----------+ | 2 | 3 | img2.jpg | +----+-----------+----------+ | 3 | 2 | img3.jpg | +----+-----------+----------+ </code></pre> <p>I need to a query that selects all people from <code>people</code> table and orders them ASC by the ones that have the least images in the images table</p> <p>So the order of the returned rows would be</p> <pre><code>John Mike Derp </code></pre>