title: Calculating relation (employee's salary)/(deparment's salary contribution) for each employee
body: <p>I'm beginning with SQL and I'm stuck with the following exercise:</p>
  
  <blockquote>
    <p>Calculate for each employee <em>the percent</em> that his wage (salary +
    commission) represents from the total contribution salary of the department
    he works in.</p>
  </blockquote>
  
  <p>My scheme looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/TMv0L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TMv0L.png" alt="enter image description here"></a></p>
  
  <p>My attempt was this:</p>
  
  <pre><code>SELECT employee.last_name, temp1.department_id AS department, wage/deptwage*100 AS "%wage/deptwage"
  FROM employee, 
  (SELECT department_id, SUM(salary+COALESCE(commission, 0)) AS deptwage
  FROM employee
  GROUP BY department_id ) temp1,
  (SELECT last_name, (salary+COALESCE(commission, 0)) AS wage
  FROM employee ) temp2
  WHERE temp1.department_id=employee.department_id
  </code></pre>
  
  <p>After executing the query I get basically this, with 1024 rows:</p>
  
  <p><a href="https://i.stack.imgur.com/VmhUY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VmhUY.png" alt="enter image description here"></a></p>
  
  <p>1024 came from 32*32: It's executing wage/deptwage*100 for each employee 32 times (which is the total number of employees).</p>
  
  <p>I've tried without success to write a new script, so I'd really appreciate any help with this.</p>