SQL unbalanced parentheses: missing right parenthesis

Every ( in a SQL statement needs a matching ): function calls, IN lists, subqueries, column definitions. When one is missing, the database keeps reading the rest of the statement as if it were inside the parentheses and fails at the first keyword that cannot appear there, or at the end of the input. Database messages rarely say which bracket is open; PasteKit points at the exact one.

Seen as:

  • ORA-00907: missing right parenthesis
  • ORA-00906: missing left parenthesis
  • ERROR: syntax error at or near "FROM"
  • ERROR: syntax error at end of input
  • Msg 102, Level 15, State 1, Line 7 Incorrect syntax near ')'.

Input

Settings

History

Load from URL

Common causes

1. A function call missing its closing parenthesis

In COUNT(order_id AS orders the alias ends up inside the call. Close the call before the alias.

Before
SELECT customer_id, COUNT(order_id AS orders FROM orders GROUP BY customer_id;
After
SELECT customer_id, COUNT(order_id) AS orders FROM orders GROUP BY customer_id;

2. An IN list that is never closed

Long value lists, often generated or pasted, lose their final ). The next clause (GROUP BY, ORDER BY) is then read as part of the list.

Before
SELECT id FROM orders WHERE status IN ('paid', 'shipped' ORDER BY id;
After
SELECT id FROM orders WHERE status IN ('paid', 'shipped') ORDER BY id;

3. A subquery missing its parenthesis

A semicolon or the end of the script inside an open subquery produces “syntax error at end of input” in PostgreSQL. Count the brackets of each nested SELECT.

Before
SELECT name FROM users WHERE id IN (SELECT user_id FROM orders WHERE total > 100;
After
SELECT name FROM users WHERE id IN (SELECT user_id FROM orders WHERE total > 100);

4. One closing parenthesis too many

An extra ) gives “Incorrect syntax near ‘)’” in SQL Server and ORA-00933 or ORA-00907 in Oracle. Remove it.

Before
SELECT COUNT(*)) FROM orders;
After
SELECT COUNT(*) FROM orders;

Frequently asked questions

Does ORA-00907 always mean a parenthesis is missing?

No. Oracle raises it whenever it expected ) and found something else, for example a misplaced keyword in a column definition (such as NOT NULL before DEFAULT) or an ORDER BY inside an IN subquery. Check that the parentheses balance first, then the syntax inside them.

How do I find the unclosed parenthesis in a long query?

Format it: with one clause per line and nested parts indented, an unclosed bracket stands out. PasteKit refuses to format unbalanced SQL and highlights the bracket that has no partner instead.

Can quotes cause bracket errors?

Yes. An unclosed string hides every bracket after it, which can make balanced SQL look unbalanced. See unterminated quoted strings.

Related