How do I list all the columns in a table?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Listing every column in a table is a routine step during migrations, debugging, and query generation. The challenge is not the concept, but writing metadata queries that are accurate across schemas and database engines. A reliable approach is to start with INFORMATION_SCHEMA, then switch to engine-specific catalogs only when you need extra detail.
Start with INFORMATION_SCHEMA.COLUMNS
For most SQL engines, the portable baseline is INFORMATION_SCHEMA.COLUMNS. It provides column names, data types, nullability, defaults, and ordinal order.
Two details matter:
- Always filter by schema and table.
- Always sort by
ordinal_position.
Without schema filtering, you can accidentally read metadata from a similarly named table in another schema.
MySQL and MariaDB Patterns
MySQL supports both quick and detailed options.
Quick interactive view:
Script-friendly detailed query:
Use SHOW COLUMNS for manual inspection and INFORMATION_SCHEMA for automation, reporting, and environment comparison.
PostgreSQL Patterns
PostgreSQL also supports INFORMATION_SCHEMA, and for advanced tooling you can query system catalogs.
Portable version:
Catalog version with richer type rendering:
Use catalogs when you need precise PostgreSQL behavior such as generated columns or internal type formatting.
SQL Server Patterns
SQL Server supports INFORMATION_SCHEMA.COLUMNS and sys.columns.
INFORMATION_SCHEMA option:
System catalog option:
System catalogs are better when you need engine-specific attributes for migration tooling.
Oracle Pattern
Oracle uses uppercase identifiers by default unless objects were created with quoted names.
If you query with lowercase identifiers in Oracle metadata views, you can get empty results even when table exists.
Programmatic Introspection Example
For automation, parameterized queries prevent schema-name injection and keep scripts reusable.
This script runs as-is when database credentials are valid and table exists.
Practical Validation Checklist
When metadata output looks wrong, verify these in order:
- schema name is correct
- identifier case matches engine behavior
- account has metadata permissions
- query orders by ordinal position
- table is not a synonym or view when you expected base table
This checklist resolves most false alarms quickly.
Common Pitfalls
A common pitfall is querying by table name alone and getting columns from the wrong schema. Another is assuming case handling is the same in PostgreSQL, MySQL, SQL Server, and Oracle. Teams also forget ORDER BY ordinal_position, producing unstable column ordering in generated documentation. In automation, unparameterized metadata queries create avoidable safety and correctness issues. Finally, developers sometimes jump to engine-specific catalogs prematurely when a portable INFORMATION_SCHEMA query would be clearer and easier to maintain.
Summary
- Use
INFORMATION_SCHEMA.COLUMNSfirst for portable column listing. - Filter by both schema and table, then sort by ordinal position.
- Use engine-specific catalogs only when you need extra metadata detail.
- Parameterize metadata queries in scripts for reliability and safety.
- Validate schema name, case, and permissions before assuming table metadata is missing.
Related reading
- How do I modify a MySQL column to allow NULL?
- How do I obtain a list of all schemas in a Sql Server database
- How do I put an 'if clause' in an SQL string?
- How do I query between two dates using MySQL?
- How do I loop through a list by twos?
- How do I make a exact duplicate copy of an array?
- How do I query by only part of a composite key in DynamoDB?
- How do I remove a MySQL database?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.