目录
1、首先创建员工表emp和部门表dept:
dept表:
emp表:
2、插入数据:
dept表:
emp表:
3、 按条件查找
create table dept (
dept1 int ,
dept_name varchar(11));
create table emp (
sid int ,
name varchar(11),
age int,
worktime_start date,
incoming int,
dept2 int);
insert into dept values (101,'财务');
insert into dept values (102,'销售');
insert into dept values (103,'IT技术');
insert into dept values (104,'行政');
insert into emp values(1789,'张三',35,'1980/1/1',4000,101);
insert into emp values(1674,'李四',32,'1983/4/1',3500,101);
insert into emp values(1776,'王五',24,'1990/7/1',2000,101);
insert into emp values(1568,'赵六',57,'1970/10/11',7500,102);
insert into emp values(1564,'荣七',64,'1963/10/11',8500,102);
insert into emp values(1879,'牛八',55,'1971/10/20',7300,103);
select name from emp where age=(select max(age)from emp);
select name
from emp
where incoming=(select min(incoming) from emp
inner join dept
on emp.dept2=dept.dept1 and dept_name='财务') ;
SELECT dept.dept_name as 部门名称,sum(emp.incoming) as 总收入
FROM emp
INNER JOIN dept
ON dept.dept1 = emp.dept2
GROUP BY dept.dept_name HAAVING 总收入 > 8000;
select emp.name,dept.dept_name,emp.age,emp.incoming
from emp
inner join dept
on emp.dept2=dept.dept1
where incoming between 7500 and 8500 and age=(select max(age) from emp);
select emp.worktime_start
from emp
inner join dept
on emp.dept2=dept.dept1 where incoming=(select min(incoming) from emp);
select emp.name from emp
inner join dept
on emp.dept2=dept.dept1 where incoming>2000 and dept.dept_name='财务';
select dept_name,avg(emp.incoming) as 部门平均收入
from emp innerjoin dept
on emp.dept2=dept.dept1 group by dept.dept_name;
select dept.dept_name,emp.sid
from emp inner
join dept
on emp.dept2=dept.dept1 where dept.dept_name='IT技术';
select dept.dept_name,sum(incoming) as ‘总收入’
from emp
inner join dept
on emp.dept2=dept.dept1 where dept.dept_name='财务';
select dept.dept_name,emp.dept2
from emp
inner join dept
on emp.dept2=dept.dept1 order by dept.dept1;
select dept.dept_name,worktime_start
from emp
inner join dept
on emp.dept2mp.dept2=dept.dept1 order by worktime_start;
select * from emp where worktime_start is null;
SELECT emp.dept2,dept.dept_name,incoming FROM emp INNER JOIN dept
ON emp.dept2 = dept.dept1 where incoming>7000;
SELECT dept.dept_name as '部门名称',sum(incoming) as '部门员工总收入'
FROM emp
inner JOIN dept
ON emp.dept2 = dept.dept1 group by dept.dept_name;
SELECT dept.dept_name,age
FROM (SELECT dept2, MAX(age) AS max_age
FROM emp
GROUP BY dept2 )
AS
max_age_table
INNER JOIN emp
ON emp.dept2 = max_age_table.dept2 AND emp.age = max_age_table.max_age
INNER JOIN dept
ON emp.dept2 = dept.dept1;
SELECT emp.name,incoming,dept.dept_name as '部门名称'
FROM emp
INNER JOIN dept
ON emp.dept2 = dept.dept1 where name='李四';
SELECT emp.name,dept.dept_name,emp.incoming
FROM (SELECT dept2,2, MAX(incoming)
AS
max_incoming
FROM emp
GROUP BY dept2 )
AS
max__incoming_table
INNER JOIN emp
ON emp.dept2 = max_incoming_table.dept2 ANDD emp.incoming = max_incoming_table.max_incoming INNER JOIN dept
ON emp.deept2 = dept.dept1 ORDER BY emp.incoming DESC;
SELECT dept.dept_name
FROM dept
INNER JOIN (SELECT dept2, COUNT(*)
AS
emp_count FROM emp GROUP BY dept2 HAVING COUNT(*) > 1 )
AS
emp_count_table ON dept.dept1 = emp_count_table.dept2;
SELECT emp.name,dept.dept_name as '部门名称'
FROM emp
INNER JOINdept
ON emp.dept2 = dept.dept1 where name='张三';