SQL Query to Get Column Names: A full breakdown
When you start working with relational databases, one of the first tasks you’ll encounter is discovering which columns a table contains. Whether you are building a data pipeline, writing a JOIN across multiple tables, or simply need to document a schema, knowing how to retrieve column names efficiently is essential. This article walks you through the most common methods for obtaining column names across major database platforms—MySQL, PostgreSQL, SQL Server, and Oracle—using standard SQL and vendor‑specific shortcuts. You’ll also learn why each approach works, how to interpret the results, and where to find additional metadata when you need it.
Why You Might Need Column Names
- Schema exploration – Quickly see what fields exist before writing complex queries.
- Dynamic reporting – Generate column lists for UI dropdowns or data validation rules.
- Migration and auditing – Compare source and target structures during ETL processes.
- Documentation – Automate the creation of data dictionaries or ER diagrams.
The ability to pull column names programmatically can save you hours of manual inspection, especially when dealing with large or frequently changing schemas.
1. Using the Information Schema (Standard SQL)
Most modern RDBMS provide a set of system views called information_schema that hold metadata about the database objects. This approach works across MySQL, PostgreSQL, SQL Server (with slight variations), and Oracle (using ALL_TAB_COLUMNS). The core view you’ll query is COLUMNS, which contains one row per column in each table.
Basic Query Structure
SELECT
COLUMN_NAME,
DATA_TYPE,
IS_NULLABLE,
COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_database'
AND TABLE_NAME = 'your_table';
Explanation of key fields
- COLUMN_NAME – The name of the column.
- DATA_TYPE – The underlying data type (e.g., VARCHAR, INT, TIMESTAMP).
- IS_NULLABLE – Indicates whether the column can hold NULL values (YES or NO).
- COLUMN_DEFAULT – The default value assigned to the column, if any.
Example Output
| COLUMN_NAME | DATA_TYPE | IS_NULLABLE | COLUMN_DEFAULT |
|---|---|---|---|
| id | int | NO | NULL |
| username | varchar(50) | NO | NULL |
| varchar(100) | YES | NULL | |
| created_at | timestamp | NO | CURRENT_TIMESTAMP |
This single query gives you a complete snapshot of a table’s structure, which is often enough for most development tasks.
2. MySQL‑Specific Shortcuts
MySQL offers two quick ways to list columns: the SHOW COLUMNS command and the DESCRIBE shortcut.
2.1 SHOW COLUMNS
SHOW COLUMNS FROM your_table;
You can also filter by a specific database:
SHOW COLUMNS FROM your_database.your_table;
Result columns
- Field – Column name.
- Type – Data type and size.
- Null – Whether the column accepts NULL.
- Key – Any key type (e.g., PRI for primary key).
- Default – Default value.
- Extra – Additional information (e.g., auto_increment).
2.2 DESCRIBE (or DESC)
DESCRIBE your_table;
It’s a shorthand that many developers find easier to type, especially when exploring a table interactively.
3. PostgreSQL Techniques
PostgreSQL also provides system catalogs that can be queried directly, but it also includes the information_schema we discussed earlier. Which means in addition, you can use the pg_catalog. pg_attribute view for low‑level introspection It's one of those things that adds up..
3.1 Information Schema Query
SELECT
column_name,
data_type,
is_nullable,
column_default
FROM information_schema.columns
WHERE table_schema = 'public' -- or your schema
AND table_name = 'your_table';
3.2 Direct Catalog Query
SELECT
a.attname AS column_name,
t.typname AS data_type,
a.attnotnull::text AS is_nullable,
ad.adsrc AS column_default
FROM pg_attribute a
JOIN pg_type t ON a.atttypid = t.oid
JOIN pg_class c ON a.attrelid = c.oid
LEFT JOIN pg_attrdef ad ON a.attnum = ad.adnum AND ad.adrelid = c.oid
WHERE c.relname = 'your_table'
AND a.attnum > 0 -- exclude system columns
AND NOT a.attisdropped; -- exclude dropped columns
This query returns the same essential information but pulls it directly from PostgreSQL’s internal catalogs, which can be useful for performance‑critical scripts Small thing, real impact..
4. SQL Server Methods
SQL Server does not expose a universal information_schema like other databases, but you can query the sys.columns and sys.types system views, or use the INFORMATION_SCHEMA views that are available as part of the SQL Server compatibility layer Worth keeping that in mind..
4.1 Using INFORMATION_SCHEMA (Compatibility View)
SELECT
COLUMN_NAME,
DATA_TYPE,
IS_NULLABLE,
COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbo' -- default schema
AND TABLE_NAME = 'your_table';
4.2 Direct System View Query
SELECT
c.name AS column_name,
t.name AS data_type,
c.is_nullable,
c.default_object_id,
def.definition AS column_default
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
LEFT JOIN sys.default_constraints def
ON c.default_object_id = def.object_id
WHERE c.object_id = OBJECT_ID('dbo.your_table');
Notes
OBJECT_ID('dbo.your_table')returns the internal ID of the table.default_object_idbeing 0 indicates no default constraint; you can check this withISNULL(def.definition, 'NULL').
5. Oracle Queries
Oracle’s approach is a bit more involved because it stores metadata in multiple data dictionaries. The most straightforward method is to query ALL_TAB_COLUMNS, which includes columns for objects you have privileges on.
5.1 Basic Query
SELECT
COLUMN_NAME,
DATA_TYPE,
NULLABLE,
DATA_DEFAULT
FROM ALL_TAB_COLUMNS
WHERE OWNER = 'YOUR_SCHEMA'
AND TABLE_NAME = 'YOUR_TABLE';
Field meanings
- NULLABLE – 'Y' if the column can be null, 'N' otherwise.
- DATA_DEFAULT – The default value expression, if any.
5.2 Using DBA_TAB_COLUMNS (if you have DBA rights)
SELECT
COLUMN_NAME,
DATA_TYPE,
NULLABLE,
DATA_DEFAULT
FROM DBA_TAB_COLUMNS
WHERE