# Query Managed ClickHouse® databases

Run a query against a Managed ClickHouse® database using a tool of your choice.

To ensure data security, stability, and its proper replication, we equip
our Managed ClickHouse® service with specific features, some
of them missing from the standard ClickHouse offer. Managed ClickHouse takes care of running queries in the distributed mode over
the entire cluster. In the standard ClickHouse, the queries `CREATE`,
`ALTER`, `RENAME` and `DROP` only affect the server where they are run.
In contrast, we ensure the proper distribution across all cluster
machines behind the scenes. You don't need to use `ON CLUSTER` for data
definition queries against a `Replicated` database.

> [!IMPORTANT]
> There are limitations on the number of concurrent queries and the number of concurrent
> connections in Managed ClickHouse:
>
> - `max_concurrent_queries` scales with the plan size, starting at `147` on the smallest plans.
> - `max_connections` is `4000` per node.
>
> See
> [Managed ClickHouse® limits and limitations](/product/dbaas/service-specific/clickhouse/service-boundaries/limits-and-quotas/)
> for details.

For querying your ClickHouse® databases, you can use
[the ClickHouse® client](/product/dbaas/service-specific/clickhouse/how-to/connect-with-clickhouse-cli/).

> [!NOTE]
> Two things in the output can surprise you on a first session:
>
> - A data definition query returns one row per node instead of the empty output stock
>   ClickHouse prints, because it runs as a distributed DDL. See
>   [Read the output of a DDL statement](/product/dbaas/service-specific/clickhouse/how-to/manage-databases-tables/#read-the-output-of-a-ddl-statement).
> - A client newer than your service, such as the one in the `clickhouse/clickhouse-server`
>   latest image, prints an `Unknown settings` warning before every result. It is harmless
>   version skew, see
>   [Connect to Managed ClickHouse with clickhouse-client](/product/dbaas/service-specific/clickhouse/how-to/connect-with-clickhouse-cli/).

## Query a database with a selected tool

### Examples of queries

Retrieve a list of current databases:

```sql
SHOW DATABASES
```

Count rows:

```sql
SELECT COUNT(*) FROM transactions.expenses
```

Create a role:

```sql
CREATE ROLE accountant
```

## Query a non-replicated table

Startup plans run a single node, so this section applies to the multi-node plans: Business
plans run three nodes and Premium plans run six or more.

On those plans, several nodes sit behind the DNS name of your Managed ClickHouse service.
When you query a non-replicated table, for example a log table, requests are routed randomly
to one of the nodes regardless of how data is distributed across them. A particular row is
found only if your `SELECT` query is directed to the node which executed a `WRITE` on
this row.

To query a non-replicated table across all the service nodes, use `clusterAllReplicas`
as follows:

```sql
SELECT *
FROM clusterAllReplicas(default, system.query_log)
WHERE query_id = '1a2b3c4d5e6f7g8h9i0j1a2b3c4d5e6f7g8'
```

