Home > Blockchain >  SQL : Show the count of multiple tables in one screen
SQL : Show the count of multiple tables in one screen

Time:05-05

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?

CodePudding user response:

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;

CodePudding user response:

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; 
  • Related