Avatar of Sarma Vadlamani
Sarma Vadlamani
 asked on

sql query

Hi,
I need  a query to total the followng

select count(*) from emp
union
select count((*) from dept

Result
10
5

Expected is the total of both rows that is 15.
can youplease advise. thanks
Oracle Database

Avatar of undefined
Last Comment
slightwv (䄆 Netminder)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
johnsone

I would recommend using UNION ALL instead of UNION.  Gets rid of an unnecessary sort and if the 2 counts are the same, you may get unexpected results.
johnsone

I take back my "may get unexpected results" and replace it with "will get unexpected results".

Test case:
create table emp (id number);
create table dept (id number);
insert into emp values (1);
insert into emp values (2);
insert into emp values (3);
insert into dept values (1);
insert into dept values (2);
insert into dept values (3);
commit;
select sum(mycount) from
(
select count(*) mycount from emp
 union
 select count(*) from dept
);

Open in new window

Result is a count of 3, not 6.  UNION dupes out the second count because they are the same.
Naitik Gamit

you can directly use as,

SELECT  (SELECT COUNT(*) FROM emp) + (SELECT COUNT(*) FROM dept) AS Total_Sum
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
slightwv (䄆 Netminder)

One correction.  In Oracle you need to select FROM something.
SELECT  (SELECT COUNT(*) FROM emp) + (SELECT COUNT(*) FROM dept) AS Total_Sum FROM DUAL;

That said:  Yes you 'can' do it this way but I wouldn't recommend it.