Codegator
Codegator

Reputation: 637

SQL : Show the count of multiple tables in one screen

I want to show the count of multiple tables in one query. Here are the different query I have

SELECT count(*) FROM Table_1;
SELECT count(*) FROM Table_2;
SELECT count(*) FROM Table_3;

Result

Table_Name      Count
Table_1          51
Table_2          75
Table_3          108

How can I achieve it?

Upvotes: 0

Views: 2404

Answers (2)

Shadow
Shadow

Reputation: 21

A select statement on dual with different columns having result of select count(*) of each table would do.

SELECT (SELECT Count(*)
        FROM   table_1) AS count1,
       (SELECT Count(*)
        FROM   table_2) AS count2,
       (SELECT Count(*)
        FROM   table_3) AS count3
FROM   dual; 

Upvotes: 2

Stu
Stu

Reputation: 32614

One simple way would be to union your queries:

SELECT 'Table_1' Table_Name, count(*) "Count" FROM Table_1
union all
SELECT 'Table_2', count(*) FROM Table_2
union all
SELECT 'Table_3', count(*) FROM Table_3;

Upvotes: 3

Related Questions