.1.建库建表
mysgl>create database mydb8 worker;
mysq> use mydb8 worker;
mysgl>create table t worker(5department id int(11)not null comment'部门号'worker id int(11)primary key not null comment'职工号',6worker date date not null comment'工作时间'wages float(8,2)not null comment'工资'8politics varchar(10)not nu1l default'群众'comment'政治面貌'010name varchar(20)not nullcomment'姓名'borth date date not null comment'出生日期');
2.插入数据
insert into t worker values (101.1001.'2015-5-4'.7500.00,'群众','张春燕','1990-7-1');
insert into t_worker values(101,1002,'2019-2-6',5200.00.'团员','李名博''1997-2--8');
insert into t worker values(102,1003,'2008-1-4',10500.00,'党员','王博涵','1983-6-8');
insert into t_worker values (102,1004,'2016-10-10',5500.00,'群众','赵小军','1994-9-5');
insert into t_worker(102,1005,'2014-4-1',8800.00,党员','钱有财','1992-12-30');
insert into t worker valuesinsert into t_worker values(103,1006,'2019-5-5',5500.00,'党员','孙菲菲','1996-9-2');
3.完成查询
(1)、显示所有职工的基本信息。
mysql> select * from t_worker;
(2)、查询所有职工所属部门的部门号,不显示重复的部门号。
mysql> select distinct department_id from t_worker;
(3)、求出所有职工的人数。
mysql> select count(1) from t_worker;
(4)、列出最高工和最低工资。
mysql> select max(wages),min(wages) from t_worker;
(5)、列出职工的平均工资和总工资。
mysql> select round(avg(wages),2),sum(wages) from t_worker;
(6)、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
mysql> create table 工作日期(worker_id int(11) primary key not null comment'职工号', name varchar(20) not null default' 群众' comment'政治面貌', department_id2 int(11) not null comment'参加部门');
(7)、显示所有党员的年龄。
mysql> select year(now())-year(borth_date),name from t_worker where politics='党员';
(8)、列出工资在4000-8000之间的所有职工姓名
mysql> select wages,name from t_worker where wages between 4000 and 8000
(9)、列出所有孙姓和李姓的职工姓名。
mysql> select name from t_worker where name like '孙%' or name like '李%';
(10)、列出所有部门号为102和103日不是党员的职工号、姓名。
mysql> select department_id, name from t_worker where department_id=102 and politics!='党员' or department_id=102 and p olitics!='党员';
(11)、将职工表t worker中的职工按出生的先后顺序排序。
mysql> select borth_date,name from t_worker order by borth_date;
(12)、显示工资最高的前3名职工的职工号和姓名。
mysql> select department_id,name from t_worker order by wages desc limit 3;
(13)、求出各部门党员的人数。
mysql> select department_id,sum(politics='党员') from t_worker group by department_id;
(14)、统计各部门的工资和平均工资并保留2位小数
mysql> select department_id,round(avg(wages),2),sum(wages) from t_worker group by department_id
(15)列出总人数大于等于3的部门号和总人数。
mysql> select department_id,count(department_id) from t_worker group by department_id having count(department_id)>=3;