Exercise 2.1-2.6
3、完成EXERCISES 2对表格的操作,1-6题
Exercise 2.1
3.1. Find the name and salary of employees in Luton.
这里我们需要将EMP和DEPT连接起来,将连接起来,然后选择ENAME,SAL选择出来,将外键写起来。
SELECT ename,sal FROM `emp`,`dept` WHERE emp.deptno=dept.deptno AND loc="luton"
Exercise 2.2
3.2. Join the DEPT table to the EMP table and show in department number order.
我们需要将DEPT和EMP连接起来,然后最后按其中一个number来排序
SELECT * FROM `emp`,`dept` WHERE emp.deptno=dept.deptno ORDER BY emp.deptno
Exercise 2.3
3.3. List the names of all salesmen who work in SALES
我们需要列出所有工作在SALES的salesman,这里我们也需要连接我们的deptno
SELECT ename FROM `emp`,`dept` WHERE emp.deptno=dept.deptno AND dname="sales" AND job
Exercise 2.4
3.4. List all departments that do not have any employees.
对于这个题目来说,我们得列出所有不含EMP的departments
SELECT dept.deptno FROM deptWHERE deptno NOT IN (SELECT dept.deptnoFROM dept, empWHERE d
Exercise 2.5
3.5 For each employee whose salary exceeds his manager’s salary, list the employee’s name and salary and the manager’s name and salary.
我们需要列出所有EMP中employee工资比他的manager工资的ename和salary,并且列出他的manager的name和salary。其实也是一样的列法
SELECT WORKER.ename, WORKER.sal, MANAGER.ename, MANAGER.salFROM emp WORKER, emp MANAGERWHERE WORKER.mgr = MANAGER.empnoAND WORKER.sal > MANAGER.sal
不过最后得到的结果是空,说明领导的工资一般还是比员工要高的
Exercise 2.6
3.6. List the employees who have BLAKE as their manager.
首先对于这道题,我们需要挑出所有的EMP中,将BLAKE作为MGR的EMP,所以我们应该将MGR和EMPNO连接起来,并且最后将ename设置为BLAKE
SELECT WORKER.*FROM emp WORKER, emp MANAGERWHERE WORKER.mgr = MANAGER.empnoAND MANAGER.ename="BLAKE";