Get Column Names From Table in SQL

How to write a query to Get Column Names From Table in SQL Server is one of the standard Interview Questions you might face. For this Get Column Names From Table example, We are going to use the below shown data

Get Column Names From Table in SQL Server 0

The above screenshot will show you the data inside the NewCustomer table present in the SQL Tutorial database.

Get Column Names From Table in SQL Server Example 1

In this SQL example, we will show you how to Get Column names using INFORMATION_SCHEMA.

-- Query to Get Column Names From Table in SQL Server
USE [SQL Tutorial]
GO
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'NewCustomers'

OUTPUT

Get Column Names From Table in SQL Server 1

You can use the below query to get all the information about the Table

-- Query to Get Column Names From Table in SQL Server
USE [SQL Tutorial]
GO
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'NewCustomers'
Get Column Names From Table in SQL Server 2

Get Column Names From Table Example 2

This Frequently asked Questions explains how to find the list of Column names in a Table using sys.columns.

-- Query to Get Column Names From Table in SQL Server
USE [SQL Tutorial]
GO
SELECT name
FROM sys.columns 
WHERE OBJECT_ID = OBJECT_ID('NewCustomers')
Get Column Names From Table in SQL Server 3

Let me show you what will happen if we replace the name column with *

-- Query for Sql Server Get Column Names
USE [SQL Tutorial]
GO
SELECT *
FROM sys.columns 
WHERE OBJECT_ID = OBJECT_ID('NewCustomers')
Get Column Names From Table in SQL Server 4