It is possible to find the number of rows in a table:
select count(*) from tablename
Is it possible to find the number of columns in a tabl
Well I tried Nathan Koop's answer and it didn't work for me. I changed it to the following and it did work:
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table_name'
It also didn't work if I put USE 'database_name'
nor WHERE table_catalog = 'database_name' AND table_name' = 'table_name'
. I actually will be happy to know why.
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'table_name';
Following query finds how columns in table:-
SELECT COUNT(COLUMN_NAME) FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = 'TableName';
SELECT TABLE_SCHEMA
, TABLE_NAME
, number = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_SCHEMA, TABLE_NAME;
This one worked for me.
Since all answers are using COUNT()
, you can also use MAX()
to get the number of columns in a specific table as
SELECT MAX(ORDINAL_POSITION) NumberOfColumnsInTable
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'YourDatabaseNameHere'
AND
TABLE_SCHEMA = 'YourSchemaNameHere'
AND
TABLE_NAME = 'YourTableNameHere';
See The INFORMATION_SCHEMA COLUMNS Table
SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'Your_table_name';
Note: Your_table_name should be replaced by your actual table name