SQL Query to Find the Number of Columns in a Table
Last Updated :
09 Aug, 2021
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. Similarly when we want any data from the database then we write the query in SQL to get that data. In this article, we are talking about how we can find the number of columns present in a table.
Creating database :
To create a database there is a query we need to use in the SQL platform, like MySql, Oracle, etc. The query is,
create database database_name;
For example,
create database GeeksforGeeks;

Output :
Commands completed successfully
Using the database :
To use the database there is a query we need to use in the SQL portal, like MySql, Oracle, etc. The query is,
use database_name;
Here the query will be,
use GeeksforGeeks;

Output :
Commands completed successfully
Add tables in the database :
To create tables in a database there is a query we need to use in the SQL platform, like MySql, Oracle, etc. The query is,
create table table_name(
column1 type(size),
column2 type(size),
.
.
.
columnN type(size)
);
For example,
create table GeeksforGeeks(
course_ID INT,
course_name VARCHAR(50),
course_mentor VARCHAR(30),
course_fee INT,
course_start_date DATE
course_enrolled_student INT
);
Here, the table has 6 columns in it.

See the table :
To see the table use the ‘DESC table_name‘ query.
here the query is,
desc geeksforgeeks;
If we use a Microsoft SQL server then we need to use ‘EXEC sp_help’ in place of DESC. In the Microsoft SQL server, the DESC command is not an SQL command, it is used in Oracle.
EXEC sp_help GFG_salary;

Output :
SL No | Column_name | Type | Computed | Length
----------------------------------------------------------------------------------
1. | course_ID | int | no | 4
2. | course_name | varchar | no | 50
3. | course_mentor | varchar | no | 30
4. | course_fee | int | no | 4
5. | course_start_date | date | no | 3
6. | course_enrolled_student | int | no | 4
----------------------------------------------------------------------------------
Add value into the table :
To add value to the table there is a query we need to use in the SQL platform, like MySql, Oracle, etc. The query is,
insert into table_name(
value1,
value2,
value3
.
.
.
valueN);
For example, here the query will be,
INSERT INTO `geeksforgeeks` (`course_ID`, `course_name`, `course_mentor`, `course_fee`, `course_start_date`, `course_enrolled_student`) VALUES
(1, 'SQL', 'mentor1', '3000', '2021-03-02', '10'),
(2, 'JAVA', 'mentor2a', '5000', '2021-03-02', '12'),
(3, 'DSA', 'mentor3', '4500', '2021-03-02', '25'),
(4, 'Python', 'mentor4', '3500', '2021-03-02', '20');

Output :
4 rows affected
Data present in the table after insertion :
select * from geeksforgeeks;

Output :
| course_ID | course_name | course_mentor | course_fee | course_start_date | course_enrolled_student
----------------------------------------------------------------------------------------------------------
1. | 1 | SQL | mentor1 | 3000 | 2021-03-02 | 10
2. | 2 | JAVA | mentor2a | 5000 | 2021-03-02 | 12
3. | 3 | DSA | mentor3 | 4500 | 2021-03-02 | 25
4. | 4 | Python | mentor4 | 3500 | 2021-03-02 | 20
----------------------------------------------------------------------------------------------------------
Now we have to find the number of columns present in the table,
To find that we can use a simple function COUNT() with INFORMATION_SCHEMA view in a relational database. This INFORMATION_SCHEMA is an ANSI standard set of views that provides read-only access to the details of databases and their objects like tables, constraints, procedures, etc. See the below query
SELECT count(*) as No_of_Column FROM information_schema.columns WHERE table_name ='geeksforgeeks';
Here, COUNT(*) counts the number of columns returned by the INFORMATION_SCHEMA .columns one by one and provides the final count of the columns. Here table_name selects the table in which we wish to work.

Output :
| No_of_Column
-------------------
1. | 6
-------------------
So here, the final output will be 6, because in the table “geeksforgeeks” there are 6 columns present.
Similar Reads
SQL Query to Count the Number of Rows in a Table
Counting rows in a database table is a fundamental operation in SQL that helps developers and analysts understand the size and structure of their datasets. Whether we're building reports, analyzing trends, or debugging data inconsistencies, the COUNT() function in SQL is an essential tool to streaml
4 min read
SQL Query to Get Column Names From a Table
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 ma
2 min read
SQL Query to Find the Sum of all Values in a Column
In SQL, calculating the sum of values in a column is a crucial task for performing data analysis and generating reports. The SUM() function helps to calculate the total sum of numeric values from a column, which is especially useful in scenarios like finding total sales, total employees, or total re
5 min read
SQL Query to Find Unique Column Values From Table
Finding unique column values is a common task in SQL when analysing data or ensuring data integrity. By using the DISTINCT clause, we can efficiently retrieve unique values from a specified column, avoiding duplicate results. In this article, we will explain the use of the DISTINCT clause with detai
3 min read
How to Find the Maximum of Multiple Columns in SQLite?
SQLite is a serverless architecture that does not require any server to perform operations and queries. It is widely used in embedded systems, mobile applications, and small-scale web applications because of its simplicity, efficiency, and portability. SQLite supports most of the standard SQL featur
4 min read
How to Find the Missing Number in SQL Column?
Given a column in the table having values from 1 to N, one value will be missed. The task is to find the missing number. So, let's start by creating a database first. Step 1 : Create Database. Query : CREATE DATABASE GFG Step 2 : Use the GFG Database. Query : USE GFG Step 3 : Create a table Create a
1 min read
SQL Query to Find the Average Value in a Column
In this article, we are going to see how to find the average value in a column in SQL. A column in the SQL table is the vertical catalog structure. In this article, we will be using the Microsoft SQL Server as our database. For the purpose of example, we will be creating a sample table and performin
3 min read
How to Find the Maximum of Multiple Columns in SQL Server?
When working with SQL Server databases, there are times when we need to find the maximum value among multiple columns. This task can be accomplished using various techniques within SQL queries. By using functions like CASE and GREATEST, SQL Server provides efficient ways to determine the maximum val
4 min read
SQL Query to Find Shortest & Longest String From a Column of a Table
Here, we are going to see how to find the shortest and longest string from a column of a table in a database with the help of SQL queries. We will first create a database "geeks", then we create a table "friends" with "firstName", "lastName", "age" columns. Then will perform our SQL query on this ta
3 min read
How to Count the Number of Rows of a Given SQLite Table using Python?
In this article, we will discuss how we can count the number of rows of a given SQLite Table using Python. We will be using the cursor_obj.fetchall() method to do the same. This method fetches all the rows of a query result. It returns all the rows as a list of tuples. An empty list is returned if t
2 min read