
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to select the table with the greatest number of columns in MySQL?
You can use INFORMATION_SCHEMA.COLUMNS to get the table with the greatest number of columns.
The syntax is as follows −
SELECT TABLE_NAME, COUNT(*) AS anyAliasName FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY yourAliasName DESC LIMIT 1;
Following is the query to select the table that has the greatest number of columns. We are getting this result because we have set the count to DESC and used GROUP BY TABLE_NAME −
mysql> SELECT TABLE_NAME, COUNT(*) as TOTAL_COUNT FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY TOTAL_COUNT DESC LIMIT 1;
This will produce the following output −
+-----------------------------------+-------------+ | TABLE_NAME | TOTAL_COUNT | +-----------------------------------+-------------+ | table_lock_waits_summary_by_table | 68 | +-----------------------------------+-------------+ 1 row in set (0.12 sec)
You can also get the least number of columns as well using the below query. We are getting this result because we have set the count to ASC and used GROUP BY TABLE_NAME −
mysql> SELECT TABLE_NAME, COUNT(*) as TOTAL_COUNT FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_NAME ORDER BY TOTAL_COUNT ASC LIMIT 1;
This will produce the following output −
+-----------------------+-------------+ | TABLE_NAME | TOTAL_COUNT | +-----------------------+-------------+ | removenullrecordsdemo | 1 | +-----------------------+-------------+ 1 row in set (0.14 sec)
- Related Articles
- How to find the number of columns in a MySQL table?
- Count the number of columns in a MySQL table with Java
- How to get the greatest of two columns values in MySQL?
- Get the number of columns in a MySQL table?
- How to know the exact number of table and columns in a MySQL database?
- How to get the datatype of MySQL table columns?
- MySQL query to select all the records only from a specific column of a table with multiple columns
- How to display MySQL Table Name with columns?
- MySQL procedure with SELECT to return the entire table
- How to count number of columns in a table with jQuery
- Implement GREATEST() in MySQL and update the table?
- How to select the last three rows of a table in ascending order with MySQL?
- How to create conditions in a MySQL table with multiple columns?
- Counting the number of non-null or nonzero columns in a MySQL table?
- How do I SELECT none of the rows and columns in MySQL?

Advertisements