How can I get column names from a table in SQL Server?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In SQL Server, column names are available from the system catalog and from standard metadata views. The best query depends on whether you want a portable standards-style answer, a SQL Server-specific answer with more detail, or a quick interactive lookup.
Use INFORMATION_SCHEMA.COLUMNS for a Standard Query
The simplest portable approach is INFORMATION_SCHEMA.COLUMNS.
This is a good default when you want readable metadata access and do not need many SQL Server-specific features.
Two important filters are:
- '
TABLE_SCHEMA, because table names are not unique across schemas' - '
TABLE_NAME, because the view contains every table in the database'
Ordering by ORDINAL_POSITION gives the columns in table order.
Use sys.columns for SQL Server-Specific Metadata
If you want the SQL Server catalog directly, use sys.columns with OBJECT_ID.
This is often more useful than INFORMATION_SCHEMA when you want to join into other SQL Server catalog views later.
For example, the same family of catalog views can also tell you about data types, defaults, computed columns, and identity properties.
Include More Metadata When Needed
Often you do not want only the column names. You want names plus types and nullability.
This is a better query for tooling, migrations, or documentation generation because it gives enough detail to be useful beyond a simple list.
sp_columns Works for Interactive Inspection
SQL Server also provides a stored procedure for metadata lookup.
This is fine for quick manual inspection in SSMS, but for application code or repeatable scripts, direct queries against the metadata views are usually cleaner and easier to control.
Build a Reusable Query for Any Table
If the table name needs to be parameterized, use a variable and schema-qualified name.
This keeps the lookup precise and avoids relying on ambiguous table names.
When Dynamic SQL or Tooling Needs This
Common use cases include:
- generating exports
- building dynamic insert or select statements
- creating documentation
- verifying schema drift in deployment tools
- auditing tables for expected columns
In these cases, sys.columns is often the better foundation because it integrates naturally with the rest of SQL Server's system catalog.
INFORMATION_SCHEMA Versus sys.*
A useful rule is:
- choose
INFORMATION_SCHEMAwhen you want a simple standards-style answer - choose
sys.columnswhen you want SQL Server-native detail or richer joins
Neither is universally “more correct.” They are different tools for slightly different needs.
Permissions and Context Matter
Metadata visibility depends on permissions. If a query returns less than expected, it may not mean the table is missing. It may mean the current login cannot see that object's metadata fully.
Also remember that the query runs in the current database context. If you are pointed at the wrong database, even a perfectly written metadata query returns the wrong answer.
Common Pitfalls
A common mistake is filtering only by table name and ignoring schema. That breaks in databases where the same table name exists in multiple schemas.
Another mistake is assuming INFORMATION_SCHEMA exposes every SQL Server-specific detail you might need. It often does not.
Developers also sometimes forget to order by position, which makes the result harder to compare with the real table definition.
Finally, do not assume an empty result means the table definitely does not exist. Check database context and permissions too.
Summary
- Use
INFORMATION_SCHEMA.COLUMNSfor a simple standards-style column list. - Use
sys.columnswhen you want SQL Server-native metadata and richer detail. - Filter by both schema and table name.
- Order by ordinal or column ID if you want real table order.
- For application and tooling scenarios, metadata queries are usually better than manual SSMS inspection.
Related reading
- How can I get the size of a MySQL database?
- How can I get the size of a MySQL database?
- How can I get the sizes of the tables of a MySQL database?
- How can I get the sizes of the tables of a MySQL database?
- How can I get the SQL of a PreparedStatement?
- How can I get the total number of items in a DynamoDB table?
- How can I implement two sort keys in Dynamo DB?
- How can I implement versioning without replacing with previous record in DynamoDB?

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.