SQLite子查询和连接查询Demo

–一对多外键关联
create table TbDept
(
deptno tinyint primary key,
dname varchar(10) not null,
dloc varchar(20) not null
);

insert into TbDept values (10, ‘会计部’, ‘北京’);
insert into TbDept values (20, ‘研发部’, ‘成都’);
insert into TbDept values (30, ‘销售部’, ‘重庆’);
insert into TbDept values (40, ‘运维部’, ‘深圳’);

create table TbEmp
(
empno int primary key,
ename varchar(20) not null,
job varchar(10) not null,
mgr int,
sal int not null,
dno tinyint,
foreign key (dno) references TbDept(deptno)
);

insert into TbEmp values (7800, ‘张三丰’, ‘总裁’, null, 9000, 20);
insert into TbEmp values (2056, ‘乔峰’, ‘分析师’, 7800, 5000, 20);
insert into TbEmp values (3088, ‘李莫愁’, ‘设计师’, 2056, 3500, 20);
insert into TbEmp values (3211, ‘张无忌’, ‘程序员’, 2056, 3200, 20);
insert into TbEmp values (3233, ‘丘处机’, ‘程序员’, 2056, 3400, 20);
insert into TbEmp values (3251, ‘张翠山’, ‘程序员’, 2056, 4000, 20);
insert into TbEmp values (5566, ‘宋远桥’, ‘会计师’, 7800, 4000, 10);
insert into TbEmp values (5234, ‘郭靖’, ‘出纳’, 5566, 2000, 10);
insert into TbEmp values (3344, ‘黄蓉’, ‘销售主管’, 7800, 3000, 30);
insert into TbEmp values (1359, ‘胡一刀’, ‘销售员’, 3344, 1800, 30);
insert into TbEmp values (4466, ‘苗人凤’, ‘销售员’, 3344, 2500, 30);
insert into TbEmp values (3244, ‘欧阳锋’, ‘程序员’, 3088, 3200, 20);
insert into TbEmp values (3577, ‘杨过’, ‘会计’, 5566, 2200, 10);
insert into TbEmp values (3588, ‘朱九真’, ‘会计’, 5566, 2500, 10);

  1. 查询最高工资及其对应员工

    select ename, sal from TbEmp where sal=(select max(sal) from TbEmp);
    
  2. 计算每位员工的年薪

    select ename, sal*12 as annSal from TbEmp;
    
  3. 统计有员工的部门的人数
    group by 按部门分组

    select dno, count(dno) from TbEmp group by dno;
    

select dname, total from (select dno,count(dno) as total from TbEmp group by dno) as t1,TbDept as t2 where T1.dno=t2.deptno;(部门名称和人数,()里的是临时表格)
select dname,total from (select dno, count(dno) as total from TbEmp group by dno) as t1 inner join TbDept as t2 on t1.dno=t2.deptno;
–左外连接,不满足条件的直接补空值
select dname,total from TbDept as t2 left outer join(select dno, count(dno) as total from TbEmp group by dno) as t1 on t1.dno=t2.deptno;

  1. 求挣最高薪水的员工(boss除外)的姓名
    –null不能直接用等号赋值,而是要用is null或者is not
    null

    select ename, sal from TbEmp where sal=(select max(sal) from TbEmp where mgr is not null);
    
  2. 查询薪水超过平均薪水的员工的姓名和工资
    select ename,sal from TbEmp ,(select avg(sal) as avgSal from TbEmp) where sal > avgSal;

select ename, sal from TbEmp
where sal>(select avg(sal) from TbEmp);

  1. 查询薪水超过其所在部门平均薪水的员工的姓名、部门编号和工资

–t1.dno是为了区分两个dno的歧义
select ename, sal, t1.dno from TbEmp as t1,
(select dno, avg(sal) as avgSal from TbEmp group by dno) as t2 where t1.dno=t2.dno and t1.sal>t2.avgSal;

select ename, sal, t1.dno from TbEmp as t1 inner join
(select dno, avg(sal) as avgSal from TbEmp group by dno) as t2 on 
t1.dno=t2.dno and t1.sal>t2.avgSal;
```
  1. 查询部门中薪水最高的人姓名、工资和所在部门名称
    select ename, sal, dname from (select ename, sal, t1.dno from TbEmp as t1 inner join(select dno, max(sal) as maxSal from TbEmp group by dno) as t2 on t1.dno=t2.dno and sal=maxSal) as t3 inner join TbDept as t4 where dno=deptno;

  2. 哪些人是主管
    –distinct去除重复出现的
    INITERSECT叉集
    UNION并集
    IN交集

    select ename, job from TbEmp where empno in 
    (select distinct mgr from TbEmp where mgr is not null);
    

select ename from TbEmp where empno in (select distinct mgr from TbEmp where mgr is not null);

  1. 求平均薪水最高的部门的名称和平均工资
    select dname,maxSal from(select dno, max(avgSal) as maxSal from (select dno,avg(sal) as avgSal from TbEmp group by dno)) as t1 inner join TbDept as t2 on t1.dno=t2.deptno;

  2. 求薪水最高的前3名雇员

    select ename, sal from TbEmp order by sal desc limit 3;
    
  3. 求薪水排在第4-6名雇员

    select ename, sal from TbEmp order by sal desc limit 3 offset 3;
    
    

–从第3个开始查询三个,也可以是五个什么的
select * from TbEmp order by sal desc limit 3,3;

  1. 求薪水最低的部门经理所在部门的名称
    原文作者:LennonLin
    原文地址: https://www.jianshu.com/p/31ae5137c31d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞