# ALTER QUERY

## Syntax <a href="#synopsis" id="synopsis"></a>

```sql
ALTER QUERY { query_id | query_name[:query_version] }
  { ADD | DROP } TAG tag_key = 'tag_value'
  [, { ADD | DROP } TAG tag_key = 'tag_value' ]...;
```

## Description <a href="#description" id="description"></a>

Modify tags associated with an existing [Streaming or Continuous Query](https://docs.deltastream.io/overview/core-concepts/queries#_streaming_or_continuous_query).

The `ALTER QUERY` statement allows you to add or remove tags from a query. Tags are key-value pairs that can be used for metadata, organization, and filtering purposes.

Queries can only be modified by a [Role](https://docs.deltastream.io/overview/core-concepts/access-control#_role) with appropriate privileges on the query.

### Arguments

#### **query\_id**

Specifies the unique identifier (UUID) of the query to modify. You can find query IDs using the [LIST QUERIES](https://docs.deltastream.io/reference/sql-syntax/command/list-queries) command.

#### **query\_name**

Specifies the name of the query to modify. For case-sensitive names, the name must be wrapped in double quotes, otherwise, the lowercased name will be used.

#### **query\_version**

Specifies the version of the query when using `query_name`. If not provided, the latest version is used.

#### **tag\_key**

Specifies the key name for the tag. Tag keys are string values that identify the tag.

#### **tag\_value**

Specifies the value for the tag. Tag values are string values associated with the tag key.

### Tag Operations

#### `ADD TAG`

Adds a new tag to the query with the specified key-value pair. If a tag with the same key already exists, it will be updated with the new value.

#### `DROP TAG`

Removes the tag with the specified key from the query. The tag value must still be specified in the syntax but is ignored during the operation.

## Examples <a href="#example" id="example"></a>

#### Add tags to a query

Add multiple tags to a query using its ID:

```sql
ALTER QUERY 142ecf31-bc8d-496b-bd0b-28ebb4a8757e 
  ADD TAG 'environment' = 'production',
  ADD TAG 'team' = 'analytics',
  ADD TAG 'cost-center' = 'engineering';
```

#### Add a tag to a query using query name

Add a tag to a query using its name and version:

```sql
ALTER QUERY my_analytics_query:2 
  ADD TAG 'priority' = 'high';
```

#### Remove tags from a query

Remove specific tags from a query:

```sql
ALTER QUERY my_analytics_query 
  DROP TAG 'environment' = 'staging',
  DROP TAG 'deprecated' = 'true';
```

#### Mix add and drop operations

Add some tags while removing others in a single statement:

```sql
ALTER QUERY 142ecf31-bc8d-496b-bd0b-28ebb4a8757e 
  ADD TAG 'environment' = 'production',
  DROP TAG 'environment' = 'staging',
  ADD TAG 'updated_by' = 'admin';
```

## Related Commands

* [LIST QUERIES](https://docs.deltastream.io/reference/sql-syntax/command/list-queries) - List all queries to find query IDs and names
* [DESCRIBE QUERY](https://docs.deltastream.io/reference/sql-syntax/command/describe-query) - View query details including current tags
* [CREATE TABLE AS](https://docs.deltastream.io/reference/sql-syntax/query/create-table-as) - Create streaming queries
* [CREATE MATERIALIZED VIEW AS](https://docs.deltastream.io/reference/sql-syntax/query/materialized-view/create-materialized-view-as) - Create materialized view queries
