 <p>If you want the five themes that have the most items in common with a given theme, you might try something like:</p> <pre><code>DECLARE @target_id_theme INT; SET @target_id_theme = 1; -- this is the id_theme you want to find similar themes for SELECT t.id_theme, COUNT(*) as matching_things FROM theme AS t LEFT OUTER JOIN theme_color AS tc ON tc.id_theme = t.id_theme LEFT OUTER JOIN theme_tag AS tt ON tt.id_theme = t.id_theme WHERE tc.id_color IN (SELECT id_color FROM theme_color WHERE id_theme = @target_id_theme) OR tt.id_tag IN (SELECT id_tag FROM theme_tag WHERE id_theme = @target_id_theme) GROUP BY t.id_theme ORDER BY COUNT(*) DESC LIMIT 5 </code></pre> <p>Not tested, buyer beware, but I hope you get the idea. This creates a row for every color or tag that matches a color or tag assigned to the @target_id_theme, orders them by count descending, and gives you the top 5.</p>