title: Shortening a SQL query to check entries have specfic values or don't exist
body: <p>I have a table of tags where each row has column "name" and "value". Names are unique.</p>
  
  <p>I have a (fixed) set of names (although over time I may need to add to the set - it's OK to edit the query when that happens)</p>
  
  <p>I need to check that for every name in the set, either there is no row with that name, or if a row exists then the corresponding value is one of two differnt fixed strings (say "ONE" and "TWO")</p>
  
  <p>I also have a limit on the length of the query - it's stored itself in a DB table and must be &lt;2048 characters long.</p>
  
  <p>For each tag, I currently use something like the query below, which works, but when I have more different names in my set, it quickly exceeds the maximum length. </p>
  
  <p>Is there a different (shorter) way of doing it? Query to get the subset of tag names that exist, and then check all of those have one of the two values?</p>
  
  <p>Ideas (that I don't know how to do in SQL): Count the number of tags that have names in my fixed list. Count the number of tags that have names in my fixed list and one of the two permitted values, compare the counts</p>
  
  <p>Get all distinct values of tags in my fixed list. Check the distinct values are a subset of my permitted values list</p>
  
  <p>DB is Oracle, but I'd like a solution that also works with MariaDB. I can use HQL or SQL.</p>
  
  <pre><code>and (
      NOT exists(
        from
          tags T
        where
          T.name = 'FOOBAR'
      )
      OR exists(
        from
          tags T
        where
          T.name = 'FOOBAR'
          and T.value in('ONE', 'TWO')
      )
    )
  </code></pre>