title: Kotlin Exposed - selecting based on sub-query count
body: <p>In my data model I have a very simple one-to-many relationship between <strong>challenges</strong> and it's <strong>whitelist items</strong>.</p>
  
  <p>I am trying to select a challenge filtered by whitelist.  Basically the challenge selection criteria is when the challenge is either does not have any entries in whitelist for itself or the whitelist matches by name.</p>
  
  <p>This can be achieved with quite simple SQL query: </p>
  
  <pre><code>select c.* from challenge c, challenge_whitelist w where (c.id = w."challengeId" and w."userName" = 'testuser') or ((select count(*) where c.id = w."challengeId") = 0);
  </code></pre>
  
  <p>I am unable to translate it to <code>Exposed</code> though:</p>
  
  <pre><code>  // will not compile
    fun listAll(userName: String) {
      ExposedChallenge.wrapRows(
        ChallengeTable.innerJoin(ChallengeWhitelistTable)
          .slice(ChallengeTable.columns)
          .select((ChallengeWhitelistTable.userName eq userName) or (ChallengeTable.innerJoin(ChallengeWhitelistTable).selectAll().count() eq 0))
      ).toList()
    }
  </code></pre>
  
  <p>The <strong>userName</strong> check works correctly but <code>ChallengeTable.innerJoin(ChallengeWhitelistTable).selectAll().count() eq 0)</code> is not qualified as the valid expression (will not compile).</p>
  
  <p>Note that the mappings are super-simple:</p>
  
  <pre><code>object ChallengeTable : IntIdTable() {
    val createdAt = datetime("createdAt")
  }
  
  class ExposedChallenge(id: EntityID&lt;Int&gt;) : IntEntity(id) {
    companion object : IntEntityClass&lt;ExposedChallenge&gt;(ChallengeTable)
    var createdAt by ChallengeTable.createdAt
    val whitelist by ExposedChallengeWhitelist referrersOn ChallengeWhitelistTable.challenge
  }
  
  object ChallengeWhitelistTable : IntIdTable(name = "challenge_whitelist") {
    var userName = varchar("userName", 50)
    var challengeId = integer("challengeId")
    val challenge = reference("challengeId", ChallengeTable).uniqueIndex()
  }
  
  class ExposedChallengeWhitelist(id: EntityID&lt;Int&gt;) : IntEntity(id) {
    companion object : IntEntityClass&lt;ExposedChallengeWhitelist&gt;(ChallengeWhitelistTable)
  
    val challengeId by ChallengeWhitelistTable.challengeId
    val challenge by ExposedChallenge referencedOn ChallengeWhitelistTable.challenge
  }
  </code></pre>
  
  <p>Any help would be appreciated.</p>