---
title: LATERAL VIEW clause
description: Apply generator functions such as EXPLODE to each row with the LATERAL VIEW clause in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-lateral-view
---

# LATERAL VIEW clause

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Used in conjunction with generator functions such as `EXPLODE`, which generates a virtual table containing one or more rows. `LATERAL VIEW` applies the rows to each original output row.

In Databricks SQL and starting with Databricks Runtime 12.2 this clause is deprecated. You should invoke a table valued generator function as a [table\_reference](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference.md).

## Syntax

```
LATERAL VIEW [ OUTER ] generator_function ( expression [, ...] ) [ table_identifier ] AS column_identifier [, ...]
```

## Parameters

* **OUTER**

  If `OUTER` specified, returns null if an input array/map is empty or null.

* **[generator\_function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-tvf.md)**

  A generator function (EXPLODE, INLINE, etc.). If the function is not a generator function, Databricks raises [UNSUPPORTED\_GENERATOR.NOT\_GENERATOR](https://docs.databricks.com/aws/en/error-messages/unsupported-generator-error-class.md#not_generator).

* **[table\_identifier](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-identifiers.md)**

  The alias for `generator_function`, which is optional.

* **[column\_identifier](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-identifiers.md)**

  Lists the column aliases of `generator_function`, which may be used in output rows.
  The number of column identifiers must match the number of columns returned by the generator function. If the number of column identifiers does not match the generator output columns, Databricks raises [UDTF\_ALIAS\_NUMBER\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/error-classes.md#udtf_alias_number_mismatch).

## Common error conditions

* [UDTF\_ALIAS\_NUMBER\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/error-classes.md#udtf_alias_number_mismatch)
* [UNSUPPORTED\_GENERATOR.NOT\_GENERATOR](https://docs.databricks.com/aws/en/error-messages/unsupported-generator-error-class.md#not_generator)

## Examples

```sql
> CREATE TABLE person (id INT, name STRING, age INT, class INT, address STRING);
> INSERT INTO person VALUES
    (100, 'John', 30, 1, 'Street 1'),
    (200, 'Mary', NULL, 1, 'Street 2'),
    (300, 'Mike', 80, 3, 'Street 3'),
    (400, 'Dan', 50, 4, 'Street 4');

> SELECT * FROM person
    LATERAL VIEW EXPLODE(ARRAY(30, 60)) tableName AS c_age
    LATERAL VIEW EXPLODE(ARRAY(40, 80)) AS d_age;
   id    name     age    class     address    c_age    d_age
 ------ ------- ------- -------- ----------- -------- --------
  100    John    30      1        Street 1    30       40
  100    John    30      1        Street 1    30       80
  100    John    30      1        Street 1    60       40
  100    John    30      1        Street 1    60       80
  200    Mary    NULL    1        Street 2    30       40
  200    Mary    NULL    1        Street 2    30       80
  200    Mary    NULL    1        Street 2    60       40
  200    Mary    NULL    1        Street 2    60       80
  300    Mike    80      3        Street 3    30       40
  300    Mike    80      3        Street 3    30       80
  300    Mike    80      3        Street 3    60       40
  300    Mike    80      3        Street 3    60       80
  400    Dan     50      4        Street 4    30       40
  400    Dan     50      4        Street 4    30       80
  400    Dan     50      4        Street 4    60       40
  400    Dan     50      4        Street 4    60       80

> SELECT c_age, COUNT(1) FROM person
     LATERAL VIEW EXPLODE(ARRAY(30, 60)) AS c_age
     LATERAL VIEW EXPLODE(ARRAY(40, 80)) AS d_age
     GROUP BY c_age;
  c_age    count(1)
 -------- -----------
  60       8
  30       8

SELECT * FROM person
    LATERAL VIEW EXPLODE(ARRAY()) tableName AS c_age;
  id    name    age    class    address    c_age
 ----- ------- ------ -------- ---------- --------

> SELECT * FROM person
     LATERAL VIEW OUTER EXPLODE(ARRAY()) tableName AS c_age;
   id    name     age    class     address    c_age
 ------ ------- ------- -------- ----------- --------
  100    John    30      1        Street 1    NULL
  200    Mary    NULL    1        Street 2    NULL
  300    Mike    80      3        Street 3    NULL
  400    Dan     50      4        Street 4    NULL
```

## Related articles

* [SELECT](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md)
