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
MySQL Data Types
MySQL CREATE DATABASE
MySQL USE and SHOW DATABASES
MySQL DROP DATABASE
MySQL CREATE TABLE
MySQL Constraints
MySQL DROP TABLE
Exercises & Assignments
Write a query to get all column names of a database table
Write a query to create a new table with the data and structure of another table
Interview Questions & Answers
Write a query to create a new table which consists of data and structure copied from the other table
What is a primary key in MySQL?
What is a foreign key in MySQL?
What is the difference between CHAR and VARCHAR in MySQL?
What is normalization in MySQL?
What is AUTO_INCREMENT in MySQL?
What is the difference between primary key and unique key?