Can you not just do a COUNT(DISTINCT .... )? Like this:-
SELECT COUNT(DISTINCT emp_dept)
FROM employee
NATURAL JOIN trial
Pergunta
I have two Tables:
Employee
emp_id, emp_name, emp_dept, emp_sal
1 A A 1
2 B B 2
3 C C 3
4 D D 4
Trial
emp_id, random
1 X
1 Y
2 Z
3 A
4 B
If I were to perform a Natural join of Employee and Trial,
I would get the result:
emp_id, emp_name, emp_dept, emp_sal random
1 A A 1 X
1 A A 1 Y
2 B B 2 Z
3 C C 3 A
4 D D 4 B
If I wanted to perform the aforementioned Natural Join and count the no of depts in the organization in a single query, how would I go about doing that?
select count(emp_dept) from (select distinct(emp_dept) from employee natural join trial) as T2;
works, but I am wondering if there's another way to do the above?
Note:
Natural join is mandatory!
I tried both the following codes and failed miserably. :P
select count(emp_dept) from (select (emp_dept) from employee natural join trial) as T2;
gave me 5, when the actual answer is 4.
select count(select distinct(emp_dept) as emp_date from (select * from employee natural join trial) as T2) from T2;
select count(select distinct(emp_dept) as emp_date from (select * from employee natural join trial) as T2) from (select * from employee natural join trial) as T3);
^ What did I do wrong in the 3 lines of code above?
Thanks in advance! :)
Solução
Can you not just do a COUNT(DISTINCT .... )? Like this:-
SELECT COUNT(DISTINCT emp_dept)
FROM employee
NATURAL JOIN trial