<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Vertica Documentation – Subqueries used in search conditions</title>
    <link>/en/data-analysis/queries/subqueries/subqueries-used-search-conditions/</link>
    <description>Recent content in Subqueries used in search conditions on Vertica Documentation</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/data-analysis/queries/subqueries/subqueries-used-search-conditions/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Data-Analysis: Combining search conditions</title>
      <link>/en/data-analysis/queries/subqueries/subqueries-used-search-conditions/combining-search-conditions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/queries/subqueries/subqueries-used-search-conditions/combining-search-conditions/</guid>
      <description>
        
        
        &lt;p&gt;Logical operators AND and OR combine two conditions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;AND evaluates to TRUE when both conditions joined by AND are matched.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OR evaluates to TRUE when either condition is matched.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;or-subqueries-complex-expressions&#34;&gt;OR subqueries (complex expressions)&lt;/h2&gt;
&lt;p&gt;Vertica supports subqueries in more complex expressions using OR. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;More than one subquery in the conjunct expression:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;(SELECT MAX(b) FROM t1) + SELECT (MAX FROM t2) a IN (SELECT a FROM t1) OR b IN (SELECT x FROM t2)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;An OR clause in the conjunct expression involves at least one subquery:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;a IN (SELECT a FROM t1) OR b IN (SELECT x FROM t2) a IN (SELECT a from t1) OR b = 5
a = (SELECT MAX FROM t2) OR b = 5
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;One subquery is present but it is part of a another expression:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;x IN (SELECT a FROM t1) = (x = (SELECT MAX FROM t2) (x IN (SELECT a FROM t1) IS NULL
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;evaluation-of-and-queries&#34;&gt;Evaluation of AND queries&lt;/h2&gt;
&lt;p&gt;Vertica treats expressions separated by AND (conjunctive) operators individually. For example, given the following WHERE clause:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;  WHERE (a IN (SELECT a FROM t1) OR b IN (SELECT x FROM t2)) AND (c IN (SELECT a FROM t1))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The query is interpreted as two conjunct expressions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(a IN (SELECT a FROM t1) OR b IN (SELECT x FROM t2))&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(c IN (SELECT a FROM t1))&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first expression is considered a complex subquery, while the second expression is not.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;The following list shows some of the ways you can filter complex conditions in the WHERE clause:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;OR expression between a subquery and a non-subquery condition:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT x FROM t WHERE x &amp;gt; (SELECT SUM(DISTINCT x) FROM t GROUP BY y) OR x &amp;lt; 9;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OR expression between two subqueries:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE x=(SELECT x FROM t) OR EXISTS(SELECT x FROM tt);
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Subquery expression:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE x=(SELECT x FROM t)+1 OR x&amp;lt;&amp;gt;(SELECT x FROM t)+1;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OR expression with [NOT] IN subqueries:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE NOT (EXISTS (SELECT x FROM t)) OR x &amp;gt;9;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OR expression with IS [NOT] NULL subqueries:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE (SELECT * FROM t)IS NULL OR (SELECT * FROM tt)IS NULL;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OR expression with boolean column and subquery that returns Boolean data type:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t2 WHERE x = (SELECT x FROM t2) OR x;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

To return TRUE, the argument of OR must be a Boolean data type.

&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OR expression in the CASE statement:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE CASE WHEN x=1 THEN x &amp;gt; (SELECT * FROM t)
       OR x &amp;lt; (SELECT * FROM t2) END ;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Analytic function, NULL-handling function, string function, math function, and so on:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT x FROM t WHERE x &amp;gt; (SELECT COALESCE (x,y) FROM t GROUP BY x,y) OR
       x &amp;lt; 9;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In user-defined functions (assuming &lt;code&gt;f()&lt;/code&gt; is one):&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE x &amp;gt; 5 OR x = (SELECT f(x) FROM t);
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use of parentheses at different places to restructure the queries:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT x FROM t WHERE (x = (SELECT x FROM t) AND y = (SELECT y FROM t))
       OR (SELECT x FROM t) =1;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Multicolumn subqueries:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE (x,y) = (SELECT x,y FROM t) OR x &amp;gt; 5;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Constant/NULL on lefthand side of subquery:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM t WHERE x &amp;gt; 5 OR 5 = (SELECT x FROM t);
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../../../../en/data-analysis/queries/subqueries/subquery-restrictions/&#34;&gt;Subquery restrictions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: Expressions as subqueries</title>
      <link>/en/data-analysis/queries/subqueries/subqueries-used-search-conditions/expressions-as-subqueries/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/queries/subqueries/subqueries-used-search-conditions/expressions-as-subqueries/</guid>
      <description>
        
        
        &lt;p&gt;Subqueries that return a single value (unlike a list of values returned by IN subqueries) can generally be used anywhere an expression is allowed in SQL: a column name, constant, function, scalar subquery, or a combination of column names, constants, and functions connected by operators or subqueries.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT c1 FROM t1 WHERE c1 = ANY (SELECT c1 FROM t2) ORDER BY c1;
=&amp;gt; SELECT c1 FROM t1 WHERE COALESCE((t1.c1 &amp;gt; ANY (SELECT c1 FROM t2)), TRUE);
=&amp;gt; SELECT c1 FROM t1 GROUP BY c1 HAVING
     COALESCE((t1.c1 &amp;lt;&amp;gt; ALL (SELECT c1 FROM t2)), TRUE);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Multi-column expressions are also supported:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT c1 FROM t1 WHERE (t1.c1, t1.c2) = ALL (SELECT c1, c2 FROM t2);
=&amp;gt; SELECT c1 FROM t1 WHERE (t1.c1, t1.c2) &amp;lt;&amp;gt; ANY (SELECT c1, c2 FROM t2);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Vertica returns an error on queries where more than one row would be returned by any subquery used as an expression:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT c1 FROM t1 WHERE c1 = (SELECT c1 FROM t2) ORDER BY c1;
   ERROR:  more than one row returned by a subquery used as an expression
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../../../../en/data-analysis/queries/subqueries/subquery-restrictions/&#34;&gt;Subquery restrictions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
  </channel>
</rss>
