Find the number of 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
Finding the number of columns in a database table can be done by querying the information schema (INFORMATION_SCHEMA.COLUMNS), using system catalog tables, or using database-specific commands like DESCRIBE (MySQL) or \d (PostgreSQL). In Python, pandas provides len(df.columns) or df.shape[1]. The approach depends on whether you are working with SQL databases, CSV files, or in-memory data structures.
SQL: INFORMATION_SCHEMA (Standard)
The INFORMATION_SCHEMA is part of the SQL standard and works across MySQL, PostgreSQL, SQL Server, and other databases:
MySQL
PostgreSQL
SQL Server
SQLite
Oracle
Python: pandas
Python: Database Connectors
Python: CSV Without pandas
Dynamic Column Count in Queries
Common Pitfalls
- Not filtering by TABLE_SCHEMA: If multiple databases have tables with the same name,
WHERE TABLE_NAME = 'users'without a schema filter returns columns from all of them, inflating the count. Always includeTABLE_SCHEMA = DATABASE()(MySQL) orTABLE_SCHEMA = 'public'(PostgreSQL). - Case sensitivity in table names: PostgreSQL stores unquoted identifiers as lowercase. Oracle stores them as uppercase.
WHERE TABLE_NAME = 'Users'fails if the table was created asusers(PostgreSQL) orUSERS(Oracle). Match the case used by your database. - Including dropped columns in PostgreSQL: The
pg_attributecatalog includes columns that wereALTER TABLE DROP COLUMN'd (markedattisdropped = true). Always filter withAND NOT attisdroppedto get the current column count. - Using SELECT * LIMIT 0 for column count: While
SELECT * FROM table LIMIT 0works to get column metadata viacursor.description, some databases still prepare the full query plan. For large tables with complex views, queryingINFORMATION_SCHEMAis faster. - Confusing column count with row count:
INFORMATION_SCHEMA.COLUMNSgives the number of columns (attributes).SELECT COUNT(*) FROM tablegives the number of rows (records). These are fundamentally different dimensions of the table.
Summary
- Use
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table'for a standard SQL approach - Use database-specific catalogs (
pg_attribute,sys.columns,pragma_table_info) for better performance - In Python, use
len(df.columns)ordf.shape[1]with pandas - Use
cursor.descriptionafter executing a query to get column metadata from any Python DB connector - Always filter by schema/database name to avoid counting columns from other schemas
- Match table name case to your database's convention (lowercase for PostgreSQL, uppercase for Oracle)
Related reading
- Find the number of elements greater than x in a given range
- Finding duplicate values in MySQL
- Finding duplicate values in MySQL
- Finding the index of elements based on a condition using python list comprehension
- Finding the reason for DBUpdateException
- firestore PERMISSION_DENIED Missing or insufficient permissions
- First-time database design am I overengineering?
- First Name Variations in a 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.