数据库练习

Posted Al_tair

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库练习相关的知识,希望对你有一定的参考价值。

数据库练习


大家好,我是小笙,我们学习完基础mysql,我们接下来需要进行一些练习,巩固一下基础!

雇员系统表

# 部门表
CREATE TABLE dept( 
deptno MEDIUMINT   UNSIGNED  NOT NULL  DEFAULT 0, 
dname VARCHAR(20)  NOT NULL  DEFAULT "", -- 部门名
loc VARCHAR(13) NOT NULL DEFAULT ""
);
 
INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK'), (20, 'RESEARCH', 'DALLAS'), (30, 'SALES', 'CHICAGO'), (40, 'OPERATIONS', 'BOSTON');

# 创建表EMP雇员
CREATE TABLE emp
(empno  MEDIUMINT UNSIGNED  NOT NULL  DEFAULT 0,  -- 编号
ename VARCHAR(20) NOT NULL DEFAULT "",  -- 名字
job VARCHAR(9) NOT NULL DEFAULT "", -- 工作
mgr MEDIUMINT UNSIGNED , -- 上级编号
hiredate DATE NOT NULL, -- 入职时间
sal DECIMAL(7,2)  NOT NULL, -- 薪水
comm DECIMAL(7,2) , -- 红利
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0  -- 部门编号
);
 
INSERT INTO emp VALUES(7369, 'SMITH', 'CLERK', 7902, '1990-12-17', 800.00,NULL , 20), 
(7499, 'ALLEN', 'SALESMAN', 7698, '1991-2-20', 1600.00, 300.00, 30),  
(7521, 'WARD', 'SALESMAN', 7698, '1991-2-22', 1250.00, 500.00, 30),  
(7566, 'JONES', 'MANAGER', 7839, '1991-4-2', 2975.00,NULL,20),  
(7654, 'MARTIN', 'SALESMAN', 7698, '1991-9-28',1250.00,1400.00,30),  
(7698, 'BLAKE','MANAGER', 7839,'1991-5-1', 2850.00,NULL,30),  
(7782, 'CLARK','MANAGER', 7839, '1991-6-9',2450.00,NULL,10),  
(7788, 'SCOTT','ANALYST',7566, '1997-4-19',3000.00,NULL,20),  
(7839, 'KING','PRESIDENT',NULL,'1991-11-17',5000.00,NULL,10),  
(7844, 'TURNER', 'SALESMAN',7698, '1991-9-8', 1500.00, NULL,30),  
(7900, 'JAMES','CLERK',7698, '1991-12-3',950.00,NULL,30),  
(7902, 'FORD', 'ANALYST',7566,'1991-12-3',3000.00, NULL,20),  
(7934,'MILLER','CLERK',7782,'1992-1-23', 1300.00, NULL,10);

# 工资级别表
CREATE TABLE salgrade
(
grade MEDIUMINT UNSIGNED NOT NULL DEFAULT 0, -- 工资级别
losal DECIMAL(17,2)  NOT NULL, -- 该级别的最低工资
hisal DECIMAL(17,2)  NOT NULL -- 该级别的最高工资
);

INSERT INTO salgrade VALUES (1,700,1200);
INSERT INTO salgrade VALUES (2,1201,1400);
INSERT INTO salgrade VALUES (3,1401,2000);
INSERT INTO salgrade VALUES (4,2001,3000);
INSERT INTO salgrade VALUES (5,3001,9999);

雇员表的结构

相关练习

1.显示所有部门名称

SELECT dname FROM dept;

2.显示所有雇员名及其全年收入13月(工资+补助),并指定列别名"年收入”

SELECT ename,(sal+IF(comm IS NULL,0,comm))*13 AS '年收入' FROM emp;
SELECT ename,(sal+IFNULL(comm,0))*13 AS '年收入' FROM emp;

3.显示工资超过2850的雇员姓名和工资

SELECT ename,sal FROM emp
			WHERE sal > 2850;

4.显示工资不在1500到2850之间的所有雇员名及工资

SELECT ename,sal FROM emp
			WHERE sal < 1500 OR sal > 2850;

5.显示编号为7566的雇员姓名及所在部门编号

SELECT ename,deptno FROM emp
			WHERE empno = 7566;

6.显示部门10和30中工资超过1500的雇员名及工资

SELECT ename,sal FROM emp		
			WHERE sal > 1500 AND (deptno = 10 OR deptno = 30);

7.显示无管理者的雇员名及岗位

SELECT ename,job FROM emp
			WHERE mgr is NULL;

8.显示在1991年2月1日到1991年5月1日之间雇用的雇员名,岗位及雇佣日期,并以雇佣日期进行排序

SELECT ename,job,hiredate FROM emp
			WHERE hiredate BETWEEN '1991-02-01' AND '1991-05-01'
						ORDER BY hiredate ASC;

9.显示获得补助的所有雇员名,工资及补助,并以工资降序排序

SELECT ename,sal,comm FROM emp
			WHERE comm IS NOT NULL 
						ORDER BY sal DESC;

10.选择部门30中的所有员工

SELECT *FROM emp 
			WHERE deptno = 30;

11.列出所有办事员(CLERK)的姓名,编号和部门编号

SELECT ename,empno,deptno FROM emp	
			WHERE job = 'CLERK';

12.找出红利高于薪资的员工

SELECT *FROM emp
			WHERE IFNULL(comm,0) > sal;

13.找出红利高于薪资60%的员工

SELECT *FROM emp
			WHERE IFNULL(comm,0) > sal*0.6;

14.找出部门10中所有经理(MANAGER)和部门20中所有办事员(CLERK)的详细资料

SELECT *from emp
			WHERE (deptno = 10 AND job = 'MANAGER') OR (deptno = 20 AND job = 'CLERK');

15.找出部门10中所有经理(MANAGER),部门20中所有办事员(CLERK,还有既不是经理又不是办事员,但其薪金大于或等于2000的所有员工的详细资料.

SELECT *from emp
			WHERE (deptno = 10 AND job = 'MANAGER') OR (deptno = 20 AND job = 'CLERK')
								OR job NOT IN('MANAGER','CLERK') AND sal >= 2000;

16.找出收取红利的员工的不同工作

SELECT DISTINCT job FROM emp
			WHERE comm IS NOT NULL;

17.找出不收取红利或收取的红利低于100的员工

SELECT *FROM emp
			WHERE comm IS NULL OR IFNULL(comm,0) < 100;

18.找出各月倒数第3天受雇的所有员工(last_day(日期),可以返回该日期月份的最后一天)

SELECT *FROM emp 
			WHERE LAST_DAY(hiredate) - 2 = hiredate;

19.找出早于12年前受雇的员工

SELECT *FROM emp			
			WHERE DATE_ADD(hiredate,INTERVAL 12 YEAR) < NOW();

20.以首字母小写的方式显示所有员工的姓名

SELECT CONCAT(LCASE(LEFT(ename,1)),SUBSTRING(ename,2,LENGTH(ename)-1)) FROM emp;

21.显示正好为5个字符的员工的姓名.

SELECT *FROM emp
			WHERE LENGTH(ename) = 5;

22.显示不带有"R"的员工的姓名

SELECT *FROM emp
		WHERE ename NOT LIKE '%R%';

23.显示所有员工姓名的前三个字符

SELECT LEFT(ename,3)FROM emp;

24.显示所有员工的姓名,用a替换所有"A"

SELECT REPLACE(ename,'A','a') FROM emp;

25.显示满10年服务年限的员工的姓名和受雇日期

SELECT ename,hiredate FROM emp			
			WHERE DATE_ADD(hiredate,INTERVAL 10 YEAR) <= NOW();

26.显示员工的详细资料,按姓名排序

SELECT *FROM emp	
			ORDER BY ename ASC;

27.显示员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面

SELECT ename,hiredate FROM emp
			ORDER BY hiredate ASC;

28.显示所有员工的姓名、工作和薪金按工作降序排序,若工作相同则按薪金排序

SELECT ename,job,sal FROM emp
			ORDER BY job DESC,sal ASC;

29.显示所有员工的姓名、加入公司的年份和月份按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面

SELECT ename,YEAR(hiredate) AS '年',MONTH(hiredate) AS '月' FROM emp
			ORDER BY MONTH(hiredate) ASC,hiredate ASC;

30.显示在一个月为30天的情况所有员工的日薪金,忽略余数

SELECT ename,FLOOR(sal/30) AS '日薪金' FROM emp;

31.找出在(任何年份的)2月受聘的所有员工

SELECT *FROM emp  
			WHERE MONTH(hiredate) = 2;

32.以年月日的方式显示所有员工的服务年限(大概)

SELECT ename,FLOOR(DATEDIFF(NOW(),hiredate)/365) AS '年',
				FLOOR(DATEDIFF(NOW(),hiredate)%365/30) AS '月', 
				FLOOR(DATEDIFF(NOW(),hiredate)%365%30) AS '日'FROM emp;

33.列出至少有一个员工的所有部门

SELECT COUNT(*) AS '员工数' FROM emp
		 GROUP BY deptno
			HAVING 员工数 > 1;

34.列出薪金比“SMITH”多的所有员工

SELECT *FROM emp
			WHERE sal > (SELECT sal FROM emp WHERE ename = 'SMITH');

35.列出受雇日期晚于其直接上级的所有员工

# 自连接
SELECT *FROM emp worker,emp boss
			WHERE worker.mgr = boss.empno AND worker.hiredate > boss.hiredate;

36.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门

# 外连接
SELECT dname,emp.* FROM dept
			LEFT JOIN emp ON dept.deptno = emp.deptno

37.列出所有“CLERK”(办事员)的姓名及其部门名称

SELECT ename,dname FROM emp,dept 
			WHERE emp.deptno = dept.deptno AND job = "CLERK"

38.列出各个部门最低薪金大于1500的各种工作

SELECT *FROM emp
			GROUP BY job
			HAVING MIN(sal) > 1500

39.列出在部门“SALES”(销售部)工作的员工的姓名

SELECT ename FROM emp,dept 
			WHERE emp.deptno = dept.deptno AND dname = "SALES"

40.列出薪金高于公司平均薪金的所有员工

SELECT *FROM emp	
			WHERE sal > (SELECT AVG(sal) AS avg_sal FROM emp)

41.列出与“SCOTT”从事相同工作的所有员工

SELECT *FROM emp
			WHERE job = (SELECT job FROM emp WHERE ename = "SCOTT")

42.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金

SELECT ename,sal FROM emp
			WHERE sal > (SELECT MAX(sal) FROM emp WHERE deptno = 30)

43.列出在每个部门工作的员工数量、平均工资

SELECT deptno,COUNT(*) AS "员工数量",AVG(sal+IFNULL(comm,0)) AS "平均工资" FROM emp
			GROUP BY deptno

44.列出所有部门的详细信息和部门人数

SELECT dept.*,sum FROM dept,(SELECT deptno,COUNT(*) AS sum FROM emp GROUP BY deptno) temp 
			WHERE temp.deptno = dept.deptno

45.列出各种工作的最低工资

SELECT job,MIN(sal) AS min_sal FROM emp
      GROUP BY job 

46.列出MANAGER(经理)的最低薪金

SELECT job,MIN(sal) AS  min_sal FROM emp
			WHERE job = "MANAGER"

47.列出所有员工的年工资,按年薪从低到高排序

SELECT ename,(sal+IFNULL(comm,0))*12 AS sum_sal FROM emp
			ORDER BY sum_sal

综合题目

48.设学校环境如下:一个系有若干个专业,每个专业一年只招一个班,每个班有若干个学生。
现要建立关于系、学生、班级的数据库,关系模式为:
班CLASS (班号classid,专业名subject,系名deptname,入学年份enrolltime,人数num)
学生STUDENT (学号studentid,姓名name,年龄age,班号classid)
系DEPARTMENT (系号departmentid,系名deptname)
试用SQL语言完成以下功能:

(1)建表,在定义中要求声明:

  • 每个表的主外键
  • deptname是唯一约束
  • 学生姓名不能为空

(2)插入如下数据

DEPARTMENT (001,数学;002,计算机:003,化学;004,中文:005,经济)

CREATE TABLE DEPARTMENT(
	departmentid INT PRIMARY KEY, -- 系号
    deptname VARCHAR(32) UNIQUE -- 系名
)
START TRANSACTION
INSERT INTO DEPARTMENT VALUES (001,"数学"),
						    (002,"计算机"),
						    (003,"化学"),
						    (004,"中文"),
						    (005,"经济");

CLASS(
101,软件,计算机,1995,20;
102,微电子,计算机,1996,30:
111,无机化学,化学,1995,29;
112,高分子化学,化学,1996,25;
121,统计数学,数学,1995,20;
131,现代语言,中文,1996,30;
141,国际贸易,经济,1997,30;
142,国际金融,经济,1996,14;)

CREATE TABLE CLASS(
		classid INT PRIMARY KEY, -- 班号
		`subject` VARCHAR(32), -- 专业名
		deptname VARCHAR(32), -- 系名
		enrolltime YEAR, -- 入学年份
		num INT, -- 人数
		FOREIGN KEY(deptname) REFERENCES `DEPARTMENT`(deptname)
)
INSERT INTO CLASS VALUES(101,"软件","计算机","1995",20),
                        (102,"微电子","计算机","1996",30),
                        (111,"无机化学","化学","1995",29),
                        (112,"高分子化学","化学","1996",25),
                        (121,"统计数学","数学",<

以上是关于数据库练习的主要内容,如果未能解决你的问题,请参考以下文章

018.查询练习50题(sql实例)

Oracle的sql语句上级练习和答案

sql 语句 在所有部门中查询薪金最高或最低的雇员的信息

Oracle的sql语句上机练习和答案

尚观干货Oracle数据库查询命令

oracle复杂查询的练习题