I have an EMPLOYEE table below:
EMP_ID DEPT_ID
101 1
102 2
103 3
104 1
And a DEPARTMENT table as:
DEPT_ID COUNTS
1
2
3
I want to write a query which would count the number of Employee that belong to a department and store it into Department column table so the Department table will look like:
DEPT_ID COUNTS
1 2
2 1
3 1
The solution is
update department p
set counts = (select count(*) from EMPLOYEE e where p.dept_id = e.dept_id);
But i really dont understand how it works internally How does it know which dept ids in DEPARTMENT it has to set counts to. what exactly does this subquery return "select count(*) from EMPLOYEE e where p.dept_id = e.dept_id"