📘本专栏文章主要是争对博主在学习《数据库原理及应用》课程时老师布置的课后练习题做出的解答,如果遇到相同的题目建议看着本文自己动手实践一遍,这样才能加深印象。
🎈如果想要了解数据库有关知识点的话,可以点击下方链接进行查看
🔑 本专栏地址【数据库练习题】
🔑有关数据库的知识点请戳⏩【数据库知识点总结】
🔑 另一个正在更新的专栏【拼图小游戏(Java)】
本文包含九道多表查询相关练习题,有一些难度较大,建议读者在复习完前面数据库的内容后再来阅读本文
EMP
E# | ENAME | AGE | SEX | ECITY |
---|---|---|---|---|
0001 | 李华 | 58 | 男 | 南京 |
0002 | 李丽 | 23 | 女 | 上海 |
0003 | 王建国 | 52 | 男 | 温州 |
0004 | 陈颖 | 32 | 女 | 北京 |
0005 | 刘晓华 | 29 | 男 | 上海 |
0006 | 韩梅梅 | 42 | 女 | 安徽 |
WORKS
E# | C# | SALARY |
---|---|---|
0001 | C001 | 2500 |
0002 | C001 | 3500 |
0003 | C002 | 2500 |
0004 | C002 | 7500 |
0006 | C002 | 1300 |
0003 | C003 | 1100 |
0005 | C003 | 6200 |
0006 | C001 | 1200 |
COMP
C# | CNAME | CITY |
---|---|---|
C001 | 联华公司 | 上海 |
C002 | 某公司 | 深圳 |
C003 | 学而思 | 上海 |
简单的查询,这里不多加赘述,直接上语句
SELECT E#,ENAME FROM EMP WHERE AGE > 50 AND SEX = '男';
EMP
表中检索E#
和ENAME
WORKS
检索工资超过1000元的员工且性别为男(子查询)SELECT EMP.E#,ENAME FROM EMP WHERE E# IN (SELECT WORKS.E# FROM WORKS WHERE SALARY > 1000) AND SEX = '男';
SELECT E#,ENAME FROM EMP WHERE EMP.E# IN(SELECT DISTINCT E# FROM WORKS WHERE C# = 'C002' OR C# = 'C003' )
EMP.E# = WORKS.E# AND WORKS.C# = COMP.C#
COMP.CNAME = '联华公司'
、WORKS.SALARY > 1000
、EMP.SEX = '男'
SELECT EMP.E#,EMP.ENAME FROM EMP,WORKS,COMP WHERE EMP.E# = WORKS.E# AND WORKS.C# = COMP.C# AND COMP.CNAME = '联华公司' AND WORKS.SALARY > 1000 AND EMP.SEX = '男';
这道题是简单的查询,涉及到了起别名和聚合函数的知识点
SELECT E#,COUNT(C#),SUM(SALARY) AS SUM.SALARY FROM WORKERS GROUP BY E#;
SELECT DISTINCT WORKS.E# FROM WORKS WHERE C# IN (SELECT DISTINCT WORKS WHERE E# = '0002')
EMP.E# = WORKS.E# AND WORKS.C# = COMP.C#
SELECT AVG(SALARY) FROM WORKS WHERE C# IN (SELECT C# FROM COMP WHERE CNAME = '联华公司')
SELECT EMP.E#,ENAME FROM EMP,COMP,WORKS
WHERE EMP.E# = WORKS.E# AND WORKS.C# = COMP.C#
AND CNAME = '联华公司'
AND SALARY < (SELECT AVG(SALARY) FROM WORKS WHERE C# IN (SELECT C# FROM COMP WHERE CNAME = '联华公司'));
UPDATE WORKS SET SALARY = SALARY + 100 WHERE E# IN (SELECT E# FROM EMP WHERE EMP.AGE > 50)
如果有任何问题欢迎在评论区留言评论