MYSQL数据库SQL语句练习实验
Posted 风信子的猫Redamancy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL数据库SQL语句练习实验相关的知识,希望对你有一定的参考价值。
mysql数据库SQL语句练习实验(2)
文章目录
Exercise 1.11-1.21
Exercise 1.11
2.11 Find all the employees who earn between £15,000 and £20,000.
Show the employee name, department and salary.
我们需要找到所有SAL在15,000和20,000之间的EMP,并且展示他们的NAME,DEPTNO和SAL,这里我们还是用SELECT语句
这里第一种可以用两个判断
SELECT `ENAME`,`DEPTNO`,`SAL` FROM `emp` WHERE (20000 > `SAL` AND `SAL` > 15000)
第二种哦我们可以用SQL的BETWEEN
SELECT `ENAME`,`DEPTNO`,`SAL` FROM `emp` WHERE `SAL` BETWEEN 15000 AND 20000
Exercise 1.12
2.12 Find the name of the President.
这个题需要我们寻找到JOB为President的EMP,我们去定义他的JOB
SELECT * FROM `emp` WHERE `JOB`="President"
Exercise 1.13
2.13 Find all the employees whose last names end with S
这里我们要找到以ENAME以S结尾的EMP,我们这里可以用%S或者_S来表示,就可以得到我们的结果。
SELECT * FROM `emp` WHERE `ENAME` LIKE '%S';
Exercise 1.14
2.14 List the employees whose names have TH or LL in them
这一题与上一题类似,不过现在是TH或者LL,但是我们可以用一样的方法
SELECT * FROM `emp` WHERE `ENAME` LIKE '%TH%' OR `ENAME` LIKE '%LL%'
Exercise 1.15
2.15 List only those employees who receive commission.
我们可以显示所有COMM不为0的EMP,用以下SQL的
SELECT * FROM `emp` WHERE `COMM` != 0
Exercise 1.16
2.16 Find the name, job, salary, hiredate, and department number of all employees by alphabetical order of name.
这道题我们需要得到的字段,并且这些字段会根据我们的名字的字母顺序进行排序
SELECT ename,job,sal,hiredate,deptno FROM `emp` ORDER BY ename
Exercise 1.17
2.17 Find the name, job, salary, hiredate and department number of all employees in ascending order by their salaries.
这里需要我们的数据,以我们的工资作为我的排序指标,并且以升序来排序,而且在我们的排序之中,我们默认是用升序的方法
SELECT ename,job,sal,hiredate,deptno FROM `emp` ORDER BY sal
Exercise 1.18
2.18 List all salesmen in descending order by commission divided by their salary.
对于这个题目来说,我们需要以commission / salary作为我们的排序指标,并且以降序的方法排列。
SELECT * FROM emp WHERE job="salesman" ORDER BY (comm/sal) DESC
Exercise 1.19
2.19 Order employees in department 30 who receive commision, in ascending order by commission
我们需要在deptno为30和comm!=0的EMP,并且以comm以升序排序
SELECT * FROM `emp` WHERE deptno=30 AND comm!=0 ORDER BY comm
Exercise 1.20
2.20 Find the names, jobs, salaries and commissions of all employees who do not have managers.
对于这个题目我们需要得到name,job,sal,comm的EMP,并且没有manager
SELECT ename,job,sal,comm FROM `emp` WHERE mgr=0
Exercise 1.21
2.21 Find all the salesmen in department 30 who have a salary greater than or equal to £18000.
对于这道题来说,我们需要对我们的EMP进行一个寻找。找一些工资大于18000的salesmen,并且在deptno=30的地方。
SELECT * FROM `emp` WHERE deptno=30 AND job="salesman" AND sal >= 18000
以上是关于MYSQL数据库SQL语句练习实验的主要内容,如果未能解决你的问题,请参考以下文章