首页 > 数据库 >SQL案例

SQL案例

时间:2022-08-22 22:59:05浏览次数:58  
标签:案例 SQL employee deptno BILLNUMBERID where id select

1.

select e.employee_name AS MANAGER,ROW_NUMBER()over(PARTITION by e.date order by e.salary DESC) AS RN FROM
(
(select DISTINCT b.manager_id,a.employee_name from Employee a inner join Employee b on a.employee_id = b.manager_id) c
left join
(select employee_id,salary,date from Salary) d on c.manager_id = d.employee_id
) e
WHERE RN = 1
2.
select d.employee_id,d.employee_name,d.SalaryByYear from (
(
select a.employee_id,a.employee_name from Employee a where a.employee_id not in
(select distinct manager_id from Employee)
) b
left join
(
select employee_id,SUBSTR(date,1,4) as year,sum(salary) as SalaryByYear from Salary GROUP by employee_id,SUBSTR(date,1,4)
) c
on b.employee_id = c.employee_id
)d order by d.SalaryByYear desc limit 1

3.

    select DISTINCT b.COMPANYCOD from
    (
    (select BILLNUMBERID,ORDERNO,ADJDIRECT,COMPANYCOD,count(*) as count from sheet1 WHERE ADJDIRECT = "调入方" GROUP BY BILLNUMBERID,ORDERNO,ADJDIRECT,COMPANYCOD) a
    INNER JOIN
    (select BILLNUMBERID,ORDERNO,ADJDIRECT,COMPANYCOD,count(*) as count from sheet1 WHERE ADJDIRECT = "调出方" GROUP BY BILLNUMBERID,ORDERNO,ADJDIRECT,COMPANYCOD) b
    on a.BILLNUMBERID = b.BILLNUMBERID and a.ORDERNO = b.ORDERNO and a.COMPANYCOD=b.COMPANYCOD and a.count = b.count
    )

4.

    select distinct b.COMPANYCOD from
    (
    (select BILLNUMBERID,ORDERNO,ADJDIRECT,COMPANYCOD,count(*) as count from sheet1 WHERE ADJDIRECT = "调入方" GROUP BY BILLNUMBERID,ORDERNO,ADJDIRECT,COMPANYCOD having count=1) a
    INNER JOIN
    (select BILLNUMBERID,ORDERNO,ADJDIRECT,COMPANYCOD from sheet1 WHERE ADJDIRECT = "调出方") b
    on a.BILLNUMBERID = b.BILLNUMBERID and a.ORDERNO = b.ORDERNO and a.COMPANYCOD=b.COMPANYCOD
    )

5.

    select a.BILLNUMBERID,sum(ADJUSTMONEY + AFTERTAXMONEY) AS 调整总金额 from
    (SELECT distinct BILLNUMBERID,ADJUSTWAYC from sheet1 where ADJUSTWAYC = "双向调整") a
    INNER JOIN sheet1 b on a.BILLNUMBERID = b.BILLNUMBERID
    GROUP BY a.BILLNUMBERID

 

6.删除工资大于所在部门平均工资的员工记录。
delete from emp where sal>(select avg(sal) from emp where deptno=emp1.deptno);

 

7.查询大于自己部门平均工资的员工姓名、工资、所在部门平均工资、高于部门平均工资的额度。
select ename,sal,avgsal,sal-avgsal from emp e,(select deptno,avg(sal) avgsal from emp group by deptno )d where e.deptno=d.deptno and e.sal>d.avgsal;


8.查询所有工作在NEW YORK和CHICAGO的员工姓名、员工编号,以及他们的经理姓名、经理编号。
select e.ename,e.empno,m.ename,m.empno from emp e,emp m,dept d where e.mgr=m.empno and e.deptno=d.deptno and d.loc in(‘NEW YORK’,‘CHICAGO’);


9.查询职位及经理和10部门任意一个员工职位及经理相同的员工姓名、职位,不包括10部门员工。
select ename,job from emp where (mgr,job)in(select job,mgr from emp where deptno=10) and deptno<>10;

10.列出一个雇员都没有的所有部门名称。
select dname from dept d where not exists(select 1 from emp where deptno=d.deptno);

 

11.列出至少有一个雇员的所有部门名称。
select dname from dept d where exists (select 1 from emp where deptno=d.deptno);

 

12查询入职日期在1982年1985年的员工姓名,入职日期。
select ename,hiredate from emp where extract(year from hiredate) between 1982 and 1985;

 

13、查询入职日期最早的前5名员工姓名。
答案: select ename,hiredate from emp e where (select count(empno) from emp where hiredate<e.hiredate)<5;

 

标签:案例,SQL,employee,deptno,BILLNUMBERID,where,id,select
From: https://www.cnblogs.com/wxd131488/p/16537131.html

相关文章

  • mysql部分--安装mysql 8.0以上版本
    安装mysqlmysql本质上是一个软件一、mysql安装1.下载链接:https://downloads.mysql.com/archives/community/2.先安装windows补丁[百度网盘下载](链接:https://pan.baid......
  • 【SQLServer】SQL server 2016如何修改网络包的大小
    SQLserver2016中,可以使用图形界面或者T-SQL来修改网络包大小的设置。缺省是4KB的大小。通常官方不建议修改默认值。支持的最小值是512bytes,最大值是32KB。此外,对于加密......
  • sql server2019 发布订阅
    1、新建文件夹“dbshare”并共享2、新建发布3、设置快照路径4、新建订阅(在订阅服务器上操作)5、采坑!!!到这位置,基本就完事了,但是发现怎么TM也不好用。......
  • 如何手动卸载 SQL Server
    背景适用于:MicrosoftSQLServer2008、MicrosoftSQLServer2008R2、MicrosoftSQLServer2012、MicrosoftSQLServer2014、MicrosoftSQLServer2016、Microsoft......
  • Docke 搭建 apache2 + php8 + MySQL8 环境
    Docker安装执行Docker安装命令curl-fsSLhttps://get.docker.com/|sh启动Docker服务sudoservicedockerstart查看Docker是否正常工作sudo......
  • 项目案例-运用西门子PLC通过Modbus RTU采集YGWE通讯数据
    本案例控制的是YGWE冷却系统。系统主PLC选用西门子CPU,通过Modbus转Profinet网关采集YGWE通讯数据。 1、首先创建新项目文件;  2、导入GSD文件,将小疆智控MODBUS转PR......
  • SYSDATE() vs NOW() in MySQL
    SYSDATE() returnsthetimeatwhichitexecutes.返回实际的执行时间。NOW() returnsaconstanttimethatindicatesthetimeatwhichthe statement begant......
  • 使用foreach 实现sql 拼接
    有时候写sql时,需要根据传入的参数构建sql语句,实现遍历集合,构建in条件语句或者批量操作语句,此时可以使用foreach实现对sql的拼接。下面是foreach标签的各个属性属性......
  • 慢SQL抓包与TCP 三次握手4次挥手的原因
                                                        来......
  • mybatis中mapper文件中的sql语句,大于小于号报错
    在mapper文件中,撰写一个sql,发现用到大于小于对比时,会提示:tagnameexpected报错原因:xml格式文件中,部分符号会被识别解决方法:1、使用转义符号,替换原本的大于号小于号,常用......