---
title: SYNTAX_ERROR ClickHouse error
meta:
  description: Learn how to fix the SYNTAX_ERROR in ClickHouse and Tinybird. Understand what causes it and see examples of how to resolve it
---

# SYNTAX_ERROR ClickHouse error

{% callout %}
This error usually means there's a syntax error in your SQL query. Check for missing keywords, incorrect punctuation, or malformed SQL statements.
{% /callout %}

The `SYNTAX_ERROR` in ClickHouse (and Tinybird) happens when there's a syntax error in your SQL query. This can occur due to missing keywords, incorrect punctuation, malformed statements, or using SQL syntax that isn't supported by ClickHouse.

## What causes this error

You'll typically see it when:

* Missing required keywords (SELECT, FROM, etc.)
* Incorrect punctuation or brackets
* Malformed SQL statements
* Using unsupported SQL syntax
* Missing or extra commas, parentheses, or quotes

{% callout type="tip" %}
Error messages point to the problem location. Check trailing commas after the last column in SELECT statements.
{% /callout %}

## Example errors

```sql {% title="Fails: missing FROM clause" %}
SELECT user_id, event_type
```

```sql {% title="Fails: missing closing parenthesis" %}
SELECT * FROM events WHERE user_id = (SELECT id FROM users
```

```sql {% title="Fails: incorrect punctuation" %}
SELECT user_id, event_type, FROM events
```

```sql {% title="Fails: malformed statement" %}
SELECT * events WHERE user_id = 123
```

## How to fix it

### Add missing keywords

Include all required SQL keywords:

```sql {% title="Fix: add missing FROM" %}
SELECT user_id, event_type FROM events
```

### Fix punctuation

Correct punctuation and brackets:

```sql {% title="Fix: correct punctuation" %}
SELECT user_id, event_type FROM events WHERE user_id = 123
```

### Complete statements

Ensure all statements are complete:

```sql {% title="Fix: complete statement" %}
SELECT * FROM events WHERE user_id = (SELECT id FROM users)
```

### Use proper ClickHouse syntax

Use syntax supported by ClickHouse:

```sql {% title="Fix: use ClickHouse syntax" %}
SELECT user_id, event_type FROM events LIMIT 10
```

## Common patterns and solutions

### Basic SELECT statements

Correct SELECT statement syntax:

```sql {% title="Basic SELECT" %}
SELECT user_id, event_type FROM events
SELECT * FROM events WHERE user_id = 123
SELECT user_id, COUNT(*) FROM events GROUP BY user_id
```

### JOIN statements

Correct JOIN syntax:

```sql {% title="JOIN syntax" %}
SELECT u.id, e.event_type
FROM users u
JOIN events e ON u.id = e.user_id
```

### Subquery syntax

Correct subquery syntax:

```sql {% title="Subquery syntax" %}
SELECT * FROM events
WHERE user_id IN (SELECT id FROM users WHERE status = 'active')
```

### INSERT statements

Correct INSERT syntax:

```sql {% title="INSERT syntax" %}
INSERT INTO events (user_id, event_type) VALUES (123, 'click')
INSERT INTO events SELECT user_id, event_type FROM source_table
```

## Advanced solutions

### Using CTEs (Common Table Expressions)

Correct CTE syntax:

```sql {% title="CTE syntax" %}
WITH user_events AS (
    SELECT user_id, COUNT(*) as event_count
    FROM events
    GROUP BY user_id
)
SELECT * FROM user_events WHERE event_count > 10
```

### Using window functions

Correct window function syntax:

```sql {% title="Window function syntax" %}
SELECT user_id, event_type,
       ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY timestamp) as row_num
FROM events
```

### Using array functions

Correct array function syntax:

```sql {% title="Array function syntax" %}
SELECT user_id, has(tags, 'important') as has_important_tag
FROM events
```

## Common syntax mistakes

### Missing keywords

```sql {% title="Wrong: missing FROM" %}
SELECT user_id, event_type
```

```sql {% title="Correct: with FROM" %}
SELECT user_id, event_type FROM events
```

### Incorrect punctuation

```sql {% title="Wrong: extra comma" %}
SELECT user_id, event_type, FROM events
```

```sql {% title="Correct: no extra comma" %}
SELECT user_id, event_type FROM events
```

### Malformed WHERE clauses

```sql {% title="Wrong: incomplete WHERE" %}
SELECT * FROM events WHERE user_id =
```

```sql {% title="Correct: complete WHERE" %}
SELECT * FROM events WHERE user_id = 123
```

### Incorrect JOIN syntax

```sql {% title="Wrong: missing ON" %}
SELECT * FROM users JOIN events
```

```sql {% title="Correct: with ON clause" %}
SELECT * FROM users JOIN events ON users.id = events.user_id
```

## Tinybird-specific notes

In Tinybird, this error often occurs when:

* Writing SQL in Pipes with syntax errors
* Using unsupported SQL syntax in Materialized Views
* Creating API endpoints with malformed queries
* Using SQL from other database systems

To debug in Tinybird:

1. Use the Query Builder to test SQL syntax
2. Check for ClickHouse-specific syntax requirements
3. Validate SQL in Pipe nodes before saving
4. Use syntax highlighting in your editor

{% callout type="tip" %}
The UI syntax highlighter can help spot missing parentheses and other syntax issues.
{% /callout %}

## Best practices

### SQL writing

* Use a SQL editor with syntax highlighting
* Test queries with simple examples first
* Break down complex queries into smaller parts
* Use consistent formatting and indentation

### Error prevention

* Double-check all keywords and punctuation
* Ensure all parentheses are properly closed
* Verify table and column names exist
* Test queries step by step

### Debugging

* Start with simple queries and build complexity
* Use the Query Builder for syntax validation
* Check ClickHouse documentation for syntax
* Use proper SQL formatting tools

## See also

* [ClickHouse SQL Reference](/sql-reference)
* [Working with Data Sources](/forward/core-concepts/data-sources)
* [Common function errors](/sql-reference/clickhouse-errors/UNKNOWN_IDENTIFIER)
