MySQL
database
SQL query
column names
tutorial

Get table column names in MySQL?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Getting column names from a MySQL table is a common metadata task. You might need it for schema inspection, dynamic SQL generation, migrations, or tools that build forms and exports automatically. MySQL gives you both quick interactive commands and SQL-standard metadata views, and the right choice depends on whether you want a human-readable summary or queryable schema data.

The Quick Interactive Commands

For manual inspection in the MySQL client, SHOW COLUMNS is the simplest answer.

sql
SHOW COLUMNS FROM customers;

That returns one row per column, including the column name, type, nullability, key information, default value, and any extra flags.

DESCRIBE is essentially the same idea with shorter syntax:

sql
DESCRIBE customers;

These commands are convenient when you are exploring a schema manually, but they are not always the best fit for application code because they return more than just the column names and are less composable than a normal SELECT query.

The Queryable Approach: INFORMATION_SCHEMA.COLUMNS

If you want only column names or want to filter them as part of a query, use INFORMATION_SCHEMA.COLUMNS.

sql
1SELECT COLUMN_NAME
2FROM INFORMATION_SCHEMA.COLUMNS
3WHERE TABLE_SCHEMA = 'app_db'
4  AND TABLE_NAME = 'customers'
5ORDER BY ORDINAL_POSITION;

This is usually the most practical approach for scripts and tooling because:

  • it returns structured metadata
  • you can filter and sort it like any other query
  • it works well in application code

The ORDINAL_POSITION sort matters if you want the columns in table order instead of alphabetical order.

When You Need More Than the Name

Often the real task is not only "give me column names," but "give me the usable schema." In that case, ask for more metadata in the same query.

sql
1SELECT
2    COLUMN_NAME,
3    DATA_TYPE,
4    IS_NULLABLE,
5    COLUMN_DEFAULT
6FROM INFORMATION_SCHEMA.COLUMNS
7WHERE TABLE_SCHEMA = 'app_db'
8  AND TABLE_NAME = 'customers'
9ORDER BY ORDINAL_POSITION;

This is especially useful for admin dashboards, migration checks, ETL jobs, and generic import or export tools.

Using It From Application Code

If you need the column names inside an application, query INFORMATION_SCHEMA from your database client library. Here is a small Python example using mysql-connector-python:

python
1import mysql.connector
2
3conn = mysql.connector.connect(
4    host="localhost",
5    user="app",
6    password="secret",
7    database="app_db",
8)
9
10cursor = conn.cursor()
11cursor.execute(
12    """
13    SELECT COLUMN_NAME
14    FROM INFORMATION_SCHEMA.COLUMNS
15    WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s
16    ORDER BY ORDINAL_POSITION
17    """,
18    ("app_db", "customers"),
19)
20
21for (column_name,) in cursor.fetchall():
22    print(column_name)
23
24cursor.close()
25conn.close()

This is preferable to parsing SHOW COLUMNS output as plain text because it keeps the result structured and parameterized.

SHOW CREATE TABLE Is Different

Another command people reach for is:

sql
SHOW CREATE TABLE customers;

This is useful when you want the full DDL, but it is the wrong tool if you only need column names. It returns the table definition as SQL text, which you then have to parse yourself. That is unnecessary work when the schema metadata is already available in INFORMATION_SCHEMA.

Common Pitfalls

A common mistake is forgetting the schema filter in INFORMATION_SCHEMA.COLUMNS. If several databases contain a table with the same name, your query may return columns from the wrong table or from multiple tables.

Another mistake is assuming the result order is always the table order. Add ORDER BY ORDINAL_POSITION when order matters.

Developers also sometimes parse SHOW CREATE TABLE output to get column names. That is much more fragile than querying metadata directly.

Summary

  • Use SHOW COLUMNS or DESCRIBE for quick manual inspection.
  • Use INFORMATION_SCHEMA.COLUMNS when you need queryable, script-friendly metadata.
  • Filter by both TABLE_SCHEMA and TABLE_NAME so you get the right table.
  • Sort by ORDINAL_POSITION if you want the original column order.
  • Prefer metadata queries over parsing raw DDL text.

Course illustration
Course illustration

All Rights Reserved.