20200327 连接子查询作业

本贴最后更新于 1615 天前,其中的信息可能已经时移世改

根据emp和dept表完成下列作业:

1.列出至少有三个员工的所有部门和部门信息。

2.列出受雇日期早于直接上级的所有员工的编号,姓名,部门名称

3.列出职位为“CLERK”的姓名和部门名称,部门人数:

-- 部门表

CREATE TABLE DEPT(
DEPTNO INT PRIMARY KEY, -- 部门编号
DNAME VARCHAR(14) , -- 部门名称
LOC VARCHAR(13) -- 部门地址
) ;

INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');

-- 员工表

CREATE TABLE EMP
(
EMPNO INT PRIMARY KEY, -- 员工编号
ENAME VARCHAR(10), -- 员工名称
JOB VARCHAR(9), -- 工作
MGR DOUBLE, -- 直属领导编号
HIREDATE DATE, -- 入职时间
SAL DOUBLE, -- 工资
COMM DOUBLE, -- 奖金
DEPTNO INT, -- 部门号
FOREIGN KEY(DEPTNO) REFERENCES DEPT(DEPTNO));

INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,'1987-07-13',3000,NULL,20);
INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,'1987-07-13',1100,NULL,20);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);

回复形式:

1、问题+答案 或者 问题求助

2、本次系列课的心得体会,收获、建议以及评价都可以哦~~

注意:老师会评选出优秀回复+作业全勤+到课全勤,将会有大奖等着优秀的你

15 回帖
请输入回帖内容 ...
  • bottle_7

    能否麻烦另外发帖解答下答案呢

  • 其他回帖
  • hljmingxi

    image.png

    一直研究到现在😂 ,终于彻底解决了,开森 😊

  • zhongyh

    根据emp和dept表完成下列作业:

    1.列出至少有三个员工的所有部门和部门信息。

    select * from dept1 d INNER JOIN
    (select DEPTNO, count() 部门人数 from emp1 GROUP BY DEPTNO HAVING count() >= 3) e
    on d.DEPTNO=e.DEPTNO;

    2.列出受雇日期早于直接上级的所有员工的编号,姓名,部门名称

    select t.ename,t.empno,d.dname from dept1 d,
    (select a.ename,a.empno,a.deptno from emp1 a where a.hiredate<(select hiredate from emp1 b where b.empno=a.mgr))t
    where t.deptno=d.deptno;

    3.列出职位为“CLERK”的员工姓名和部门名称,部门人数:

    select e1.ename,e1.job,t.dname,t.部门总人数 from emp1 e1 left join
    (select d.deptno,d.dname,count(*) 部门总人数 from dept1 d
    INNER JOIN emp1 e
    on d.deptno=e.deptno GROUP BY d.dname) t
    on e1.deptno= t.deptno where e1.job='CLERK'

  • che 1

    1.列出至少有三个员工的所有部门和部门信息
    select * from DEPT WHERE DEPT.DEPTNO IN(select DEPTNO from EMP group by DEPTNO HAVING COUNT(EMP.DEPTNO)>=3)

    2.列出受雇日期早于直接上级的所有员工的编号,姓名,部门名称
    SELECT S.EMPNO,S.ENAME,DEPT.DNAME FROM EMP S LEFT JOIN EMP Y ON Y.EMPNO=S.MGR INNER JOIN DEPT ON DEPT.DEPTNO=S.DEPTNO WHERE Y.HIREDATE>S.HIREDATE
    3.列出职位为“CLERK”的姓名和部门名称,部门人数:
    SELECT DEPT.DNAME,B.ENAME,B.NUM FROM (SELECT * FROM EMP WHERE JOB='CLERK') A JOIN (SELECT COUNT(EMPNO) NUM,ENAME,DEPTNO FROM EMP GROUP BY DEPTNO) B ON A.DEPTNO=B.DEPTNO
    JOIN DEPT ON B.DEPTNO=DEPT.DEPTNO

  • 查看更多回帖