Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Build the query up piece-meal - start with the easier parts, and continue to the harder. The key observation to making it work is that you need to use two joins to the same table. This query gives the list of pairs where the two people come from the same country:</p> <pre><code>SELECT p1.nick, p1.country, p2.nick, p2.country FROM tab2 JOIN tab1 AS p1 ON tab2.player = p1.nick JOIN tab1 AS p2 ON tab2.friend = p2.nick WHERE p1.country = p2.country </code></pre> <p>This counts the number of players with one or more friends from the same country:</p> <pre><code>SELECT COUNT(DISTINCT p1.nick) FROM tab2 JOIN tab1 AS p1 ON tab2.player = p1.nick JOIN tab1 AS p2 ON tab2.friend = p2.nick WHERE p1.country = p2.country </code></pre> <p>And, if what you are after is people who only have friends from the same country as they come from (as suggested in the comment), then you need to establish that the player has no friends where the friend's country is not the player's country. You get into the interesting question: does some who has no friends get counted? Since they don't have any friends from the same country, they're not counted by the query below:</p> <pre><code>SELECT COUNT(DISTINCT p1.nick) FROM tab2 JOIN tab1 AS p1 ON tab2.player = p1.nick JOIN tab1 AS p2 ON tab2.friend = p2.nick WHERE p1.country = p2.country AND NOT EXISTS (SELECT * FROM tab2 AS t2 JOIN tab1 AS p3 ON t2.player = p3.nick JOIN tab1 AS p4 ON t2.friend = p4.nick WHERE p3.country != p4.country AND p3.nick = p1.nick ) </code></pre> <p>This says "players who have at least one friend from their own country and where there is no friend for the player who comes from a different country".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload