The Vertica Forum is getting a makeover! The site will be undergoing maintenance from Tuesday 8/13 to Friday 8/16. We appreciate your patience and cooperation during this time.

Make Comparisons on Subqueries that Return Multiple Rows

Jim_KnicelyJim_Knicely Administrator
edited May 8 in Tips from the Team

You typically use comparison operators (=, >, < , etc.) only on subqueries that return one row. With ANY and ALL operators, you can make comparisons on subqueries that return multiple rows.

Example:

I want to check if an integer is greater than any of the integers a set of rows. If a try to use the “>”comparison operator I’ll get an error:

dbadmin=> SELECT 5 > (SELECT 5 UNION ALL SELECT 6 UNION SELECT 7) "5 is GT 5, 6 or 7)";
ERROR 4840:  Subquery used as an expression returned more than one row

But if I add the “ANY” key word, I get my answer:

dbadmin=> SELECT 5 > ANY (SELECT 5 UNION ALL SELECT 6 UNION SELECT 7) "5 is GT 5, 6 or 7";
5 is GT 5, 6 or 7
-------------------
f
(1 row)

dbadmin=> SELECT 5 > ANY (SELECT 4 UNION ALL SELECT 6 UNION SELECT 7) "5 is GT 4, 6 or 7";
5 is GT 4, 6 or 7
-------------------
t
(1 row)

Helpful Link:
https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/AnalyzingData/Queries/Subqueries/ANYSOMEAndALL.htm

Have fun!

Sign In or Register to comment.

Can't find what you're looking for? Search the Vertica Documentation, Knowledge Base, or Blog for more information.