Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of columns in a table using SQL? [duplicate]

Tags:

sql

oracle

How to count the number of columns in a table using SQL?

I am using Oracle 11g

Please help. t.

like image 251
tomasz-mer Avatar asked Apr 10 '12 06:04

tomasz-mer


3 Answers

select count(*) 
from user_tab_columns
where table_name='MYTABLE' --use upper case

Instead of uppercase you can use lower function. Ex: select count(*) from user_tab_columns where lower(table_name)='table_name';

like image 94
Vikram Avatar answered Oct 27 '22 01:10

Vikram


Maybe something like this:

SELECT count(*) FROM user_tab_columns WHERE table_name = 'FOO'

this will count number of columns in a the table FOO

You can also just

select count(*) from all_tab_columns where owner='BAR' and table_name='FOO';

where the owner is schema and note that Table Names are upper case

like image 31
Arion Avatar answered Oct 27 '22 01:10

Arion


Old question - but I recently needed this along with the row count... here is a query for both - sorted by row count desc:

SELECT t.owner, 
       t.table_name, 
       t.num_rows, 
       Count(*) 
FROM   all_tables t 
       LEFT JOIN all_tab_columns c 
              ON t.table_name = c.table_name 
WHERE  num_rows IS NOT NULL 
GROUP  BY t.owner, 
          t.table_name, 
          t.num_rows 
ORDER  BY t.num_rows DESC; 
like image 39
Ray K Avatar answered Oct 27 '22 01:10

Ray K