SQL 课后作业答案 练习2 练习6

Exercise2

1. Find the name and salary of employees in Luton.

SELECT ename,sal
FROM emp e,dept2020290223 d
WHERE loc='Luton' AND e.deptno=d.deptno

 2. Join the DEPT table to the EMP table and show in department number
order.

SELECT *FROM emp e NATURAL JOIN dept2020290223 d
ORDER BY deptno

3. List the names of all salesmen who work in SALES
 

SELECT ename
FROM emp e NATURAL JOIN dept2020290223 d
WHERE job='SALESMAN' AND DNAME='SALES'

 4. List all departments that do not have any employees.

SELECT d.deptno
FROM dept2020290223 d
WHERE deptno NOT IN (SELECT deptno FROM emp NATURAL JOIN
dept2020290223)

 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.

SELECT employee.ename,employee.sal,manager.ename,manager.sal
FROM emp employee,emp manager
WHERE employee.sal>manager.sal AND employee.mgr=manager.empno

6. List the employees who have BLAKE as their manager.

SELECT employee.ename
FROM emp employee,emp manager
WHERE employee.mgr=manager.empno AND manager.ename='BLAKE'

 Exercise 6:

1. List the name and job of employees who have the same job as Jones.

SELECT e1.ename, e1.job
FROM emp e1,emp e2
WHERE e1.job=e2.job AND e2.ename='Jones' AND e1.ename != 'Jones'

2. Find all the employees in Department 10 that have a job that is the
same as anyone in department 30.

SELECT d10.ename, d10.job
FROM emp d10,emp d30WHERE d10.job = d30.job AND d30.deptno=30 AND d10.deptno=10

3. List the name, job, and department of employees who have the same
job as Jones or a salary greater than or equal to Ford.

SELECT ename,job,deptno
FROM emp
WHERE job IN (SELECT job FROM emp WHERE ename='Jones') AND
ename!="Jones" OR
sal>=(SELECT sal FROM emp WHERE ename='Ford') AND ename!="Ford"

4. Find all employees in department 10 that have a job that is the same as
anyone in the Sales department.

SELECT ename,job,deptno
FROM emp
WHERE deptno=10 AND job IN (SELECT job FROM emp WHERE deptno IN
(SELECT deptno FROM dept2020290223 WHERE dname='Sales') )

5. Find the employees located in Liverpool who have the same job as
Allen. Return the results in alphabetical order by employee name.
 

SELECT ename,loc
FROM emp NATURAL JOIN dept2020290223
WHERE loc='Liverpool' AND job IN (SELECT job FROM emp WHEREename='Allen') AND ename!='Allen'
ORDER BY ename

6. Find all the employees that earn more than the average salary of
employees in their department.

SELECT ename,sal,e.deptno
FROM emp e,(SELECT AVG(sal) sal_avg,deptno FROM emp GROUP BY deptno)
WHERE e.deptno=a.deptno AND sal>sal_avg

7. Find all the employees that earn more than JONES, using temporary
labels to abbreviate table names.

SELECT ename,sal
FROM emp e
WHERE sal>(SELECT sal FROM emp WHERE ename='Jones')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值