This query returns columns in a database sorted by their name length.
Query
select c.column_name,
length(c.column_name) as column_name_length,
c.table_schema,
c.table_name
from information_schema.columns c
join information_schema.tables t
on t.table_schema = c.table_schema
and t.table_name = c.table_name
where t.table_schema not in ('information_schema', 'pg_catalog')
and t.table_type = 'BASE TABLE'
order by column_name_length desc,
column_name;
Columns
- column_name - column name
- column_name_length - column name length
- table_schema - column table schema name
- table_name - column table name
Rows
- One row represents one column of each table in a database
- Scope of rows: each column that exists in a database
- Ordered by length descrending - from longest to shortest