Q:

Exclude duplicate rows while aggregating one column based on distinct values in another using MySQL

Given the table employees, I need to write a query to obtain total salary for each department. Any duplicate e_id in the table must not be counted twice (in this case Molly). 
The table looks like this:
(Please pardon my way of inserting data table, I am new to posting questions on SO)

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
<table class="tg">
  <tr>
    <th class="tg-0pky">e_id</th>
    <th class="tg-0lax">l_name</th>
    <th class="tg-0lax">f_name</th>
    <th class="tg-0lax">salary</th>
    <th class="tg-0lax">dept_id</th>
  </tr>
  <tr>
    <td class="tg-0lax">12</td>
    <td class="tg-0lax">grace</td>
    <td class="tg-0lax">Barbara</td>
    <td class="tg-0lax">8400</td>
    <td class="tg-0lax">1</td>
  </tr>
  <tr>
    <td class="tg-0lax">34</td>
    <td class="tg-0lax">Gates</td>
    <td class="tg-0lax">David</td>
    <td class="tg-0lax">8000</td>
    <td class="tg-0lax">1</td>
  </tr>
  <tr>
    <td class="tg-0lax">39</td>
    <td class="tg-0lax">Patrick</td>
    <td class="tg-0lax">Neil</td>
    <td class="tg-0lax">7500</td>
    <td class="tg-0lax">1</td>
  </tr>
  <tr>
    <td class="tg-0lax">45</td>
    <td class="tg-0lax">Park</td>
    <td class="tg-0lax">Molly</td>
    <td class="tg-0lax">8100</td>
    <td class="tg-0lax">2</td>
  </tr>
  <tr>
    <td class="tg-0lax">45</td>
    <td class="tg-0lax">Park</td>
    <td class="tg-0lax">Molly</td>
    <td class="tg-0lax">8100</td>
    <td class="tg-0lax">2</td>
  </tr>
  <tr>
    <td class="tg-0lax">75</td>
    <td class="tg-0lax">Gates</td>
    <td class="tg-0lax">Ryan</td>
    <td class="tg-0lax">8300</td>
    <td class="tg-0lax">2</td>
  </tr>
</table>

The output desired is:

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-s268{text-align:left}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
<table class="tg">
  <tr>
    <th class="tg-s268">Uniq_Dept</th>
    <th class="tg-s268">TotalSalary</th>
  </tr>
  <tr>
    <td class="tg-s268">1</td>
    <td class="tg-s268">23900</td>
  </tr>
  <tr>
    <td class="tg-0lax">2</td>
    <td class="tg-0lax">16400</td>
  </tr>
</table>

A:

You can try to use distinct in a subquery to remove duplicates row by dept_id then do SUM
Query #1
SELECT dept_id as Uniq_Dept,SUM(salary) as TotalSalary 
FROM (
  SELECT distinct dept_id, salary
  FROM t 
) t1
GROUP BY dept_id;

| Uniq_Dept | TotalSalary |
| --------- | ----------- |
| 1         | 23900       |
| 2         | 16400       |

View on DB Fiddle

