2017-02-25 5 views
0

少なくとも1人の従業員を雇っているテーブル内のすべての部門、採用する人数、各部門の給与の合計を含むテーブルを返すSQLクエリを作成したい、部門IDテーブルを返すためのSQLクエリの作成

部門が発注:

dept_id | dept_name | dept_location 
10  | Accounts | Delhi 
20  | Marketing | Delhi 
40  | IT  | Warsaw 
30  | production| Hyderabad 
5O  | Sales  | Bengaluru 

従業員:

emp_id | emp_name | dept_id | salary 
1  | Jojo  | 20  | 5000 
2  | Popat Lal | 30  | 15000 
3  | Santa Singh| 40  | 25000 
4  | Banta Singh| 20  | 7500 
5  | Soban Lal | 20  | 15000 
6  | Kk  | 10  | 12000 
7  | Bob  | 20  | 35000 
8  | John  | 30  | 25000 
9  | Smith  | 40  | 5000 

クエリが返す必要があります:

dept_id | count  | sum_of_salary 
10  | 1   | 12000 
20  | 4   | 62500 
30  | 2   | 40000 
40  | 2   | 30000 
+1

ヒント: 'GROUP BY'。 「JOIN」は必要ない。 –

答えて

2

これはgroup byだけで簡単です:

select dept_id, 
    count(*) cnt, 
    sum(salary) sum_of_salary 
from employee 
group by dept_id 
order by dept_id; 
1
select d.dept_id, d.dept_name, count(*) total_hires, sum(salary) total_salary 
from employee e join department d on e.dept_id = d.dept_id 
group by d.dept_id, d.dept_name 
+1

元の質問に正確な出力を得るために、参加する必要はありません。 –

+0

は同意しますが、彼はevetnually部門の名前が欲しい –

関連する問題