Java笔试题之SQL语句(单表)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java笔试题之SQL语句(单表)相关的知识,希望对你有一定的参考价值。
题目:系统中有一个表WCEmploy(职工号,姓名,部门名,工种,工资)
一.请写出建表语句
1 create table WCEmploy( 2 id int AUTO_INCREMENT PRIMARY KEY, 3 `name` char(8) not null, 4 department_name char(16), 5 type char(8), 6 salary double 7 )
二.插入数据
1 insert into wcemploy values(null,‘张三‘,‘车间一‘,‘钳工‘,6000) 2 insert into wcemploy values(null,‘李四‘,‘车间一‘,‘电工‘,8000) 3 insert into wcemploy values(null,‘王五‘,‘车间二‘,‘车间主任‘,10000)
1 insert into wcemploy values(null,‘泰日天‘,‘车间一‘,‘钳工‘,6000), 2 (null,‘风高放火‘,‘车间一‘,‘电工‘,8000), 3 (null,‘沃尔沃‘,‘车间二‘,‘车间主任‘,10000), 4 (null,‘CVBS‘,‘车间二‘,‘钳工‘,2000), 5 (null,‘单个人‘,‘车间三‘,‘钳工‘,4500), 6 (null,‘人头狗‘,‘车间二‘,‘钳工‘,6000), 7 (null,‘后宫番‘,‘车间一‘,‘钳工‘,1500)
多条数据插入
在插入数据的时候,发生了一个错误,插入汉字数据报错
修改mysql的数据库的数据库属性字符集为
三.查询语句
1.请用一个SQL语句查询每个部门的总人数
select department_name ,COUNT(DISTINCT id) from wcemploy GROUP BY department_name
2.请用一个SQL语句查询出不同部门的担任“钳工”的职工平均工资
select department_name ,AVG(salary) from wcemploy where type=‘钳工‘ GROUP BY department_name//如果没有分组,就会出现错误
3.请用一个SQL语句查询出不同部门的担任“钳工”的职工平均工资高于2000的部门
select department_name ,AVG(salary)as avgsalary from wcemploy where type=‘钳工‘ GROUP BY department_name HAVING AVG(salary)>2000
错误写法:
select department_name ,AVG(salary)as avgsalary from wcemploy where type=‘钳工‘ and AVG(salary)>2000 GROUP BY department_name
4.请用一个SQL语句查询每个部门低于平均工资的员工信息
select * from wcemploy w ,(select AVG(salary) as davg,department_name//一个W表 from wcemploy GROUP BY department_name)t//一个t表 where w.department_name = t.department_name and w.salary<davg
1 select w.*,davg from 2 wcemploy w 3 left join (select AVG(salary) as davg,department_name 4 from wcemploy 5 GROUP BY department_name )t on w.department_name = t.department_name 6 where w.department_name = t.department_name and w.salary<davg
以上是关于Java笔试题之SQL语句(单表)的主要内容,如果未能解决你的问题,请参考以下文章