題目:系統中有一個表WCEmploy(職工號,姓名,部門名,工種,工資)數據庫
一.請寫出建表語句spa
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 )
二.插入數據code
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)
多條數據插入blog
在插入數據的時候,發生了一個錯誤,插入漢字數據報錯table
修改MySQL的數據庫的數據庫屬性字符集爲class
三.查詢語句select
1.請用一個SQL語句查詢每一個部門的總人數im
select department_name ,COUNT(DISTINCT id) from wcemploy GROUP BY department_name
2.請用一個SQL語句查詢出不一樣部門的擔任「鉗工」的職工平均工資d3
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