Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone tell me how to get column count in SQL?

2 Answers

0 votes
by (119k points)

Here is the SQL query to count the number of columns in Employee table:

SELECT count (column_name) as Number 

FROM information_schema.columns 

WHERE table_name='Employee' 

Here is the SQL query to get the list of all the columns of Employee table:

SELECT column_name,table_name as Number 

FROM information_schema.columns

WHERE table_name = ‘Employee’

Here is the SQL query to get the list of all the columns of tables in the database:

SELECT column_name,table_name as Number 

FROM information_schema.columns

If you want to learn SQL and writing SQL queries, then check out this SQL Course by Intellipaat that provides Instructor-led training, certification, and also job assistance.

0 votes
ago by (3.1k points)

Some SQL commands, including ‘CREATE’ or ‘DROP’, cannot be executed without knowledge of the database architectural design of its tables. Therefore, to determine the count of columns in a specific table one should check the INFORMATION_SCHEMA.COLUMNS view and/or the sys.columns catalog in SQL Server. These two processes give you the ability to limit the output to the required table and specify the total number of columns in it. This comes in handy when the designer has to be concerned about the already existing tables within the system and their specific structures.

SELECT COUNT(*) AS ColumnCount

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = 'your_table_name';

SELECT COUNT(*) AS ColumnCount

FROM sys.columns

WHERE object_id = OBJECT_ID('your_table_name');

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 6, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked May 5, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked May 3, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Apr 22, 2020 in SQL by Sudhir_1997 (55.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...