SQL Query to Get Column Names From a Table
Last Updated :
10 Oct, 2021
SQL stands for Structured Query Language. It is a language used to interact with the database, i.e to create a database, to create a table in the database, to retrieve data or update a table in the database, etc. SQL is an ANSI(American National Standards Institute) standard. Using SQL, we can do many things. For example – we can execute queries, we can insert records into a table, we can update records, we can create a database, we can create a table, we can delete a table, etc.
In this article, we will look at how to get column names from a table.
Step 1: Creating Database
We are creating the database using CREATE query.
Query:
CREATE DATABASE Test
Output:

The command is completed successfully. It means the Database named Test is created. The next step is to create a table.
Step 2: Creating table
The Data table will have three fields FirstName, LastName, and Age. Using the below query we will be creating a table.
Query:
CREATE TABLE Data(FirstName varchar(40),
LastName varchar(30),Age int, );
Output:

The Data table is created in the database.
Step 3: Insert Data into the Table
Using the below query we will be adding the data to our table.
Query:
INSERT INTO Data
VALUES ('Rahul','Sharma',15),
('Soha','Shaikh',24),
('Vivek','Rao',18),
('Sonali ','Rane',20);
Output:

We have added the data to our table. We can verify the data in the table using the SELECT query as below.
Step 4: View Table Data
Query:
SELECT * FROM Data
Output:

Step 5: Getting column names from the table
We will be using sys. columns to get the column names in a table. It is a system table and used for maintaining column information. It contains the following information about columns:
- Name - Name of the column.
- Object_id - ID of the object for the table in which column belongs.
- Column_id - ID of the column.
- user_type_id - ID of the user-defined column type.
- max_length - Maximum length of the column (In bytes).
- is_nullable - 1=Column is nullable.
Query:
SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('Data')
Output:
Similar Reads
How to get column names from SQLAlchemy? In this article, we will discuss how to get column names using SQLAlchemy in Python. SQLAlchemy is an open-source SQL toolkit and object-relational mapper for the Python programming language released under the MIT License. It gives full power and flexibility of SQL to an application. To follow along
3 min read
SQL Query to Find the Number of Columns in a Table SQL stands for a structure query language, which is used in the database to retrieve data, update and modify data in relational databases like MySql, Oracle, etc. And a query is a question or request for data from the database, that is if we ask someone any question then the question is the query. S
4 min read
Get column names from PostgreSQL table using Psycopg2 This article is an illustration of how to extract column names from PostgreSQL table using psycopg2 and Python. Used table for demonstration: Example 1: First, we connect the PostgreSQL database using psycopg2.connect()Â method, then we create a cursor using cursor() method, after that we use the cu
1 min read
How to Search For Column Names in SQL? In SQL, sometimes we need to search the column names in a table using the prefixes. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks. Query: CREATE DATABA
2 min read
SQL Server ALTER TABLE DROP COLUMN In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In
4 min read