How to get Column Count in SQL

How to get Column Count in SQL

Getting the column count in SQL is useful for retrieving insights into tables when a table may contain large or complex data, especially when dealing with unfamiliar databases. This can be achieved by using the COUNT function. This blog will explain all the related concepts and SQL queries to get the column count in SQL.

Table of Contents: 

SQL’s INFORMATION_SCHEMA and Column Counts

In SQL, tables are structured data collections where rows are commonly referred to as records or tuples, which define the single entry or instance of data, and columns are commonly referred to as fields or attributes, which define the type of information stored in that column for each record. Getting the column count can be useful for those who need to manipulate database schemas efficiently. 

In SQL, retrieving the column count can be done with the help of system views (an ANSI standard) like INFORMATION_SCHEMA.COLUMNS and database-specific metadata tables, which are provided by various relational database management systems (such as MySQL, SQL Server, and PostgreSQL) that access the information of data (metadata) about the columns of tables within a database.

The COUNT() function in SQL is an aggregate function, that returns the number of rows that match a specific condition. 

Example

In this example, the count() function will be used with the system views(INFORMATION_SCHEMA.columns) to get the number of columns.

SELECT count(*) AS No_of_Column 

FROM information_schema.columns 

WHERE table_name = 'Employee';

The above SQL query results in the total count of columns with the help of count() function in the table named “Employee”, which will fetch metadata about the table structure by querying the INFORMATION_SCHEMA.COLUMNS system view.

Conclusion

The system views such as INFORMATION_SCHEMA.COLUMNS or database-specific methods, can be utilized with the count() function to retrieve the number of columns in a table. This metadata is important for schema exploration, data validation, automation, and performance optimization. With this, developers and database administrators can understand the table structures better which ensures consistency, and streamline dynamic query generation or reporting tasks.

FAQs

Q1. How do you get the total column count in SQL?

Use the system view(like  INFORMATION_SCHEMA.COLUMNS) with the count() function to get the total column count in SQL.

Q2. Why do we need to use the count() Function with INFORMATION_SCHEMA.COLUMNS View in SQL?

To determine the total number of columns in a table or across multiple tables in a database.

Q3. Is it possible to use the INFORMATION_SCHEMA.COLUMNS view with all other relational databases?

Yes, the  INFORMATION_SCHEMA.COLUMNS view is supported by most of the relational databases (e.g., MySQL, SQL Server, PostgreSQL, Oracle). Although, some DB(eg SQLite) require different methods to implement, such as PRAGMA table_info() function.

Q4. Can we use the COUNT(*) method to include the columns having the NULL values?

Yes, the COUNT(*) function will count all columns in the table, having those that may contain NULL values.

Q5. Is it possible to automate retrieving the column count in a script?

Yes, embed the column count query within a stored procedure, utilize a dynamic SQL script, or employ programming languages like Python or Java along with SQL libraries to execute it programmatically.

About the Author

Data Engineer

As a skilled Data Engineer, Sahil excels in SQL, NoSQL databases, Business Intelligence, and database management. He has contributed immensely to projects at companies like Bajaj and Tata. With a strong expertise in data engineering, he has architected numerous solutions for data pipelines, analytics, and software integration, driving insights and innovation.

business intelligence professional