<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – Collection functions</title>
    <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/</link>
    <description>Recent content in Collection functions on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/sql-reference/functions/data-type-specific-functions/collection-functions/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Sql-Reference: APPLY_AVG</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-avg/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-avg/</guid>
      <description>
        
        
        &lt;p&gt;Returns the average of all elements in a collection (array or set) with numeric values.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;APPLY_AVG(&lt;span class=&#34;code-variable&#34;&gt;collection&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;collection&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target collection&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;The following cases return NULL:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;if the input collection is NULL&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;if the input collection contains only null values&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;if the input collection is empty&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the input collection contains a mix of null and non-null elements, only the non-null values are considered in the calculation of the average.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_avg(ARRAY[1,2.4,5,6]);
apply_avg
-----------
3.6
(1 row)
&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/sql-reference/functions/data-type-specific-functions/collection-functions/apply-sum/#&#34;&gt;APPLY_SUM&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: APPLY_COUNT (ARRAY_COUNT)</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-count-array-count/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-count-array-count/</guid>
      <description>
        
        
        &lt;p&gt;Returns the total number of non-null elements in a collection (array or set). To count all elements including nulls, use &lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-count-elements-array-length/#&#34;&gt;APPLY_COUNT_ELEMENTS (ARRAY_LENGTH)&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;APPLY_COUNT(&lt;span class=&#34;code-variable&#34;&gt;collection&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;span class=&#34;sql&#34;&gt;ARRAY_COUNT&lt;/span&gt; is a synonym of &lt;span class=&#34;sql&#34;&gt;APPLY_COUNT&lt;/span&gt;.&lt;/p&gt;
&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;collection&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target collection&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;Null values are not included in the count.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;The array in this example contains six elements, one of which is null:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_count(ARRAY[1,NULL,3,7,8,5]);
apply_count
-------------
5
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: APPLY_COUNT_ELEMENTS (ARRAY_LENGTH)</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-count-elements-array-length/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-count-elements-array-length/</guid>
      <description>
        
        
        &lt;p&gt;Returns the total number of elements in a collection (array or set), including NULLs. To count only non-null values, use &lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-count-array-count/#&#34;&gt;APPLY_COUNT (ARRAY_COUNT)&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;APPLY_COUNT_ELEMENTS(&lt;span class=&#34;code-variable&#34;&gt;collection&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;span class=&#34;sql&#34;&gt;ARRAY_LENGTH&lt;/span&gt; is a synonym of &lt;span class=&#34;sql&#34;&gt;APPLY_COUNT_ELEMENTS&lt;/span&gt;.&lt;/p&gt;
&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;collection&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target collection&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;This function counts all members, including nulls.&lt;/p&gt;
&lt;p&gt;An empty collection (&lt;code&gt;ARRAY[]&lt;/code&gt; or &lt;code&gt;SET[]&lt;/code&gt;) has a length of 0. A collection containing a single null (&lt;code&gt;ARRAY[null]&lt;/code&gt; or &lt;code&gt;SET[null]&lt;/code&gt;) has a length of 1.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;The following array has six elements including one null:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_count_elements(ARRAY[1,NULL,3,7,8,5]);
apply_count_elements
---------------------
                   6
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As the previous example shows, a null element is an element. Thus, an array containing only a null element has one element:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_count_elements(ARRAY[null]);
apply_count_elements
---------------------
                   1
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A set does not contain duplicates. If you construct a set and pass it directly to this function, the result could differ from the number of inputs:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_count_elements(SET[1,1,3]);
apply_count_elements
---------------------
                   2
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: APPLY_MAX</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-max/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-max/</guid>
      <description>
        
        
        &lt;p&gt;Returns the largest non-null element in a collection (array or set). This function is similar to the &lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/max-aggregate/#&#34;&gt;MAX [aggregate]&lt;/a&gt; function; APPLY_MAX operates on elements of a collection and MAX operates on an expression such as a column selection.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;APPLY_MAX(&lt;span class=&#34;code-variable&#34;&gt;collection&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;collection&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target collection&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;This function ignores null elements. If all elements are null or the collection is empty, this function returns null.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_max(ARRAY[1,3.4,15]);
apply_max
-----------
     15.0
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: APPLY_MIN</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-min/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-min/</guid>
      <description>
        
        
        &lt;p&gt;Returns the smallest non-null element in a collection (array or set). This function is similar to the &lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/min-aggregate/#&#34;&gt;MIN [aggregate]&lt;/a&gt; function; APPLY_MIN operates on elements of a collection and MIN operates on an expression such as a column selection.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;APPLY_MIN(&lt;span class=&#34;code-variable&#34;&gt;collection&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;collection&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target collection&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;This function ignores null elements. If all elements are null or the collection is empty, this function returns null.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_min(ARRAY[1,3.4,15]);
apply_min
-----------
       1.0
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: APPLY_SUM</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-sum/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/apply-sum/</guid>
      <description>
        
        
        &lt;p&gt;Computes the sum of all elements in a collection (array or set) of numeric values (INTEGER, FLOAT, NUMERIC, or INTERVAL).&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;APPLY_SUM(&lt;span class=&#34;code-variable&#34;&gt;collection&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;collection&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target collection&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;The following cases return NULL:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;if the input collection is NULL&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;if the input collection contains only null values&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;if the input collection is empty&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT apply_sum(ARRAY[12.5,3,4,1]);
apply_sum
-----------
      20.5
(1 row)
&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/sql-reference/functions/data-type-specific-functions/collection-functions/apply-avg/#&#34;&gt;APPLY_AVG&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: ARRAY_CAT</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-cat/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-cat/</guid>
      <description>
        
        
        &lt;p&gt;Concatenates two arrays of the same element type and dimensionality. For example, ROW elements must have the same fields.&lt;/p&gt;
&lt;p&gt;If the inputs are both bounded, the bound for the result is the sum of the bounds of the inputs.&lt;/p&gt;
&lt;p&gt;If any input is unbounded, the result is unbounded with a binary size that is the sum of the sizes of the inputs.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ARRAY_CAT(&lt;span class=&#34;code-variable&#34;&gt;array1&lt;/span&gt;,&lt;span class=&#34;code-variable&#34;&gt;array2&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;array1&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;array2&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Arrays of matching dimensionality and element type&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;If either input is NULL, the function returns NULL.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;Types are coerced if necessary, as shown in the second example.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT array_cat(ARRAY[1,2], ARRAY[3,4,5]);
array_cat
-----------------------
[1,2,3,4,5]
(1 row)

=&amp;gt; SELECT array_cat(ARRAY[1,2], ARRAY[3,4,5.0]);
array_cat
-----------------------
[&amp;#34;1.0&amp;#34;,&amp;#34;2.0&amp;#34;,&amp;#34;3.0&amp;#34;,&amp;#34;4.0&amp;#34;,&amp;#34;5.0&amp;#34;]
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: ARRAY_CONTAINS</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-contains/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-contains/</guid>
      <description>
        
        
        &lt;p&gt;Returns true if the specified element is found in the array and false if not. Both arguments must be non-null, but the array may be empty.

&lt;div class=&#34;admonition deprecated&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Deprecated&lt;/h4&gt;

This function has been renamed to &lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/contains/#&#34;&gt;CONTAINS&lt;/a&gt;.

&lt;/div&gt;&lt;/p&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: ARRAY_DIMS</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-dims/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-dims/</guid>
      <description>
        
        
        &lt;p&gt;Returns the dimensionality of the input array.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ARRAY_DIMS(&lt;span class=&#34;code-variable&#34;&gt;array&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;array&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target array&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT array_dims(ARRAY[[1,2],[2,3]]);
array_dims
------------
        2
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: ARRAY_FIND</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-find/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/array-find/</guid>
      <description>
        
        
        &lt;p&gt;Returns the ordinal position of a specified element in an array, or -1 if not found. This function uses null-safe equality checks when testing elements.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;ARRAY_FIND(&lt;span class=&#34;code-variable&#34;&gt;array&lt;/span&gt;, { &lt;span class=&#34;code-variable&#34;&gt;value&lt;/span&gt; | &lt;span class=&#34;code-variable&#34;&gt;lambda-expression&lt;/span&gt; })
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;array&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target array.&lt;/dd&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Value to search for; type must match or be coercible to the element type of the array.&lt;/dd&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;lambda-expression&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;&lt;a href=&#34;../../../../../en/sql-reference/language-elements/lambda-functions/&#34;&gt;Lambda function&lt;/a&gt; to apply to each element. The function must return a Boolean value. The first argument to the function is the element, and the optional second element is the index of the element.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;The function returns the first occurrence of the specified element. However, nothing ensures that value is unique in the array:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT array_find(ARRAY[1,2,7,5,7],7);
 array_find
------------
          2
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The function returns -1 if the specified element is not found:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT array_find(ARRAY[1,3,5,7],4);
array_find
------------
        -1
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can search for complex element types:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT ARRAY_FIND(ARRAY[ARRAY[1,2,3],ARRAY[1,null,4]],
                     ARRAY[1,2,3]);
 ARRAY_FIND
------------
          0
(1 row)

=&amp;gt; SELECT ARRAY_FIND(ARRAY[ARRAY[1,2,3],ARRAY[1,null,4]],
                     ARRAY[1,null,4]);
 ARRAY_FIND
------------
          1
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The second example, comparing arrays with null elements, finds a match because ARRAY_FIND uses a null-safe equality check when evaluating elements.&lt;/p&gt;
&lt;h3 id=&#34;lambdas&#34;&gt;Lambdas&lt;/h3&gt;
&lt;p&gt;Consider a table of departments where each department has an array of ROW elements representing employees. The following example searches for a specific employee name in those records. The results show that Alice works (or has worked) for two departments:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT deptID, ARRAY_FIND(employees, e -&amp;gt; e.name = &amp;#39;Alice Adams&amp;#39;) AS &amp;#39;has_alice&amp;#39;
   FROM departments;
 deptID | has_alice
--------+-----------
      1 |         0
      2 |        -1
      3 |         0
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the following example, each person in the table has an array of email addresses, and the function locates fake addresses. The function takes one argument, the array element to test, and calls a regular-expression function that returns a Boolean:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT name, ARRAY_FIND(email, e -&amp;gt; REGEXP_LIKE(e,&amp;#39;example.com&amp;#39;,&amp;#39;i&amp;#39;))
                AS &amp;#39;example.com&amp;#39;
   FROM people;
      name      | example.com
----------------+-------------
 Elaine Jackson |          -1
 Frank Adams    |           0
 Lee Jones      |          -1
 M Smith        |           0
(4 rows)
&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/sql-reference/functions/data-type-specific-functions/collection-functions/contains/#&#34;&gt;CONTAINS&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/filter/#&#34;&gt;FILTER&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: CONTAINS</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/contains/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/contains/</guid>
      <description>
        
        
        &lt;p&gt;Returns true if the specified element is found in the collection and false if not. This function uses null-safe equality checks when testing elements.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;CONTAINS(&lt;span class=&#34;code-variable&#34;&gt;collection&lt;/span&gt;, { &lt;span class=&#34;code-variable&#34;&gt;value&lt;/span&gt; | &lt;span class=&#34;code-variable&#34;&gt;lambda-expression&lt;/span&gt; })
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;collection&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Target collection (&lt;a href=&#34;../../../../../en/sql-reference/data-types/complex-types/array/#&#34;&gt;ARRAY&lt;/a&gt; or &lt;a href=&#34;../../../../../en/sql-reference/data-types/complex-types/set/#&#34;&gt;SET&lt;/a&gt;).&lt;/dd&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Value to search for; type must match or be coercible to the element type of the collection.&lt;/dd&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;lambda-expression&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;&lt;a href=&#34;../../../../../en/sql-reference/language-elements/lambda-functions/&#34;&gt;Lambda function&lt;/a&gt; to apply to each element. The function must return a Boolean value. The first argument to the function is the element, and the optional second element is the index of the element.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT CONTAINS(SET[1,2,3,4],2);
 contains
----------
t
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can search for NULL as an element value:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT CONTAINS(ARRAY[1,null,2],null);
 contains
----------
 t
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can search for complex element types:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT CONTAINS(ARRAY[ARRAY[1,2,3],ARRAY[1,null,4]],
                   ARRAY[1,2,3]);
 CONTAINS
----------
 t
(1 row)

=&amp;gt; SELECT CONTAINS(ARRAY[ARRAY[1,2,3],ARRAY[1,null,4]],
                   ARRAY[1,null,4]);
 CONTAINS
----------
 t
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The second example, comparing arrays with null elements, returns true because CONTAINS uses a null-safe equality check when evaluating elements.&lt;/p&gt;
&lt;p&gt;In the following example, the orders table has the following definition:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE EXTERNAL TABLE orders(
  orderid int,
  accountid int,
  shipments Array[
    ROW(
      shipid int,
      address ROW(
        street varchar,
        city varchar,
        zip int
      ),
      shipdate date
    )
  ]
 ) AS COPY FROM &amp;#39;...&amp;#39; PARQUET;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following query tests for a specific order. When passing a ROW literal as the second argument, cast any ambiguous fields to ensure type matches:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT CONTAINS(shipments,
            ROW(1,ROW(&amp;#39;911 San Marcos St&amp;#39;::VARCHAR,
                  &amp;#39;Austin&amp;#39;::VARCHAR, 73344),
            &amp;#39;2020-11-05&amp;#39;::DATE))
   FROM orders;
 CONTAINS
----------
 t
 f
 f
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;lambdas&#34;&gt;Lambdas&lt;/h3&gt;
&lt;p&gt;Consider a table of departments where each department has an array of ROW elements representing employees. The following query finds departments with early hires (low employee IDs):&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT deptID FROM departments
   WHERE CONTAINS(employees, e -&amp;gt; e.id &amp;lt; 20);
 deptID
--------
      1
      3
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the following example, a schedules table includes an array of events, where each event is a ROW with several fields:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE schedules
       (guest VARCHAR,
       events ARRAY[ROW(e_date DATE, e_name VARCHAR, price NUMERIC(8,2))]);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can use the &lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/contains/#&#34;&gt;CONTAINS&lt;/a&gt; function with a lambda expression to find people who have more than one event on the same day. The second argument, &lt;code&gt;idx&lt;/code&gt;, is the index of the current element:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT guest FROM schedules
WHERE CONTAINS(events, (e, idx) -&amp;gt;
                       (idx &amp;lt; ARRAY_LENGTH(events) - 1)
                       AND (e.e_date = events[idx + 1].e_date));
    guest
-------------
 Alice Adams
(1 row)
&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/sql-reference/functions/data-type-specific-functions/collection-functions/array-find/#&#34;&gt;ARRAY_FIND&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/filter/#&#34;&gt;FILTER&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: EXPLODE</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/explode/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/explode/</guid>
      <description>
        
        
        &lt;p&gt;Expands the elements of one or more collection columns (&lt;a href=&#34;../../../../../en/sql-reference/data-types/complex-types/array/#&#34;&gt;ARRAY&lt;/a&gt; or &lt;a href=&#34;../../../../../en/sql-reference/data-types/complex-types/set/#&#34;&gt;SET&lt;/a&gt;) into individual table rows, one row per element. For each exploded collection, the results include two columns, one for the element index, and one for the value at that position. If the function explodes a single collection, these columns are named &lt;code&gt;position&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; by default. If the function explodes two or more collections, the columns for each collection are named &lt;code&gt;pos_&lt;/code&gt;&lt;em&gt;&lt;code&gt;column-name&lt;/code&gt;&lt;/em&gt; and &lt;code&gt;val_&lt;/code&gt;&lt;em&gt;&lt;code&gt;column-name&lt;/code&gt;&lt;/em&gt;. You can use an AS clause in the SELECT to change these column names.&lt;/p&gt;
&lt;p&gt;EXPLODE and &lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/unnest/#&#34;&gt;UNNEST&lt;/a&gt; both expand collections.  They have the following differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;By default, EXPLODE expands only the first collection it is passed and UNNEST expands all of them. See the &lt;code&gt;explode_count&lt;/code&gt; and &lt;code&gt;explode_all&lt;/code&gt; parameters.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;By default, EXPLODE returns element positions in an &lt;code&gt;index&lt;/code&gt; column and UNNEST does not. See the &lt;code&gt;with_offsets&lt;/code&gt; parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;By default, EXPLODE requires an OVER clause and UNNEST ignores an OVER clause if present. See the &lt;code&gt;skip_partitioning&lt;/code&gt; parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;EXPLODE (&lt;span class=&#34;code-variable&#34;&gt;column&lt;/span&gt;[,...] [USING PARAMETERS &lt;span class=&#34;code-variable&#34;&gt;param&lt;/span&gt;=&lt;span class=&#34;code-variable&#34;&gt;value&lt;/span&gt;])
[ OVER ( [&lt;span class=&#34;code-variable&#34;&gt;&lt;a href=&#34;../../../../../en/sql-reference/language-elements/window-clauses/window-partition-clause/#&#34;&gt;window-partition-clause&lt;/a&gt;&lt;/span&gt;] ) ]
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;column&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Column in the table being queried. Unless &lt;code&gt;explode_all&lt;/code&gt; is true, you must specify at least as many collection columns as the value of the &lt;code&gt;explode_count&lt;/code&gt; parameter. Columns that are not collections are passed through without modification.
&lt;p&gt;Passthrough columns are not needed if &lt;code&gt;skip_partitioning&lt;/code&gt; is true.&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;OVER(...)&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;How to partition and sort input data. The input data is the result set that the query returns after it evaluates FROM, WHERE, GROUP BY, and HAVING clauses. For EXPLODE, use OVER() or OVER(PARTITION BEST).
&lt;p&gt;This clause is ignored if &lt;code&gt;skip_partitioning&lt;/code&gt; is true.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;explode_all&lt;/code&gt; (BOOLEAN)&lt;/dt&gt;
&lt;dd&gt;If true, explode all collection columns. When &lt;code&gt;explode_all&lt;/code&gt; is true, passthrough columns are not permitted.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; false&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;explode_count&lt;/code&gt; (INT)&lt;/dt&gt;
&lt;dd&gt;The number of collection columns to explode. The function checks each column, up to this value, and either explodes it if is a collection or passes it through if it is not a collection or if this limit has been reached. If the value of &lt;code&gt;explode_count&lt;/code&gt; is greater than the number of collection columns specified, the function returns an error.
&lt;p&gt;If &lt;code&gt;explode_all&lt;/code&gt; is true, you cannot specify &lt;code&gt;explode_count&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; 1&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;skip_partitioning&lt;/code&gt; (BOOLEAN)&lt;/dt&gt;
&lt;dd&gt;Whether to skip partitioning and ignore the OVER clause if present. EXPLODE translates a single row of input into multiple rows of output, one per collection element. There is, therefore, usually no benefit to partitioning the input first. Skipping partitioning can help a query avoid an expensive sort or merge operation. Even so, setting this parameter can negatively affect performance in rare cases.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; false&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;with_offset&lt;/code&gt; (BOOLEAN)&lt;/dt&gt;
&lt;dd&gt;Whether to return the index of each element.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; true&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;This function expands each element in a collection into a row, including null elements. If the input column is NULL or an empty collection, the function produces no rows for that column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT EXPLODE(ARRAY[1,2,null,4]) OVER();
 position | value
----------+-------
        0 |     1
        1 |     2
        2 |
        3 |     4
(4 rows)

=&amp;gt; SELECT EXPLODE(ARRAY[]::ARRAY[INT]) OVER();
 position | value
----------+-------
(0 rows)

=&amp;gt; SELECT EXPLODE(NULL::ARRAY[INT]) OVER();
 position | value
----------+-------
(0 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a name=&#34;Joining&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;joining-on-results&#34;&gt;Joining on results&lt;/h2&gt;
&lt;p&gt;To use JOIN with this function you must set the &lt;code&gt;skip_partitioning&lt;/code&gt; parameter, either in the function call or as a session parameter.&lt;/p&gt;
&lt;p&gt;You can use the output of this function as if it were a relation by using CROSS JOIN or LEFT JOIN LATERAL in a query. Other JOIN types are not supported.&lt;/p&gt;
&lt;p&gt;Consider the following table of students and exam scores:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM tests;
 student |    scores     |    questions
---------+---------------+-----------------
 Bob     | [92,78,79]    | [20,20,100]
 Lee     |               |
 Pat     | []            | []
 Sam     | [97,98,85]    | [20,20,100]
 Tom     | [68,75,82,91] | [20,20,100,100]
(5 rows)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following query finds the best test scores across all students who have scores:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER SESSION SET UDPARAMETER FOR ComplexTypesLib skip_partitioning = true;

=&amp;gt; SELECT student, score FROM tests
   CROSS JOIN EXPLODE(scores) AS t (pos, score)
   ORDER BY score DESC;
 student | score
---------+-------
 Sam     |    98
 Sam     |    97
 Bob     |    92
 Tom     |    91
 Sam     |    85
 Tom     |    82
 Bob     |    79
 Bob     |    78
 Tom     |    75
 Tom     |    68
(10 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following query returns maximum and average per-question scores, considering both the exam score and the number of questions:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT student, MAX(score/qcount), AVG(score/qcount) FROM tests
   CROSS JOIN EXPLODE(scores, questions USING PARAMETERS explode_count=2)
      AS t(pos_s, score, pos_q, qcount)
   GROUP BY student;
 student |         MAX          |       AVG
---------+----------------------+------------------
 Bob     | 4.600000000000000000 | 3.04333333333333
 Sam     | 4.900000000000000000 | 3.42222222222222
 Tom     | 4.550000000000000000 |             2.37
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;These queries produce results for three of the five students. One student has a null value for scores and another has an empty array. These rows are not included in the function&#39;s output.&lt;/p&gt;
&lt;p&gt;To include null and empty arrays in output, use LEFT JOIN LATERAL instead of CROSS JOIN:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT student, MIN(score), AVG(score) FROM tests
LEFT JOIN LATERAL EXPLODE(scores) AS t (pos, score)
GROUP BY student;
 student | MIN |       AVG
---------+-----+------------------
 Bob     |  78 |               83
 Lee     |     |
 Pat     |     |
 Sam     |  85 | 93.3333333333333
 Tom     |  68 |               79
(5 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The LATERAL keyword is required with LEFT JOIN. It is optional for CROSS JOIN.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;Consider an orders table with the following contents:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT orderkey, custkey, prodkey, orderprices, email_addrs
   FROM orders LIMIT 5;
  orderkey  | custkey |                    prodkey                    |            orderprices            |                                                  email_addrs
------------+---------+-----------------------------------------------+-----------------------------------+----------------------------------------------------------------------------------------------------------------
 113-341987 |  342799 | [&amp;#34;MG-7190 &amp;#34;,&amp;#34;VA-4028 &amp;#34;,&amp;#34;EH-1247 &amp;#34;,&amp;#34;MS-7018 &amp;#34;] | [&amp;#34;60.00&amp;#34;,&amp;#34;67.00&amp;#34;,&amp;#34;22.00&amp;#34;,&amp;#34;14.99&amp;#34;] | [&amp;#34;bob@example,com&amp;#34;,&amp;#34;robert.jones@example.com&amp;#34;]
 111-952000 |  342845 | [&amp;#34;ID-2586 &amp;#34;,&amp;#34;IC-9010 &amp;#34;,&amp;#34;MH-2401 &amp;#34;,&amp;#34;JC-1905 &amp;#34;] | [&amp;#34;22.00&amp;#34;,&amp;#34;35.00&amp;#34;,null,&amp;#34;12.00&amp;#34;]    | [&amp;#34;br92@cs.example.edu&amp;#34;]
 111-345634 |  342536 | [&amp;#34;RS-0731 &amp;#34;,&amp;#34;SJ-2021 &amp;#34;]                       | [&amp;#34;50.00&amp;#34;,null]                    | [null]
 113-965086 |  342176 | [&amp;#34;GW-1808 &amp;#34;]                                  | [&amp;#34;108.00&amp;#34;]                        | [&amp;#34;joe.smith@example.com&amp;#34;]
 111-335121 |  342321 | [&amp;#34;TF-3556 &amp;#34;]                                  | [&amp;#34;50.00&amp;#34;]                         | [&amp;#34;789123@example-isp.com&amp;#34;,&amp;#34;alexjohnson@example.com&amp;#34;,&amp;#34;monica@eng.example.com&amp;#34;,&amp;#34;sara@johnson.example.name&amp;#34;,null]
(5 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following query explodes the order prices for a single customer. The other two columns are passed through and are repeated for each returned row:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT EXPLODE(orderprices, custkey, email_addrs
                  USING PARAMETERS skip_partitioning=true)
            AS (position, orderprices, custkey, email_addrs)
   FROM orders WHERE custkey=&amp;#39;342845&amp;#39; ORDER BY orderprices;
 position | orderprices | custkey |         email_addrs
----------+-------------+---------+------------------------------
        2 |             |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;,null]
        3 |       12.00 |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;,null]
        0 |       22.00 |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;,null]
        1 |       35.00 |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;,null]
(4 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The previous example uses the &lt;code&gt;skip_partitioning&lt;/code&gt; parameter. Instead of setting it for each call to EXPLODE, you can set it as a session parameter. EXPLODE is part of the ComplexTypesLib UDx library. The following example returns the same results:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER SESSION SET UDPARAMETER FOR ComplexTypesLib skip_partitioning=true;

=&amp;gt; SELECT EXPLODE(orderprices, custkey, email_addrs)
            AS (position, orderprices, custkey, email_addrs)
   FROM orders WHERE custkey=&amp;#39;342845&amp;#39; ORDER BY orderprices;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can explode more than one column by specifying the &lt;code&gt;explode_count&lt;/code&gt; parameter:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT EXPLODE(orderkey, prodkey, orderprices
                  USING PARAMETERS explode_count=2, skip_partitioning=true)
          AS (orderkey,pk_idx,pk_val,ord_idx,ord_val)
   FROM orders
   WHERE orderkey=&amp;#39;113-341987&amp;#39;;
  orderkey  | pk_idx |  pk_val  | ord_idx | ord_val
------------+--------+----------+---------+---------
 113-341987 |      0 | MG-7190  |       0 |   60.00
 113-341987 |      0 | MG-7190  |       1 |   67.00
 113-341987 |      0 | MG-7190  |       2 |   22.00
 113-341987 |      0 | MG-7190  |       3 |   14.99
 113-341987 |      1 | VA-4028  |       0 |   60.00
 113-341987 |      1 | VA-4028  |       1 |   67.00
 113-341987 |      1 | VA-4028  |       2 |   22.00
 113-341987 |      1 | VA-4028  |       3 |   14.99
 113-341987 |      2 | EH-1247  |       0 |   60.00
 113-341987 |      2 | EH-1247  |       1 |   67.00
 113-341987 |      2 | EH-1247  |       2 |   22.00
 113-341987 |      2 | EH-1247  |       3 |   14.99
 113-341987 |      3 | MS-7018  |       0 |   60.00
 113-341987 |      3 | MS-7018  |       1 |   67.00
 113-341987 |      3 | MS-7018  |       2 |   22.00
 113-341987 |      3 | MS-7018  |       3 |   14.99
(16 rows)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following example uses a multi-dimensional array:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT name, pingtimes FROM network_tests;
 name |                       pingtimes
------+-------------------------------------------------------
 eng1 | [[24.24,25.27,27.16,24.97],[23.97,25.01,28.12,29.5]]
 eng2 | [[27.12,27.91,28.11,26.95],[29.01,28.99,30.11,31.56]]
 qa1  | [[23.15,25.11,24.63,23.91],[22.85,22.86,23.91,31.52]]
(3 rows)

=&amp;gt; SELECT EXPLODE(name, pingtimes USING PARAMETERS explode_count=1) OVER()
   FROM network_tests;
 name | position |           value
------+----------+---------------------------
 eng1 |        0 | [24.24,25.27,27.16,24.97]
 eng1 |        1 | [23.97,25.01,28.12,29.5]
 eng2 |        0 | [27.12,27.91,28.11,26.95]
 eng2 |        1 | [29.01,28.99,30.11,31.56]
 qa1  |        0 | [23.15,25.11,24.63,23.91]
 qa1  |        1 | [22.85,22.86,23.91,31.52]
(6 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can rewrite the previous query as follows to produce the same results:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT name, EXPLODE(pingtimes USING PARAMETERS skip_partitioning=true)
   FROM network_tests;
&lt;/code&gt;&lt;/pre&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: FILTER</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/filter/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/filter/</guid>
      <description>
        
        
        &lt;p&gt;Takes an input array and returns an array containing only elements that meet a specified condition. This function uses null-safe equality checks when testing elements.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;FILTER(&lt;span class=&#34;code-variable&#34;&gt;array&lt;/span&gt;, &lt;span class=&#34;code-variable&#34;&gt;lambda-expression&lt;/span&gt; )
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;array&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Input array.&lt;/dd&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;lambda-expression&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;&lt;a href=&#34;../../../../../en/sql-reference/language-elements/lambda-functions/&#34;&gt;Lambda function&lt;/a&gt; to apply to each element. The function must return a Boolean value. The first argument to the function is the element, and the optional second element is the index of the element.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;Given a table that contains names and arrays of email addresses, the following query filters out fake email addresses and returns the rest:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT name, FILTER(email, e -&amp;gt; NOT REGEXP_LIKE(e,&amp;#39;example.com&amp;#39;,&amp;#39;i&amp;#39;)) AS &amp;#39;real_email&amp;#39;
   FROM people;
      name      |                   real_email
----------------+-------------------------------------------------
 Elaine Jackson | [&amp;#34;ejackson@somewhere.org&amp;#34;,&amp;#34;elaine@jackson.com&amp;#34;]
 Frank Adams    | []
 Lee Jones      | [&amp;#34;lee.jones@somewhere.org&amp;#34;]
 M Smith        | [&amp;#34;ms@msmith.com&amp;#34;]
(4 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can use the results in a WHERE clause to exclude rows that no longer contain any email addresses:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT name, FILTER(email, e -&amp;gt; NOT REGEXP_LIKE(e,&amp;#39;example.com&amp;#39;,&amp;#39;i&amp;#39;)) AS &amp;#39;real_email&amp;#39;
   FROM people
   WHERE ARRAY_LENGTH(real_email) &amp;gt; 0;
      name      |                   real_email
----------------+-------------------------------------------------
 Elaine Jackson | [&amp;#34;ejackson@somewhere.org&amp;#34;,&amp;#34;elaine@jackson.com&amp;#34;]
 Lee Jones      | [&amp;#34;lee.jones@somewhere.org&amp;#34;]
 M Smith        | [&amp;#34;ms@msmith.com&amp;#34;]
(3 rows)
&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/sql-reference/functions/data-type-specific-functions/collection-functions/contains/#&#34;&gt;CONTAINS&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/array-find/#&#34;&gt;ARRAY_FIND&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: IMPLODE</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/implode/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/implode/</guid>
      <description>
        
        
        &lt;p&gt;Takes a column of any scalar type and returns an unbounded array. Combined with GROUP BY, this function can be used to reverse an &lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/explode/#&#34;&gt;EXPLODE&lt;/a&gt; operation.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt; if the WITHIN GROUP ORDER BY clause specifies a column or set of columns that resolves to unique element values within each output array group.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/volatile-functions/&#34; title=&#34;&#34;&gt;Volatile&lt;/a&gt; otherwise because results are non-commutative.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;IMPLODE (&lt;span class=&#34;code-variable&#34;&gt;input-column&lt;/span&gt; [ USING PARAMETERS &lt;span class=&#34;code-variable&#34;&gt;param&lt;/span&gt;=&lt;span class=&#34;code-variable&#34;&gt;value&lt;/span&gt;[,...] ] )
    [ &lt;span class=&#34;code-variable&#34;&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/within-group-order-by-clause/#&#34;&gt;within-group-order-by-clause&lt;/a&gt;&lt;/span&gt; ]
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;input-column&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Column of any scalar type from which to create the array.&lt;/dd&gt;
&lt;dt&gt;&lt;span class=&#34;code-variable&#34;&gt;[within-group-order-by-clause](/en/sql-reference/functions/aggregate-functions/within-group-order-by-clause/)&lt;/span&gt;&lt;/dt&gt;
&lt;dd&gt;Sorts elements within each output array group:
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;WITHIN GROUP (ORDER BY { &lt;span class=&#34;code-variable&#34;&gt;column-expression&lt;/span&gt;[ &lt;span class=&#34;code-variable&#34;&gt;sort-qualifiers&lt;/span&gt; ] }[,...])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;em&gt;&lt;code&gt;sort-qualifiers&lt;/code&gt;&lt;/em&gt;: &lt;code&gt;{ ASC | DESC [ NULLS { FIRST | LAST | AUTO } ] }&lt;/code&gt;

&lt;div class=&#34;alert admonition tip&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Tip&lt;/h4&gt;

WITHIN GROUP ORDER BY can consume a large amount of memory per group. To minimize memory consumption, create projections that support &lt;a href=&#34;../../../../../en/data-analysis/query-optimization/group-by-queries/group-by-implementation-options/#GROUPBY&#34;&gt;GROUPBY PIPELINED&lt;/a&gt;.

&lt;/div&gt;&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;allow_truncate&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;Boolean, if true truncates results when output length exceeds column size. If false (the default), the function returns an error if the output array is too large.
&lt;p&gt;Even if this parameter is set to true, IMPLODE returns an error if any single array element is too large. Truncation removes elements from the output array but does not alter individual elements.&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;max_binary_size&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;The maximum binary size in bytes for the returned array. If you omit this parameter, IMPLODE uses the value of the configuration parameter &lt;a href=&#34;../../../../../en/sql-reference/config-parameters/general-parameters/#DefaultArrayBinarySize&#34;&gt;DefaultArrayBinarySize&lt;/a&gt;.&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;Consider a table with the following contents:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM filtered;

 position | itemprice | itemkey
----------+-----------+---------
        0 |     14.99 |     345
        0 |     27.99 |     567
        1 |     18.99 |     567
        1 |     35.99 |     345
        2 |     14.99 |     123
(5 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following query calls IMPLODE to assemble prices into arrays (grouped by keys):&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT itemkey AS key, IMPLODE(itemprice) AS prices
    FROM filtered GROUP BY itemkey ORDER BY itemkey;
 key |      prices
-----+-------------------
 123 | [&amp;#34;14.99&amp;#34;]
 345 | [&amp;#34;35.99&amp;#34;,&amp;#34;14.99&amp;#34;]
 567 | [&amp;#34;27.99&amp;#34;,&amp;#34;18.99&amp;#34;]
(3 rows)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can modify this query by including a WITHIN GROUP ORDER BY clause, which specifies how to sort array elements within each group:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT itemkey AS key, IMPLODE(itemprice) WITHIN GROUP (ORDER BY itemprice) AS prices
    FROM filtered GROUP BY itemkey ORDER BY itemkey;
 key |      prices
-----+-------------------
 123 | [&amp;#34;14.99&amp;#34;]
 345 | [&amp;#34;14.99&amp;#34;,&amp;#34;35.99&amp;#34;]
 567 | [&amp;#34;18.99&amp;#34;,&amp;#34;27.99&amp;#34;]
(3 rows)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;See &lt;a href=&#34;../../../../../en/data-analysis/queries/arrays-and-sets-collections/#&#34;&gt;Arrays and sets (collections)&lt;/a&gt; for a fuller example.&lt;/p&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: SET_UNION</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/set-union/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/set-union/</guid>
      <description>
        
        
        &lt;p&gt;Returns a &lt;a href=&#34;../../../../../en/sql-reference/data-types/complex-types/set/#&#34;&gt;SET&lt;/a&gt; containing all elements of two input sets.&lt;/p&gt;
&lt;p&gt;If the inputs are both bounded, the bound for the result is the sum of the bounds of the inputs.&lt;/p&gt;
&lt;p&gt;If any input is unbounded, the result is unbounded with a binary size that is the sum of the sizes of the inputs.&lt;/p&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;SET_UNION(&lt;span class=&#34;code-variable&#34;&gt;set1&lt;/span&gt;,&lt;span class=&#34;code-variable&#34;&gt;set2&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;set1&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;set2&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Sets of matching element type&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Null arguments are ignored. If one of the inputs is null, the function returns the non-null input. In other words, an argument of NULL is equivalent to SET[].&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If both inputs are null, the function returns null.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT SET_UNION(SET[1,2,4], SET[2,3,4,5.9]);
set_union
-----------------------
[&amp;#34;1.0&amp;#34;,&amp;#34;2.0&amp;#34;,&amp;#34;3.0&amp;#34;,&amp;#34;4.0&amp;#34;,&amp;#34;5.9&amp;#34;]
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: STRING_TO_ARRAY</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/string-to-array/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/string-to-array/</guid>
      <description>
        
        
        &lt;p&gt;Splits a string containing array values and returns a native one-dimensional array. The output does not include the &amp;quot;ARRAY&amp;quot; keyword. This function does not support nested (multi-dimensional) arrays.&lt;/p&gt;
&lt;p&gt;This function returns array elements as strings by default. You can cast to other types, as in the following example:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT STRING_TO_ARRAY(&amp;#39;[1,2,3]&amp;#39;)::ARRAY[INT];
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;behavior&#34;&gt;Behavior&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;STRING_TO_ARRAY(&lt;span class=&#34;code-variable&#34;&gt;string&lt;/span&gt; [USING PARAMETERS &lt;span class=&#34;code-variable&#34;&gt;param&lt;/span&gt;=&lt;span class=&#34;code-variable&#34;&gt;value&lt;/span&gt;[,...]])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following syntax is deprecated:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;STRING_TO_ARRAY(&lt;span class=&#34;code-variable&#34;&gt;string&lt;/span&gt;, &lt;span class=&#34;code-variable&#34;&gt;delimiter&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;string&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;String representation of a one-dimensional array; can be a VARCHAR or LONG VARCHAR column, a literal string, or the string output of an expression.
&lt;p&gt;Spaces in the string are removed unless elements are individually quoted. For example, &lt;code&gt;&#39; a,b,c&#39;&lt;/code&gt; is equivalent to &lt;code&gt;&#39;a,b,c&#39;&lt;/code&gt;. To preserve the space, use &lt;code&gt;&#39;&amp;quot; a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;&#39;&lt;/code&gt;.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;p&gt;These parameters behave the same way as the corresponding options when loading delimited data (see &lt;a href=&#34;../../../../../en/sql-reference/statements/copy/parsers/delimited/#&#34;&gt;DELIMITED&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;No parameter may have the same value as any other parameter.&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;collection_delimiter&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;The character or character sequence used to separate array elements (VARCHAR(8)). You can use any ASCII values in the range E&#39;\000&#39; to E&#39;\177&#39;, inclusive.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; Comma (&#39;,&#39;).&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;collection_open&lt;/code&gt;, &lt;code&gt;collection_close&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;The characters that mark the beginning and end of the array (VARCHAR(8)). It is an error to use these characters elsewhere within the list of elements without escaping them. These characters can be omitted from the input string.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; Square brackets (&#39;[&#39; and &#39;]&#39;).&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;collection_null_element&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;The string representing a null element value (VARCHAR(65000)). You can specify a null value using any ASCII values in the range E&#39;\001&#39; to E&#39;\177&#39; inclusive (any ASCII value except NULL: E&#39;\000&#39;).
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; &#39;null&#39;&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;collection_enclose&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;An optional quote character within which to enclose individual elements, allowing delimiter characters to be embedded in string values. You can choose any ASCII value in the range E&#39;\001&#39; to E&#39;\177&#39; inclusive (any ASCII character except NULL: E&#39;\000&#39;). Elements do not need to be enclosed by this value.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; double quote (&#39;&amp;quot;&#39;)&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;The function uses comma as the default delimiter. You can specify a different value:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT STRING_TO_ARRAY(&amp;#39;[1,3,5]&amp;#39;);
 STRING_TO_ARRAY
-----------------
 [&amp;#34;1&amp;#34;,&amp;#34;3&amp;#34;,&amp;#34;5&amp;#34;]
(1 row)

=&amp;gt; SELECT STRING_TO_ARRAY(&amp;#39;[t|t|f|t]&amp;#39; USING PARAMETERS collection_delimiter = &amp;#39;|&amp;#39;);
  STRING_TO_ARRAY
-------------------
 [&amp;#34;t&amp;#34;,&amp;#34;t&amp;#34;,&amp;#34;f&amp;#34;,&amp;#34;t&amp;#34;]
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The bounding brackets are optional:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT STRING_TO_ARRAY(&amp;#39;t|t|f|t&amp;#39; USING PARAMETERS collection_delimiter = &amp;#39;|&amp;#39;);
  STRING_TO_ARRAY
-------------------
 [&amp;#34;t&amp;#34;,&amp;#34;t&amp;#34;,&amp;#34;f&amp;#34;,&amp;#34;t&amp;#34;]
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The input can use other characters for open and close:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT STRING_TO_ARRAY(&amp;#39;{NASA-1683,NASA-7867,SPX-76}&amp;#39; USING PARAMETERS collection_open = &amp;#39;{&amp;#39;, collection_close = &amp;#39;}&amp;#39;);
          STRING_TO_ARRAY
------------------------------------
 [&amp;#34;NASA-1683&amp;#34;,&amp;#34;NASA-7867&amp;#34;,&amp;#34;SPX-76&amp;#34;]
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;By default the string &#39;null&#39; in input is treated as a null value:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT STRING_TO_ARRAY(&amp;#39;{&amp;#34;us-1672&amp;#34;,null,&amp;#34;darpa-1963&amp;#34;}&amp;#39; USING PARAMETERS collection_open = &amp;#39;{&amp;#39;, collection_close = &amp;#39;}&amp;#39;);
        STRING_TO_ARRAY
-------------------------------
 [&amp;#34;us-1672&amp;#34;,null,&amp;#34;darpa-1963&amp;#34;]
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the following example, the input comes from a column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT STRING_TO_ARRAY(name USING PARAMETERS collection_delimiter=&amp;#39; &amp;#39;) FROM employees;
    STRING_TO_ARRAY
-----------------------
 [&amp;#34;Howard&amp;#34;,&amp;#34;Wolowitz&amp;#34;]
 [&amp;#34;Sheldon&amp;#34;,&amp;#34;Cooper&amp;#34;]
(2 rows)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: TO_JSON</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/to-json/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/to-json/</guid>
      <description>
        
        
        &lt;p&gt;Returns the JSON representation of a complex-type argument, including mixed and nested complex types. This is the same format that queries of complex-type columns return.&lt;/p&gt;
&lt;h2 id=&#34;behavior&#34;&gt;Behavior&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;TO_JSON(&lt;span class=&#34;code-variable&#34;&gt;value&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Column or literal of a complex type&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;These examples query the following table:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT name, contact FROM customers;
        name        |                                                        contact
--------------------+-----------------------------------------------------------------------------------------------------------------------
Missy Cooper       | {&amp;#34;street&amp;#34;:&amp;#34;911 San Marcos St&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Austin&amp;#34;,&amp;#34;zipcode&amp;#34;:73344,&amp;#34;email&amp;#34;:[&amp;#34;missy@mit.edu&amp;#34;,&amp;#34;mcooper@cern.gov&amp;#34;]}
Sheldon Cooper     | {&amp;#34;street&amp;#34;:&amp;#34;100 Main St Apt 4B&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[&amp;#34;shelly@meemaw.name&amp;#34;,&amp;#34;cooper@caltech.edu&amp;#34;]}
Leonard Hofstadter | {&amp;#34;street&amp;#34;:&amp;#34;100 Main St Apt 4A&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[&amp;#34;hofstadter@caltech.edu&amp;#34;]}
Leslie Winkle      | {&amp;#34;street&amp;#34;:&amp;#34;23 Fifth Ave Apt 8C&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[]}
Raj Koothrappali   | {&amp;#34;street&amp;#34;:null,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[&amp;#34;raj@available.com&amp;#34;]}
Stuart Bloom       |
(6 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can call TO_JSON on a column or on specific fields or array elements:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT TO_JSON(contact) FROM customers;
    to_json
-----------------------------------------------------------------------------------------------------------------------
{&amp;#34;street&amp;#34;:&amp;#34;911 San Marcos St&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Austin&amp;#34;,&amp;#34;zipcode&amp;#34;:73344,&amp;#34;email&amp;#34;:[&amp;#34;missy@mit.edu&amp;#34;,&amp;#34;mcooper@cern.gov&amp;#34;]}
{&amp;#34;street&amp;#34;:&amp;#34;100 Main St Apt 4B&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[&amp;#34;shelly@meemaw.name&amp;#34;,&amp;#34;cooper@caltech.edu&amp;#34;]}
{&amp;#34;street&amp;#34;:&amp;#34;100 Main St Apt 4A&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[&amp;#34;hofstadter@caltech.edu&amp;#34;]}
{&amp;#34;street&amp;#34;:&amp;#34;23 Fifth Ave Apt 8C&amp;#34;,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[]}
{&amp;#34;street&amp;#34;:null,&amp;#34;city&amp;#34;:&amp;#34;Pasadena&amp;#34;,&amp;#34;zipcode&amp;#34;:91001,&amp;#34;email&amp;#34;:[&amp;#34;raj@available.com&amp;#34;]}

(6 rows)

=&amp;gt; SELECT TO_JSON(contact.email) FROM customers;
    to_json
---------------------------------------------
[&amp;#34;missy@mit.edu&amp;#34;,&amp;#34;mcooper@cern.gov&amp;#34;]
[&amp;#34;shelly@meemaw.name&amp;#34;,&amp;#34;cooper@caltech.edu&amp;#34;]
[&amp;#34;hofstadter@caltech.edu&amp;#34;]
[]
[&amp;#34;raj@available.com&amp;#34;]

(6 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When calling TO_JSON with a SET, note that duplicates are removed and elements can be reordered:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT TO_JSON(SET[1683,7867,76,76]);
    TO_JSON
----------------
[76,1683,7867]
(1 row)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Sql-Reference: UNNEST</title>
      <link>/en/sql-reference/functions/data-type-specific-functions/collection-functions/unnest/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/functions/data-type-specific-functions/collection-functions/unnest/</guid>
      <description>
        
        
        &lt;p&gt;Expands the elements of one or more collection columns (&lt;a href=&#34;../../../../../en/sql-reference/data-types/complex-types/array/#&#34;&gt;ARRAY&lt;/a&gt; or &lt;a href=&#34;../../../../../en/sql-reference/data-types/complex-types/set/#&#34;&gt;SET&lt;/a&gt;) into individual rows. If called with a single array, UNNEST returns the elements in a column named &lt;code&gt;value&lt;/code&gt;. If called with two or more arrays, it returns columns named &lt;code&gt;val_&lt;/code&gt;&lt;em&gt;&lt;code&gt;column-name&lt;/code&gt;&lt;/em&gt;. You can use an AS clause in the SELECT to change these names.&lt;/p&gt;
&lt;p&gt;UNNEST and &lt;a href=&#34;../../../../../en/sql-reference/functions/data-type-specific-functions/collection-functions/explode/#&#34;&gt;EXPLODE&lt;/a&gt; both expand collections.  They have the following differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;By default, UNNEST expands all passed collections and EXPLODE expands only the first. See the &lt;code&gt;explode_count&lt;/code&gt; and &lt;code&gt;explode_all&lt;/code&gt; parameters.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;By default, UNNEST returns only the elements and EXPLODE also returns their positions in an &lt;code&gt;index&lt;/code&gt; column. See the &lt;code&gt;with_offsets&lt;/code&gt; parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;By default, UNNEST does not partition its input and ignores an OVER() clause if present. See the &lt;code&gt;skip_partitioning&lt;/code&gt; parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;behavior-type&#34;&gt;Behavior type&lt;/h2&gt;
&lt;a class=&#34;glosslink&#34; href=&#34;../../../../../en/glossary/immutable-invariant-functions/&#34; title=&#34;&#34;&gt;Immutable&lt;/a&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;UNNEST (&lt;span class=&#34;code-variable&#34;&gt;column&lt;/span&gt;[,...])
 [USING PARAMETERS &lt;span class=&#34;code-variable&#34;&gt;param&lt;/span&gt;=&lt;span class=&#34;code-variable&#34;&gt;value&lt;/span&gt;])
[ OVER ( [&lt;span class=&#34;code-variable&#34;&gt;&lt;a href=&#34;../../../../../en/sql-reference/language-elements/window-clauses/window-partition-clause/#&#34;&gt;window-partition-clause&lt;/a&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;arguments&#34;&gt;Arguments&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;column&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;Column in the table being queried. If &lt;code&gt;explode_all&lt;/code&gt; is false, you must specify at least as many collection columns as the value of the &lt;code&gt;explode_count&lt;/code&gt; parameter. Columns that are not collections are passed through without modification.
&lt;p&gt;Passthrough columns are not needed if &lt;code&gt;skip_partitioning&lt;/code&gt; is true.&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;OVER(...)&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;How to partition and sort input data. The input data is the result set that the query returns after it evaluates FROM, WHERE, GROUP BY, and HAVING clauses.
&lt;p&gt;This clause only applies if &lt;code&gt;skip_partitioning&lt;/code&gt; is false.&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;explode_all&lt;/code&gt; (BOOLEAN)&lt;/dt&gt;
&lt;dd&gt;If true, explode all collection columns. When &lt;code&gt;explode_all&lt;/code&gt; is true, passthrough columns are not permitted.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; true&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;explode_count&lt;/code&gt; (INT)&lt;/dt&gt;
&lt;dd&gt;The number of collection columns to explode. The function checks each column, up to this value, and either explodes it if is a collection or passes it through if it is not a collection or if this limit has been reached. If the value of &lt;code&gt;explode_count&lt;/code&gt; is greater than the number of collection columns specified, the function returns an error.
&lt;p&gt;If &lt;code&gt;explode_all&lt;/code&gt; is true, you cannot specify &lt;code&gt;explode_count&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; 1&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;skip_partitioning&lt;/code&gt; (BOOLEAN)&lt;/dt&gt;
&lt;dd&gt;Whether to skip partitioning and ignore the OVER clause if present. UNNEST translates a single row of input into multiple rows of output, one per collection element. There is, therefore, usually no benefit to partitioning the input first. Skipping partitioning can help a query avoid an expensive sort or merge operation.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; true&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;with_offset&lt;/code&gt; (BOOLEAN)&lt;/dt&gt;
&lt;dd&gt;Whether to return the index of each element as an additional column.
&lt;p&gt;&lt;strong&gt;Default:&lt;/strong&gt; false&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;null-handling&#34;&gt;Null-handling&lt;/h2&gt;
&lt;p&gt;This function expands each element in a collection into a row, including null elements. If the input column is NULL or an empty collection, the function produces no rows for that column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT UNNEST(ARRAY[1,2,null,4]) OVER();
 value
-------
     1
     2

     4
(4 rows)

=&amp;gt; SELECT UNNEST(ARRAY[]::ARRAY[INT]) OVER();
 value
-------
(0 rows)

=&amp;gt; SELECT UNNEST(NULL::ARRAY[INT]) OVER();
 value
-------
(0 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;joining-on-results&#34;&gt;Joining on results&lt;/h2&gt;
&lt;p&gt;You can use the output of this function as if it were a relation by using CROSS JOIN or LEFT JOIN LATERAL in a query. Other JOIN types are not supported.&lt;/p&gt;
&lt;p&gt;Consider the following table of students and exam scores:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM tests;
 student |    scores     |    questions
---------+---------------+-----------------
 Bob     | [92,78,79]    | [20,20,100]
 Lee     |               |
 Pat     | []            | []
 Sam     | [97,98,85]    | [20,20,100]
 Tom     | [68,75,82,91] | [20,20,100,100]
(5 rows)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following query finds the best test scores across all students who have scores:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT student, score FROM tests
CROSS JOIN UNNEST(scores) AS t (score)
ORDER BY score DESC;
 student | score
---------+-------
 Sam     |    98
 Sam     |    97
 Bob     |    92
 Tom     |    91
 Sam     |    85
 Tom     |    82
 Bob     |    79
 Bob     |    78
 Tom     |    75
 Tom     |    68
(10 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following query returns maximum and average per-question scores, considering both the exam score and the number of questions:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT student, MAX(score/qcount), AVG(score/qcount) FROM tests
CROSS JOIN UNNEST(scores, questions) AS t(score, qcount)
GROUP BY student;
 student |         MAX          |       AVG
---------+----------------------+------------------
 Bob     | 4.600000000000000000 | 3.04333333333333
 Sam     | 4.900000000000000000 | 3.42222222222222
 Tom     | 4.550000000000000000 |             2.37
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;These queries produce results for three of the five students. One student has a null value for scores and another has an empty array. These rows are not included in the function&#39;s output.&lt;/p&gt;
&lt;p&gt;To include null and empty arrays in output, use LEFT JOIN LATERAL instead of CROSS JOIN:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT student, MIN(score), AVG(score) FROM tests
LEFT JOIN LATERAL UNNEST(scores) AS t (score)
GROUP BY student;
 student | MIN |       AVG
---------+-----+------------------
 Bob     |  78 |               83
 Lee     |     |
 Pat     |     |
 Sam     |  85 | 93.3333333333333
 Tom     |  68 |               79
(5 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The LATERAL keyword is required with LEFT JOIN. It is optional for CROSS JOIN.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;p&gt;Consider a table with the following definition:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE orders (
        orderkey VARCHAR, custkey INT,
        prodkey ARRAY[VARCHAR], orderprices ARRAY[DECIMAL(12,2)],
        email_addrs ARRAY[VARCHAR]);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following query expands one of the array columns. One of the elements is null:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT UNNEST(orderprices) AS price, custkey, email_addrs
   FROM orders WHERE custkey=&amp;#39;342845&amp;#39; ORDER BY price;
 price | custkey |       email_addrs
-------+---------+-------------------------
       |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;]
 12.00 |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;]
 22.00 |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;]
 35.00 |  342845 | [&amp;#34;br92@cs.example.edu&amp;#34;]
(4 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;UNNEST can expand more than one column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT orderkey, UNNEST(prodkey, orderprices)
   FROM orders WHERE orderkey=&amp;#39;113-341987&amp;#39;;
  orderkey  | val_prodkey | val_orderprices
------------+-------------+-----------------
 113-341987 | MG-7190     |           60.00
 113-341987 | MG-7190     |           67.00
 113-341987 | MG-7190     |           22.00
 113-341987 | MG-7190     |           14.99
 113-341987 | VA-4028     |           60.00
 113-341987 | VA-4028     |           67.00
 113-341987 | VA-4028     |           22.00
 113-341987 | VA-4028     |           14.99
 113-341987 | EH-1247     |           60.00
 113-341987 | EH-1247     |           67.00
 113-341987 | EH-1247     |           22.00
 113-341987 | EH-1247     |           14.99
 113-341987 | MS-7018     |           60.00
 113-341987 | MS-7018     |           67.00
 113-341987 | MS-7018     |           22.00
 113-341987 | MS-7018     |           14.99
(16 rows)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
  </channel>
</rss>
