Hey! This content applies only to previous CARTO products

Please check if it's relevant to your use case. On October 2021 we released a new version of our platform.
You can learn more and read the latest documentation at docs.carto.com

Questions  /  Working with Data  /  SQL

How to select all columns in a table except some of them

Learn how to select all column names in a CARTO dataset except some of them.

Let’s say you want to select all the columns in a hundred-fields table, but if you also select cartodb_id, you may face a conflict later. Would you build the select query by hand? This query will build the query for you, and returns it as a string

SELECT
    'SELECT '
    || array_to_string(
        ARRAY(
            SELECT
                'o' || '.' || c.column_name
            FROM
                information_schema.columns As c
            WHERE
                table_name = 'my_dataset' AND
                c.column_name NOT IN('field_1', 'field_2')
            ),
        ','
        )
    || ' FROM my_dataset As o' As sqlstmt

…so you need to copy&paste the resulting string and use it as needed.