sql语句练习题目
员工信息表 staff: user_id , name, store_id, salary
商店表store:store_id, name,city
题目1:
找出平均工资小于5000的商店所在的城市
SELECT a.ctity,AVG(b.salary) FROM store a,staff b
WHERE a.store_id=b.store_id
GROUP BY b.store_id
HAVING AVG(b.`salary`)<5000
题目2 查找工资最低的人的名字,商店名字,商店所在城市
SELECT a.name,b.name,b.ctity,MIN(salary) FROM staff a,store b
题目3查找每个店的店名、平均工资、城市
分析: 店名 城市在 store 表中, 工资在 staff表中, 那么需要连接查询, 店的平均工资, 那么需要group by 店id,
SELECT b.name,b.ctity,avg(salary) from staff a,store b
where a.user_id=b.store_id
group by b.store_id
题目4 查询每个店的店名、平均工资、城市,且降序排列
SELECT b.name,b.ctity,avg(salary) from staff a,store b
where a.user_id=b.=store_id
group by b.store_id
order by avg(salary) desc
题目5、查询重名的员工名字和重复次数。
SELECT a.name,COUNT(*) AS 重复次数 FROM staff a
GROUP BY a.name
HAVING COUNT(*)>1
ORDER BY COUNT(*) DESC