一、有一張員工表emp,欄位:姓名name,性別sex,部門depart,工資salary,查詢以下資料:
1、查詢男女員工的平均工資
2、查詢各部門的總薪水
3、查詢總薪水排名第二的部門
4、查詢姓名重復的員工資訊
5、查詢各部門薪水大于10000的男性員工的平均薪水
1.select sex,avg(salary) from emp group by sex;
2.select depart,sum(salary) from emp group by depart;
3.select depart,sum(salary) from emp group by depart order by sum(salary) limit 1,1;
4.select name from emp group by name having count(*) > 1;
5.select depart,avg(salary) from emp where sex = '男' and salary > 10000 group by depart;
二、有員工表、部門表和薪資表,根據查詢條件寫出對應的sql
現在有員工表、部門表和薪資表,
部門表depart的欄位有depart_id, name;
員工表 staff 的欄位有 staff_id, name, age, depart_id;
薪資表salary 的欄位有 salary_id,staff_id,salary,month,
(問題a):求每個部門’2016-09’月份的部門薪水總額
select
dep.name,
sum(sal.salary)
from
salary sal
join staff sta on dep.depart_id = sta.depart_id
join salary sal on sta.staff_id = sal.staff_id
where year(sal.month) = 2016 and
month(sal.month) = 9
group by dep.depart_id
(問題b):求每個部門的部門人數,要求輸出部門名稱和人數
select
dep.name,
count(sta.taff_id)
from
staff sta
join depart dep on dep.depart_id = sta.depart_id
group by
sta.depart_id
(問題c):求公司每個部門的月支出薪資數,要求輸出月份和本月薪資總數
select
dep.name,
sal.month,
sum(sal.salary)
from
depart dep
join staff sta on dep.depart_id = sta.depart_id
join salary sal on sta.staff_id = sal.staff_id
group by
dep.depart_id,
sal.month
三、寫出以下資料庫的查詢條件
有兩個表分別如下:
表A(varchar(32) name, int grade)
資料:zhangshan 80, lisi 60, wangwu 84
表B(varchar(32) name, int age)
資料:zhangshan 26, lisi 24, wangwu 26, wutian 26
寫SQL陳述句得到如下查詢結果:
| NAME | GRADE | AGE |
|---|---|---|
| zhangshan | 80 | 26 |
| lisi | 60 | 24 |
| wangwu | 84 | 26 |
| wutian | null | 26 |
select B.name,grade,age from A right join B on A.NAME = B.NAME;
三、撰寫一個SQL查詢,獲取Employee表中第二高的薪水(Salary)
±----±------+
| Id | Salary|
±----±------+
| 1 | 100 |
±----±------+
| 2 | 200 |
±----±------+
| 3 | 300 |
±----±------+
例如上述Employee表,SQL查詢應該回傳200作為第二高的薪水,如果不存在第二高的薪水,那么查詢應該回傳null,
±--------------------+
| SecondHighestSalary |
±--------------------+
| 200 |
±--------------------+
#Write your MySQL query statement below
select (select distinct Salary from Employee order by Salary DESC limit 1,1) as SecondHighestSalary ;
四、已知T1和T2的欄位定義完全相同,T1有5條不同資料,T2有5條不同資料,其中T1有2條資料存在表T2中,使用UNION陳述句查詢這些資料,要求重復資料不出現
SELECT * FROM T1
UNION
SELECT * FROM T2
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/220890.html
標籤:python
