Skip to content

SQL Formatter Does Not Properly Indent UNION Queries and Derived Tables #1698

@gkjoyes

Description

@gkjoyes

What happened?

The SQL formatter does not correctly format queries that contain a UNION inside a derived table (subquery in the FROM clause).

Instead of maintaining consistent indentation and formatting for nested queries, the formatter collapses parts of the query and places the closing parenthesis directly after the final SELECT, reducing readability.

Steps to Reproduce

  1. Open the SQL editor.

  2. Paste the following query:

    SELECT
        country,
        avg(happiness_score) AS happiness_score
    FROM
        (
        SELECT
            YEAR,
            country,
            happiness_score
        FROM
            happiness_scores
        UNION
        SELECT
            2024,
            country,
            ladder_score
        FROM
            happiness_scores_current
        ) AS final
    GROUP BY
        country;
  3. Run the SQL formatter.

Expected Result

SELECT
    country,
    avg(happiness_score) AS happiness_score
FROM
    (
    SELECT
        YEAR,
        country,
        happiness_score
    FROM
        happiness_scores
    UNION
    SELECT
        2024,
        country,
        ladder_score
    FROM
        happiness_scores_current
    ) AS final
GROUP BY
    country;

The formatter should preserve indentation for the derived table and align the UNION query blocks consistently.

Actual Result

SELECT country,
       avg(happiness_score) AS happiness_score
FROM (
  SELECT YEAR,
         country,
         happiness_score
  FROM happiness_scores

UNION

SELECT 2024,
       country,
       ladder_score
FROM happiness_scores_current) AS final
GROUP BY country;

Database type

MySQL / MariaDB

TablePro version

0.51.1

macOS version & chip

macOS 26.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions