title: Efficient filter query in Wikidata
body: <p>I am trying to form an efficient filter query in SPARQL on Wikidata. Let me explain my process:</p>
  
  <ol>
  <li>I query the search-entities API using key words e.g. (Apple, Orange)</li>
  <li>The API query returns a list of relevant item ID's e.g. (wd:Q629269, wd:Q154950, wd:Q312, wd:Q95, wd:Q4878289, wd:Q10817602)</li>
  <li>With this list of ID's, I then query SPARQL and  to return items that are CLASS or are SUBLCASS of certain types e.g. (p:P31/ps:P31/wdt:P279* wd:Q43229) - which returns everything if it is an Organisation or subclass thereof.</li>
  <li>Then for items in the list of ID's, that are of certain CLASS, return information items if they exists e.g. (OPTIONAL). </li>
  </ol>
  
  <p>I am new to SPARQL.  My Question is, is this the most efficient method to achieve this output? It seems to me to be quite inefficient and I cannot find a similar type of problem <a href="https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial" rel="nofollow noreferrer">in the tutorial examples</a>.</p>
  
  <p>You can <a href="https://query.wikidata.org/#%23%20define%20columns%0ASELECT%20%3Fitem%20%3FitemLabel%20%3FitemDescription%20%3Fweb%20%3Finception%20%3FISIN%0A%23%20WHERE%20clause%20takes%20form%3A%20item%20-%20has%20-%20value%0A%23%20items%20should%20be%20prefixed%20with%20wd%3A%20properties%20with%20wdt%3A%28%20But%20this%20only%20applies%20to%20fixed%20values%20%E2%80%93%20variables%20don%E2%80%99t%20get%20a%20prefix%21%29%0AWHERE%7B%0A%20%20FILTER%20%28%3Fitem%20IN%20%28wd%3AQ629269%2C%20wd%3AQ154950%2C%20wd%3AQ312%2C%20wd%3AQ95%2C%20wd%3AQ4878289%2C%20wd%3AQ10817602%29%29%0A%20%20%3Fitem%20p%3AP31%2Fps%3AP31%2Fwdt%3AP279%2A%20wd%3AQ43229.%20%23%20get%20item-company%20and%20its%20subclasses%0A%20%20%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%3Fitem%20wdt%3AP856%20%3Fweb.%20%23%20get%20item-web%0A%20%20%20%20%3Fitem%20wdt%3AP571%20%3Finception.%20%23%20get%20item-web%0A%20%20%20%20%3Fitem%20wdt%3AP946%20%3FISIN.%20%23%20get%20item-isin%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%22.%20%7D%0A%20%20%20%20%7D%0Alimit%2010" rel="nofollow noreferrer">try the query here</a>.</p>
  
  <pre><code>    SELECT distinct ?item ?itemLabel ?itemDescription ?web ?inception ?ISIN
      WHERE{
        FILTER (?item IN (wd:Q629269, wd:Q154950, wd:Q312, wd:Q95, wd:Q4878289, wd:Q10817602))
        ?item p:P31/ps:P31/wdt:P279* wd:Q43229.
        OPTIONAL {
          ?item wdt:P856 ?web. # get item-web
          ?item wdt:P571 ?inception. # get item-web
          ?item wdt:P946 ?ISIN. # get item-isin
              }
        SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
          }
  LIMIT 10
  </code></pre>