20200327 连接子查询作业

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

根据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 回帖
请输入回帖内容 ...
  • keke

    谦虚了,能坚持保持学习状态,棒棒的

  • 其他回帖
  • jeck

    image.png
    image.png
    image.png

  • ke2beck
    #1.列出至少有三个员工的所有部门和部门信息。
    SELECT dept.* from dept WHERE dept.DEPTNO IN 
            (SELECT DEPTNO from emp GROUP BY emp.DEPTNO HAVING COUNT(emp.DEPTNO)>=3);
    
    #2.列出受雇日期早于直接上级的所有员工的编号,姓名,部门名称
    SELECT e1.EMPNO, e1.ENAME,d.DNAME FROM emp e1,dept d
    	WHERE e1.HIREDATE < (SELECT e2.HIREDATE FROM emp e2 WHERE e2.empno=e1.MGR)
    	AND d.DEPTNO=e1.DEPTNO;
    
    #3.列出职位为“CLERK”的姓名和部门名称,部门人数
    SELECT emp_name,dept_name,emp_num FROM (
    	SELECT emp.ENAME emp_name,
    			dept.DNAME dept_name,
    			(SELECT COUNT(emp.EMPNO) FROM emp WHERE emp.DEPTNO=dept.DEPTNO) emp_num 
    	FROM emp,dept 
    	WHERE emp.JOB='CLERK' AND emp.DEPTNO=dept.DEPTNO
    ) as t
    
    
    

    image.png
    image.png
    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'

  • 查看更多回帖