---
title: PATTERN clause for MATCH_RECOGNIZE
description: Specify row patterns to match in MATCH\_RECOGNIZE in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize-pattern
---

# PATTERN clause for MATCH\_RECOGNIZE

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

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Specifies the pattern to match in a [MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize.md) operation.

## Syntax

```
PATTERN ( row_pattern )
```

```
row_pattern
  { { pattern_primary | pattern_operator } [ pattern_quantifier ] }

pattern_primary
  { variable
  | $
  | ^ }

pattern_operator
  { ( [ row_pattern ] )
  | PERMUTE ( row_pattern [, ...] )
  | row_pattern | row_pattern
  | row_pattern row_pattern }

pattern_quantifier
  { *
  | +
  | ?
  | { n }
  | { min , [ max ] }
  | { , max } }
```

## Parameters

### Pattern primary

* **variable**

  A variable defined in the [DEFINE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize-define.md) clause. An undefined variable matches all rows.

* **`$`**

  The end of the partition.

* **`^`**

  The beginning of the partition.

  In this release, `^` and `$` are supported only at the leading or trailing position of the top-level pattern. For example, `^ A B $` is supported, but mid-sequence anchors such as `A ^ B` and per-branch anchors such as `(^ A) | (B $)` are not supported.

### Pattern operator

* **`( [ row_pattern ] )`**

  A grouping that overrides precedence.

* **`PERMUTE ( row_pattern [, ...] )`**

  Matches row patterns in any permutation of order. `PERMUTE` requires at least two patterns. If you specify fewer than two, Databricks raises [MATCH\_RECOGNIZE\_PERMUTE\_TOO\_FEW\_PATTERNS](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#match_recognize_permute_too_few_patterns). If you specify more than the supported maximum, Databricks raises [MATCH\_RECOGNIZE\_PERMUTE\_TOO\_MANY\_PATTERNS](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#match_recognize_permute_too_many_patterns).

* **`row_pattern | row_pattern`**

  Matches either of the row patterns.

* **`row_pattern row_pattern`**

  Matches a sequence of row patterns.

### Pattern quantifier

Specifies how often the `pattern_primary` must be matched:

* **`*`**

  Zero or more times.

* **`+`**

  One or more times.

* **`?`**

  At most once.

* **`{ n }`**

  Exactly `n` times. `n` must be an `INTEGER` literal greater than `0`.

* **`{ min , [ max ] }`**

  At least `min` times, and if specified up to `max` times.

* **`{ , max }`**

  No more than `max` times.

If you omit a quantifier, the default is `{ 1 }`: exactly one match.

## Common error conditions

* [MATCH\_RECOGNIZE\_PERMUTE\_TOO\_FEW\_PATTERNS](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#match_recognize_permute_too_few_patterns)
* [MATCH\_RECOGNIZE\_PERMUTE\_TOO\_MANY\_PATTERNS](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#match_recognize_permute_too_many_patterns)

## Related articles

* [MATCH\_RECOGNIZE clause](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize.md)
* [DEFINE clause for MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize-define.md)
* [MEASURES clause for MATCH\_RECOGNIZE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize-measures.md)
