'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?
Solution 1:[1]
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;
Solution 2:[2]
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stu |
| Solution 2 | Shadow |
