Write a query to get all column names of a database table

Description: Get the names of all columns of the students table.

Tables: the exercises use the students (id, name, city, marks, course_id) and courses (id, course_name) tables, with the data from our MySQL INSERT chapter. The students are: Robin (Delhi, 85), Maria (Mumbai, 92), John (Pune, 78), Asha (Jaipur, 88), Vikram (Delhi, 65), Neha (Mumbai, 45).

Hint:

  • Use SHOW COLUMNS FROM.
  • Or read the information_schema.columns table.

MySQL query to get the column names

-- way 1
SHOW COLUMNS FROM students;

-- way 2: only the names
SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'school' AND TABLE_NAME = 'students'
ORDER BY ORDINAL_POSITION;

Output:

Tutorials Class - Output Window
COLUMN_NAME
id
name
city
marks
course_id

How it works

  • information_schema is a database that has information about all other databases.
  • Change 'school' to the name of your database.

Read MySQL CREATE TABLE (the DESCRIBE command shows the same details).


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.