sql语句—实例及答案
(完整版)sql语句练习题及答案

(完整版)sql语句练习题及答案⼀在数据库 school 中建⽴student , sc, course 表。
学⽣表、课程表、选课表属于数据库School ,其各⾃的数据结构如下:学⽣Student (Sno,Sname,Ssex,Sage,Sdept)课程表course(Cno,Cname,Cpno,Ccredit)学⽣选课SC(Sno,Cno,Grade)⼆设定主码1 Student表的主码:sno2 Course表的主码:cno3 Sc表的主码:sno,cno1写出使⽤ Create Table 语句创建表 student , sc, course 的SQL语句23 删除student表中的元组4在数据库school中删除关系student5在student表添加属性sbirthdate 类型datetimeDelete1 删除所有JSJ 系的男⽣delete from Student where Sdept=’JSJ’ and Ssex=’男’;2 删除“数据库原理”的课的选课纪录delete from SC where Cno in (select Cno fromCourse where Cname=’数据库原理’);Update1 修改0001 学⽣的系科为: JSJ2 把陈⼩明的年龄加1岁,性别改为⼥。
2 修改李⽂庆的1001课程的成绩为93 分3 把“数据库原理”课的成绩减去1分Select 查询语句⼀单表1查询年龄在19⾄21岁之间的⼥⽣的学号,姓名,年龄,按年龄从⼤到⼩排列。
2查询姓名中第2个字为“明”字的学⽣学号、性别。
3查询 1001课程没有成绩的学⽣学号、课程号4查询JSJ 、SX、WL 系的年龄⼤于25岁的学⽣学号,姓名,结果按系排列5按10分制查询学⽣的sno,cno,10分制成绩(1-10分为1 ,11-20分为2 ,30-39分为3,。
90-100为10)6查询 student 表中的学⽣共分布在那⼏个系中。
数据库常用SQL语句练习(含答案)

Emp(员工表)和Dept(部门表)简单题1. 案例:查询没有上级领导的员工的编号,姓名,工资select empno,ename,sal from emp where mgr is null;2. 案例:查询emp表中没有奖金的员工的姓名,职位,工资,以及奖金select ename,job,sal,comm from emp where comm is null or comm=0;既可以是null,也可以是03. 案例:查询emp表中含有奖金的员工的编号,姓名,职位,以及奖金select ename,job,sal,comm from emp where comm>0;4. 案例:查询含有上级领导的员工的姓名,工资以及上级领导的编号select ename,sal,mgr from emp where mgr is not null;5. 案例:查询emp表中名字以‘S’开头的所有员工的姓名select ename from emp where ename like 's%'6. 案例:查询emp表中名字的最后一个字符是'S'的员工的姓名select ename from emp where ename like '%s'7. 案例:查询倒数的第2个字符是‘E’的员工的姓名select ename from emp where ename like '%e_'8. 案例:查询emp表中员工的倒数第3个字符是‘N’的员工姓名select ename from emp where ename like '%n__'9. 案例:查询emp表中员工的名字中包含‘A’的员工的姓名select ename from emp where ename like '%a%'10. 案例:查询emp表中名字不是以'K'开头的员工的所有信息select * from emp where ename not like 'k%';11. 案例:查询emp表中名字中不包含‘A’的所有员工的信息select * from emp where ename not like '%a%'12. 案例:做文员的员工人数(job= CLERK 的)select count(*) from emp where job='clerk';13. 案例:销售人员job: SALESMAN 的最高薪水select max(sal) from emp where job='salesman';14. 案例:最早和最晚入职时间select min(hiredate),max(hiredate) from emp;15. 案例:查询emp表中员工的编号,姓名,职位,工资,并且工资在1000~2000之间。
SQL四个基本语句练习及参考答案

ACCESS数据表结构:Class:班级信息表cour_cla_tea:课程—班级—教师对应关系表。
就是一门课程有什么班上由哪位老师任教course:课程信息表stu_mark:学生的成绩记录student:学生信息表teacher:教师信息表查询select:单表查询:1、查找显示所有管理员和老师的基本信息2、查找显示所有老师的基本信息3、查找显示“郭襄”老师的所有基本信息4、查找显示“古龙”管理员的密码5、查找显示“第二学期”中成绩不及格的信息多表查询:1、查找显示“周伯通”同学的所有课程成绩信息2、查找显示“段誉”同学不及格的课程及其分数3、查找显示有上“计算机通讯与网络”这门课程所有班别4、查找显示“01数本(2)班”所有的学生名单5、查找显示“01电本(1)班”的所有课程名称6、查找显示“计算机科学与技术”专业的所有学生7、查找显示“数据库原理”课程前10名的学生成绩,学生名称,并以成绩的降序排列8、查找显示“郭襄”老师担任的所有课程名称9、查找显示“01数本(1)班”所有不及格的课程及对应学生名单10、查找显示上“教育学”这门课的所有学生名单,学期数及其分数11、查找显示“莫声谷”老师教的所有班级名称及其对应专业名称其他查询:1、查询所有姓王的学生基本资料2、查询学习课程“软件工程”中,分数最高和分数最低的学生名字以及分数3、计算出“01数本(2)班”班学生学习“操作系统”课程的平均分4、计算出“王语嫣”同学“第二学期”的各们课程成绩总分与平均分更新update:1、将“郭襄”老师的密码改为“010012”2、把“王重阳”同学调到“01计本(1)班”班3、把“李莫愁”同学所有不及格的课程分数改为60分添加insert:1、在student添加一个学号为“ST01260113”,名字为“张三”,初始密码为“260113”,班别为“CL012601”的一个学生记录删除:1、删除“王重阳”同学的所有数据记录查询select:单表查询:1、查找显示所有管理员和老师的基本信息select * from teacher2、查找显示所有老师的基本信息select * from teacher where user_level='teacher'3、查找显示“郭襄”老师的所有基本信息select * from teacher where teach_name='郭襄'4、查找显示“古龙”管理员的密码select teach_pwd from teacher where teach_name='古龙'5、查找显示“第二学期”中成绩不及格的信息select * from stu_mark where year_term='第二学期' and mark<60多表查询:1、查找显示“周伯通”同学的所有课程成绩信息(三个表连接查询)select stu_name,course_name,mark from student,stu_mark,course where student.stu_name='周伯通' and student.stu_id=stu_mark.stu_id and stu_mark.course_id=course.course_id2、查找显示“段誉”同学不及格的课程及其分数(三个表连接查询)select stu_name,course_name,mark from student,stu_mark,course where student.stu_name='段誉' and mark<60 and student.stu_id=stu_mark.stu_id and stu_mark.course_id=course.course_id3、查找显示有上“计算机通讯与网络”这门课程所有班别(三个表连接查询)select course_name,class_name from course,class,cour_cla_tea where course_name='计算机通讯与网络' and course.course_id=cour_cla_tea.course_id and cour_cla_tea.class_id=class.class_id 4、查找显示“01数本(2)班”所有的学生名单(两个表连接查询)select class_name,stu_name from class,student where class.class_id=student.class_id and class_name='01数本(2)班'5、查找显示“01电本(1)班”的所有课程名称(三个表连接查询)select class_name,course_name from class,course,cour_cla_tea where class_name='01电本(1)班' and course.course_id=cour_cla_tea.course_id and cour_cla_tea.class_id=class.class_id6、查找显示“计算机科学与技术”专业的所有学生(两个表连接查询)select speciality_name,stu_name from class,student where speciality_name='计算机科学与技术' and class.class_id=student.class_id7、查找显示“数据库原理”课程前5名的学生成绩,学生名称,并以成绩的降序排列(三个表连接查询)select top 5 course_name,stu_name,mark from student,course,stu_mark where course_name='数据库原理' and course.course_id=stu_mark.course_id and stu_mark.stu_id=student.stu_id order by mark desc8、查找显示“郭襄”老师担任的所有课程名称(三个表连接查询)select distinct teach_name,course_name from course,teacher,cour_cla_tea where teach_name='郭襄' and teacher.teach_id=cour_cla_tea.teach_id and cour_cla_tea.course_id=course.course_id9、查找显示“01数本(1)班”所有不及格的课程及对应学生名单(四个表连接查询)select class_name,course_name,stu_name,mark from class,stu_mark,student,course where class_name='01数本(1)班' and class.class_id=student.class_id and student.stu_id=stu_mark.stu_id and stu_mark.course_id=course.course_name and mark<6010、查找显示上“教育学”这门课的所有学生名单,学期数及其分数(三个表连接查询) select course_name,stu_name,year_term,mark from course,student,stu_mark where course_name='教育学' and course.course_id=stu_mark.course_id and stu_mark.stu_id=student.stu_id10、查找显示“莫声谷”老师教的所有班级名称及其对应专业名称(三个表连接查询) select teach_name,class_name, speciality_name from teacher,cour_cla_tea,class where teach_name='莫声谷' and teacher.teach_id=cour_cla_tea.teach_id andcour_cla_tea.class_id=class.class_id其他查询:1、查询所有姓王的学生基本资料select * from student where stu_name like ‘王*’2、查询学习课程“软件工程”中,分数最高和分数最低的学生名字以及分数分数最高:select top 1 course_name,stu_name,mark as score from student,stu_mark,course where course_name='软件工程' and stu_mark.course_id=course.course_id and stu_mark.stu_id=student.stu_id order by mark desc分数最低:select top 1 course_name,stu_name,mark as score from student,stu_mark,course where course_name='软件工程' and stu_mark.course_id=course.course_id and stu_mark.stu_id=student.stu_id order by mark asc3、计算出“01数本(2)班”班学生学习“操作系统”课程的平均分select class_name,course_name,avg(mark) as 平均分from class,stu_mark,course,student where class.class_id=student.class_id and student.stu_id=stu_mark.stu_id and stu_mark.course_id=course.course_id and class_name='01数本(2)班' and course_name='操作系统' group by class_name,course_name4、计算出“王语嫣”同学“第二学期”的各门课程成绩总分与平均分select stu_name,year_term,sum(mark) as 总分,avg(mark) as 平均分from student,stu_mark where stu_name='王语嫣' and year_term='第二学期' and student.stu_id=stu_mark.stu_id group by stu_name,year_term更新update:1、将“郭襄”老师的密码改为“010012”update teacher set teach_pwd='010012' where teach_name='郭襄'2、把“王重阳”同学调到“01计本(1)班”班update student set class_id='CL012401'(注:这一题中,无法直接用一条语句达到目的,而是要先在class表中查出“01计本(1)班”的班级ID号CL012401)3、把“李莫愁”同学所有不及格的课程分数改为60分update stu_mark set mark=60 where mark<60 and stu_id in (select stu_id from student where stu_name='李莫愁')添加insert:在student添加一个学号为“ST01260113”,名字为“张三”,初始密码为“260113”,班别为“CL012601”的一个学生记录insert into student(stu_id,stu_name,stu_pwd) values(' ST01260113', '张三', '260113')删除:删除“王重阳”同学的所有数据记录delete from stu_mark where stu_id in (select stu_id from student where stu_name='王重阳') delete from student where stu_name='王重阳'(注:第一句先删除该学生的成绩记录,第二句删除该学生的基本信息记录。
数据库SQL语句练习题含答案

SQL语句练习练习5. 1. 2: 基于不断滚动的电影数据库的实例:Movie( 电影名title,拍摄年代year ,长度length, inColor ,制片厂名studioName, 制片人证书号producerC# )StarsIn(电影名movieTitle,拍摄年份movieYear ,影星名starName) MovieStar (姓名name,地址address,性别gender ,生日birthdate) MovieExec( 姓名name,地址address ,证书号cert # ,净资产netWorth ) Studio(姓名name, 地址address,总裁证书号presC# )请写出下列SQL 查询语句:* ( a) 找出米高梅制片公司(MGM studios)的地址。
SELECT address AS Studio_AddressFROM Studio WHERE name = 'MGM';( b) 找出桑德拉·布洛克( Sandra Bullock ) 的出生日期( birthdate) 。
SELECT birthdate AS Star_BirthdateFROM MovieStar WHERE name = 'Sandra Bullock';* ( c) 找出在1980 年拍摄过电影的所有影星, 或者拍摄过电影名中含有“Love”的电影的所有影星。
SELECT starName FROM StarsInWHERE movieYear = 1980 OR movieTitle LIKE '%Love%';( d) 找出净资产至少1 000 万美元的所有行政长官。
SELECT name AS Exec_NameFROM MovieExec WHERE netWorth >= 10000000;( e) 找出所有的男影星或者是住址中含有Malibu 的影星。
SQL语句练习及参考答案

SQL语句练习及参考答案SQL 语句练习1.设学⽣选课数据库有关系S (sno,sname,age,sex )、SC (sno,cno,grade )和C(cno,cname,teacher ),分别表⽰学⽣、选课和课程,sno 代表学号,sname 代表学⽣姓名,age 代表年龄,sex 代表性别,grade 代表成绩,cno 代表课程号,teacher 代表任课教师。
试完成表⽰下列查询。
(1)检索年龄⼤于21的男学⽣学号(sno)和姓名(sname)。
(2)建⽴性别只能为“男”、“⼥”的约束。
(3)创建⼀个视图v1,该视图⽤来查询学⽣的选课情况,要求包含:学⽣姓名(sname),课程名(cname),任课教师teacher 和成绩grade 。
(4)检索选修课程号为k1和k5的学⽣学号(sno)。
(5)检索全部学⽣都选修的课程的课程号(cno)和课程名(cname)。
(6)删除所有男同学的选课记录。
1.(1)select sno,snae from s where sex=’男’ and age>21 (2)alter table s add constraint c1 check sex in (‘男’,’⼥’)(3)create view v1 as select sname,cname,teacher,grade from s,sc,c where s.sno=sc.sno and/doc/62176c89fe4733687e21aaca.html o=/doc/62176c89fe4733687e21aaca.html o (4)select sno fromsc sc1 where cno=’k1’ and exists (se lect * fromsc sc2 where sc1.sno =sc2.sno and/doc/62176c89fe4733687e21aaca.html o=’k5’)(5)select cno,cnam from c where not exists (select * from s where not exists (select * from sc where/doc/62176c89fe4733687e21aaca.html o=/doc/62176c89fe4733687e21aaca.html o and s.sno=sc.sno)(6)delete from sc where sno in (select sno from s where sex=’男’)或delete sc from sc,s where s.sno=sc.sno and sex=’男’2.设图书借阅数据库有关系图书(图书编号,书名,作者,出版社,出版时间,图书类别)、读者(读者编号,姓名,读者类别)和借阅(读者编号, 图书编号, 借阅⽇期,还期)。
SQL语句(有答案)

一. SQL语句【1】(C226)下列关于SQL语言特点的描述中,错误的是( )。
A)语言非常简洁B)是一种一体化语言C)是一种高度过程化的语D)可以直接以命令方式交互使用,也可以程序方式使用【2】1103(7)负责数据库中查询操作的数据库语言是A)数据定义语言B)数据管理语言C)数据操作语言D)数据控制语言【3】Y3(18)SQL语言的核心是( )。
A)数据操纵B)数据定义C)数据查询D)数据定义【4】Y1(24)VisualFoxPro在SQL方面,不支持的功能是( )。
A)数据控制B)数据操纵C)数据查询D)数据定义【5】Y4(30)下列选项中,不属于SQL特殊运算符的是( )。
A)GROUP B)ON C)ALL D)EMPTY二. SQL查询语句【1】0904(12)SQL语句的查询语句是A) INSERT B)UPDATE C) DELETE D) SELECT【2】(C126)标准的SQL基本查询语句的格式是( )。
A)SELECT…FROM…WHERE B)SELECT…WHERE…FROM C)SELECT…WHERE…GROUP BY D)SELECT…FROM…ORDER BY 【3】Y3 (25)SQL语句中,SELECT语句中的JOIN是用来建立表间的联系短语应放在下列哪个短语之后( )。
A)FROM B)WHERE C)ON D)GROUP BY【4】Y2(20)SQL语句中,SELECT命令中的JOIN是用来建立表间的联系短语,连接条件应出现在下列哪个短语中A)WHERE B)ON C)HAVING D)INNER【5】Y4(17)SQL SELECT语句中的WHERE用于说明( )。
A)查询数据B)查询条件C)查询分组D)查询排序【6】Y4(18)SQL语句可以进行多个查询的嵌套,但Visual FoxPro中只支持( )层嵌套。
A)1 B)2 C)3 D)无穷【7】(C128)在Visual FoxPro中,嵌套查询是基于( )的查询A)2个关系B)3个关系C)多个关系D)2个或3个关系【8】0704 (17)以下有关SELECT语句的叙述中错误的是A)SELECT语句中可以使用别名B)SELECT语句中只能包含表中的列及其构成的表达式C)SELECT语句规定了结果集中的顺序D)如果FROM短语引用的两个表有同名的列,则SELECT短语引用它们时必须使用表名前缀加以限定【9】0504 (31) 在Visual FoxPro中,以下有关SQL的SELECT语句的叙述中,错误的是 ( )。
50条数据库sql语句及答案

select teaid from tblTeacher where teaname = '叶平')))
--练习
select stuid from tblScore where courseid in (
--11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
------运用连接查询
SELECT DISTINCT ST.* FROM tblStudent ST, tblScore SC WHERE ST.STUID = SC.STUID
AND SC.COURSEID IN
select stuid from tblScore GROUP BY STUID HAVING max(SCORE)<60)
--10、查询没有学全所有课的同学的学号、姓名;
--练习
SELECT TS.STUID,TS.STUNAME,COUNT(TSC.SCORE) FROM tblStudent TS
TC.COURSEID INNER JOIN tblTeacher TT ON TC.TEAID = TT.TEAID
WHERE TT.TEANAME ='叶平')
--对
SELECT STUID, STUNAME FROM tblStudent WHERE STUID NOT IN(
where A.SCORE > B.SCORE
--老师讲的方法
SELECT T.* FROM tblScore T WHERE T.COURSEID = 1 AND
高级SQL语句查询(含答案和截图)

C顾客cidcnamecity discntc001 李广天津10.00c002 王开基北京12.00c003 安利德北京8.00c004 曹士雄天津8.00c006 曹士雄广州0.00P商品pidpname city quantity pricep01 梳子天津111400 0.50p02 刷子成都203000 0.50p03 刀片西安150600 1.00p04 钢笔西安125300 1.00p05 铅笔天津221400 1.00p06 文件夹天津123100 2.00p07 盒子成都100500 1.00A代理aidanamecity percenta01 史可然北京 6a02 韩利利上海 6a03 杜不朗成都7a04 甘瑞北京 6a05 敖斯群武汉 5a06 史可然天津 5O订单ordnomonthcidaid pidqtydollars1011 01 c001 a01 p01 1000 450.00 1012 01 c001 a01 p01 1000 450.00 1019 02 c001 a02 p02 400 180.00 1017 02 c001 a06 p03 600 540.00 1018 02 c001 a03 p04 600 540.00 1023 03 c001 a04 p05 500 450.00 1022 03 c001 a05 p06 400 720.00 1025 04 c001 a05 p07 800 720.001013 01 c002 a03 p03 1000 880.001026 05 c002 a05 p03 800 704.001 查询所有定购了至少一个价值为0.50的商品的顾客的名字。
amefrom clientwherecid in (select cid from order1 where pid in(select pid from product where price='0.5'))2 找出全部没有在代理商a03处订购商品的顾客cid值。
sql查询举例(含答案)

查询练习一、简单查询(无条件查询):1、查询“学生档案”表中所有的记录SELECT * FORM 学生档案2、查询“学生档案”表中全体学生的姓名、学号、家庭地址SELECT 姓名, 学号, 家庭地址 FROM 学生档案二、有条件查询1、查询“成绩管理”表中语文成绩在80分以下的学生的学号。
SELECT 学号 FROM 成绩管理 WHERE 语文<802、查询“成绩管理”表中语文成绩在80分到90分之间的学生的学号,语文,数学,英语成绩。
SELECT 学号,语文,数学,英语FROM成绩管理WHERE 语文 >= 80 AND 语文<=90==(语文 BETWEEN 80 AND 90)3、查询“成绩管理”表中数学成绩不在75分到85分之间的学生的学号,语文,数学,英语成绩。
SELECT 学号,语文,数学,英语FROM 成绩管理WHERE 数学 NOT BETWEEN 75 AND 854、查询“学生档案”表中李成刚,刘艺梅,郑莉三名学生的信息。
SELECT *FROM 学生档案WHERE 姓名 IN (“李成刚”,“刘艺梅”,“郑莉”)==(姓名 =“李成刚” OR 姓名=“刘艺梅” OR 姓名=“郑莉”)5、查询“学生档案”表中所有姓张的学生的姓名、学号和性别SELECT 姓名,学号,性别 FROM学生档案WHERE 姓名 LIKE “张*”6、查询“学生档案”表中所有姓张且全名为三个汉字的学生的姓名SELECT 姓名FROM 学生档案WHERE姓名 LIKE “张??”7、查询“学生档案”表中第二个字符为“建”字的学生的学号和姓名SELECT 学号,姓名FROM 学生档案WHERE姓名 LIKE “?建*”8、查询“学生档案”表中家庭住址为“人民路”和“育才路”的学生学号,姓名,性别和家庭住址。
SELECT 学号,姓名,性别,家庭住址FROM 学生档案WHERE家庭住址 LIKE “人民路*” OR家庭住址 LIKE “育才路*”9、查询“学生档案”表中所有团员的学生班级和姓名。
SQL典型实例及答案.doc

SQL查询习题一、对于教学数据库的三个基本表S (S#, SNAME, AGE, SEX)SC(S#, C#, GRADE)C(C#, CNAME, TEACHER)试用SQL的查询语句表达下列查询:检索LIU老师所授课程的课程号和课程名。
检索年龄大于23岁的男学生的学号和姓名。
检索学号为S3学生所学课程的课程名与任课教师名。
检索至少选修LIU老师所授课程中一门课程的女学生姓名。
检索WANG同学不学的课程的课程号。
检索至少选修两门课程的学生学号。
检索全部学生都选修的课程的课程号与课程名。
检索选修课程包含LIU老师所授课程的学生学号。
二、对于教学数据库的三个基本表S (S#, SNAME, AGE, SEX)SC(S#, C#, GRADE)C(C#, CNAME, TEACHER)试用SQL的查询语何表达下列查询:在表C中统计开设课程的教师人数。
求选修C4课程的女学生的平均年龄。
求LIU老师所授课程的每门课程的平均成绩。
统计每个学生选修课程的门数(超过5门的学生才统计)。
要求输出学生学号和选修门数,查询结果按门数降序排列,若门数相同,按学号升序排列。
检索学号比WANG同学大,而年龄比他小的学生姓名。
在表SC中检索成绩为空值的学生学号和课程号。
检索姓名以L打头的所有学生的姓名和年龄。
求年龄大于女同学平均年龄的男学生姓名和年龄。
求年龄大于所有女同学年龄的男学生姓名和年龄。
参考答案作业一:1.select C#,CNAMEfrom Cwhere teacher='LIU f2.select S# ,snamefrom Swhere age>23 and sex—男'3.select cname,teacherfrom SC,Cwhere SC.C#=C.C# and SC.S#=,S3'4.select distinct SNAMEfrom S,SC,Cwhere SC.S#=S.S# and SC.C#=C.C# and sex—女'and teacher=,LIU'5.select C#from Cwhere not exists(select *from SC,Swhere SC.C#=C.C# and SC.S#=S.S# and S.sname=*WANG')6.select distinct S#from SC X, SC Ywhere X.S#=Y.S# and X.C# != Y.C#7.SELECT C#, cnameFROMCWHERE NOT EXISTS(SELECT *FROMSWHERE NOT EXISTSSELECT *FROM SCWHERE SC.S# 二S.S# AND SC.C# = C.C# )&解法一SELECT S#FROM SCWHERE EXISTS(SELECT *FROMCWHERE SC.C# = C.C# AND teacher = 'LIU')解法二SELECT S#FROM SC,CWHERE SC.C# = C.C# and teacher = 'LIU'作业二1.SELECT COUNT DISTINCT teacherFROMC2.select avg(age)from SC,Swhere SC.S#=S.S# and sex=*女 and C#='c4'3.select AME, avg(grade)from SC,Cwhere SC.C#=C.C# and teacher='LIU*group by C.C#,AME4.SELECT S#, COUNT(*)FROM SCGROUPBYS#HAVING COUNT(*) > 5ORDER BY COUNT(*) DESC, S#5.select snamefrom Swhere s# >(select S#from Swhere sname='WANG'and age<(select agefrom Swhere sname='WANG')6.SELECT S#,C#from SCwhere grade is NULL7.select sname,agefrom Swhere sname like'L%*8.select sname,agefrom Swhere sex—男' and age>(select avg(age)from Swhere sex二女)9.select sname,agefrom Swhere sex='男'and age>(select max(age)from Swhere sex='女。
SQL查询语句考试及答案

一、聚合函数练习1、统计<学生信息表>,统计共有多少个学生Select count (*) as 学生数量from A_studentinfo2、统计<学生信息表>,统计年龄大于20岁的学生有多少个Select count(*) as 学生数量from A_studentinfowhere (2008-yearofbirth)>203、统计<学生信息表>,统计入学时间在1980年至1982年的学生人数select count(*) as 学生数量from A_studentinfowhere enrollment between '1998-01-01' and '2003-12-30'对比以下查询方式,看看有何不同,为什么?select count(*) as 学生数量from A_studentinfowhere enrollment between '1998' and '2003'4、统计<学生选修信息表>,统计学号为"S001"的学生的平均成绩Select avg(score) as 平均成绩from A_studentcoursewhere sno='S001'5、统计<学生选修信息表>,统计学号为"S001"的学生的总成绩select sum(score) as 总成绩from A_studentcoursewhere sno ='S001'6、统计<学生选修信息表>,查询课程号为”C001”的课程的最高成绩select max(score) as 最高成绩from A_studentcoursewhere cno='C001'7、统计<学生信息表>,查询所有学生中的最大年龄是多少select 2008-min(yearofbirth) as 最大年龄from A_studentinfo二、单表查询练习from A_studentinfowhere sname='张三'2、查询<学生信息表>,查询学生"张三"和”李四”的基本信息Select *from A_studentinfowhere sname='张三'or sname='李四'3、查询<学生信息表>,查询姓"张"学生的基本信息Select *from A_studentinfowhere sname like '张%'4、查询<学生信息表>,查询姓名中含有"四"字的学生的基本信息Select *from A_studentinfowhere sname like '%四%'5、查询<学生信息表>,查询姓名长度为三个字,姓“李”,且最后一个字是“强”的全部学生信息。
Oracle的sql语句练习题含答案

Oracle的sql语句练习题含答案--1、选择部门30中的雇员select * from emp where deptno=30;--2、列出所有办事员的姓名、编号和部门select ename,empno,dname from emp e inner join dept d on e.deptno = d.deptno where job=upper('clerk‟);--3、找出佣金高于薪金的雇员select * from emp where comm>sal;--4、找出佣金高于薪金60%的雇员select * from emp where comm>sal*0.6--5、找出部门10中所有经理和部门20中的所有办事员的详细资料select * from emp where (deptno=10 and job=upper('manager')) or (deptno=20 and job=upper('clerk '));--6、找出部门10中所有经理、部门20中所有办事员,既不是经理又不是办事员但其薪金>=2000的所有雇员的详细资料select * from emp where (deptno=10 and job=upper('manager')) or (deptno=20 and job=upper('clerk ')) or (job<>upper(…manager‟) and job<>upper(…clerk‟) and sal>=2000)--7、找出收取佣金的雇员的不同工作select distinct job from emp where comm>0;--8、找出不收取佣金或收取的佣金低于100的雇员select * from emp where nvl(comm,0)<100;--9、找出各月最后一天受雇的所有雇员select * from emp where hiredate= last_day(hiredate);--10、找出早于25年之前受雇的雇员select * from emp where months_between(sysdate,hiredate)/12>25;select * from emp where hiredate<ADD_MONTHS(SYSDATE,-12*25);< P>--11、显示只有首字母大写的所有雇员的姓名select ename from emp where ename=initcap(ename);--12、显示正好为6个字符的雇员姓名select ename from emp where length(ename)=6--13、显示不带有'R'的雇员姓名Select ename from emp where ename not like …%R%‟;Select ename from emp where instr(ename,‟R‟)=0;--14、显示所有雇员的姓名的前三个字符select substr(ename,1,3) from emp--15、显示所有雇员的姓名,用a替换所有'A'Select replace(ename,‟A‟,‟a‟) from emp--16、显示所有雇员的姓名以及满10年服务年限的日期Select ename,add_months(hiredate,12*10) …服务年限的日期‟ from e mp--17、显示雇员的详细资料,按姓名排序Select * from emp order by ename--18、显示雇员姓名,根据其服务年限,将最老的雇员排在最前面Select ename from emp order by hiredate--19、显示所有雇员的姓名、工作和薪金,按工作的降序顺序排序,而工作相同时按薪金升序Select ename,job,sal from emp order by job desc ,sal asc--20、显示所有雇员的姓名和加入公司的年份和月份,按雇员受雇日所在月排序,将最早年份的项目排在最前面select ename,to_char(hiredate,'yyyy'),to_char(hiredate,'mm') from emp order by hiredate asc--21、显示在一个月为30天的情况下所有雇员的日薪金select ename,sal/30 from emp;--22、找出在(任何年份的)2月受聘的所有雇员select * from emp where to_char(hiredate,'mm')='02';--23、对于每个雇员,显示其加入公司的天数select ename,sysdate-hiredate from emp--24、显示姓名字段的任何位置,包含"A" 的所有雇员的姓名select ename from emp where ename like '%A%';select ename from emp where instr(ename,‟A‟,1)>0;--25、以年、月和日显示所有雇员的服务年限Select months_between(sysdate,hiredate)/12 as “年”,months_between(sysdate,hiredate) as “月”, sysdate-hiredate as “日” from emp--1、列出至少有一个雇员的所有部门select distinct dname from dept where deptno in (select distinct deptno from emp);--2、列出薪金比"SMITH"多的所有雇员select ename,sal from emp where sal>(select sal from emp whereename=upper('smith'));--3、列出所有雇员的姓名及其直接上级的姓名select e.ename,m.ename from emp e,emp m where e.mgr=m.empno(+);--4、列出入职日期早于其直接上级的所有雇员select ename from emp e where hiredate<(select hiredate from emp where empno=e.mgr);--5、列出部门名称和这些部门的雇员,同时列出那些没有雇员的部门select dname,ename from dept d left join emp e on d.deptno=e.deptno;--6、列出所有“CLERK”(办事员)的姓名及其部门名称select ename,dname from emp e left join dept d on e.deptno=d.deptno wherejob=upper('clerk');--7、列出各种工作类别的最低薪金,显示最低薪金大于1500的记录select job,min(sal) from emp group by job having min(sal)>1500;--8、列出从事“SALES”(销售)工作的雇员的姓名,假定不知道销售部的部门编号select ename from emp where deptno = (select deptno from dept wheredname=uppder('SALES'))--9、列出薪金高于公司平均水平的所有雇员select ename from emp where sal>(select avg(sal) from emp);--10、列出与“SCOTT”从事相同工作的所有雇员select ename from emp where job=(select job from emp whereename=upper('scott'));--11、列出某些雇员的姓名和薪金,条件是他们的薪金等于部门30中任何一个雇员的薪金select ename,sal from emp where sal in (select sal from emp where deptno=30);--12、列出某些雇员的姓名和薪金,条件是他们的薪金高于部门30中所有雇员的薪金select ename ,sal from emp where sal>(select max(sal) from emp where deptno=30);--13、列出每个部门的信息以及该部门中雇员的数量select d.deptno,dname,count(ename) from dept d left join emp e on(d.deptno=e.deptno)group by d.deptno,dname--14、列出所有雇员的雇员名称、部门名称和薪金Select e.ename,d.dname,e.sal from emp e left join dept d on (d.deptno=e.deptno)--15、列出从事同一种工作但属于不同部门的雇员的不同组合Select tba.ename,tbb.ename,tba.job,tbb.job,tba.deptno,tba.deptnoFrom emp tba,emp tbbWhere tba.job=tbb.job and tba.deptno<>tbb.deptno--16、列出分配有雇员数量的所有部门的详细信息,即使是分配有0个雇员Select dept.deptno,dname,loc,count(empno)From dept,empWhere dept.deptno=emp.deptno(+)Group by dept.deptno,dname,loc--17、列出各种类别工作的最低工资Select min(sal) from emp group by job--18、列出各个部门的MANAGER(经理)的最低薪金Select deptno,min(sal) from emp where job=upper(…manager‟) group by deptno--19、列出按年薪排序的所有雇员的年薪select (sal+nvl(comm,0))*12 as avn from emp order by avn--20、列出薪金水平处于第四位的雇员Select * from (Select ename,sal, rank() over (order by sal desc) as grade from emp) where grade=4。
SQL语句练习参考答案(教学数据库)

以下为SQL查询练习参考答案(查询用到的表参照教材P82)。
1.查询全体学生的姓名和所在系。
Select Sno,SdeptFrom Student;2.查询所有课程的详细信息。
Select *From Course;或者Select Cno,Cname,Cpno,CcreditFrom Course;3.查询选修了课程的学生学号。
Select Distinct SnoFrom SC;注意:select子句中需要指定Distinct 关键词4.查询有学生选修的课程号。
Select Distinct CnoFrom SC;5.检索年龄在 18到 20之间(含18和20)的女生的学号、姓名及年龄。
Select Sno,Sname,SageFrom StudentWhere Ssex=’女’ and (Sage between 18 and 20);注意: where子句性别的常量值——女,需用英文状态下的单引号或双引号。
或者Select Sno,Sname,SageFrom StudentWhere Ssex=’女’ and (Sage >=18 and Sage<= 20);6.检索“200215121”学生选修课程号及成绩。
Select Cno,GradeFrom SCWhere Sno=’200215121’;7.检索选修了“1”号或“3”号课程的学生学号、课程号及成绩。
Select *From SCWhere Cno=’1’ or Cno=’3’;注意: where子句若用Cno=’1’ or ’3’是错误的,or连接的是两个布尔表达式。
8.检索所有姓王的同学的姓名、性别和所在系。
Select Sname,Ssex,SdeptFrom StudentWhere Sname like ’王*’;注意: Access中的通配符为’*’ 和’?’标。
准SQL中的通配符为’%’ 和’_’9.检索课程名中包括“数据库”的课程详细信息,结果按学分降序排列。
SQL语句练习及答案

sql语句练习题1数据库有如下四个表格:student(sno,sname,sage,ssex,sdpt) 学生表系表(dptno,dname)course(cno,cname, gradet, tno) 课程表sc(sno,cno,score) 成绩表teacher(tno,tname) 教师表要求:完成以下操作1.查询姓"欧阳"且全名为三个汉字的学生的姓名。
select sname from student where sname like “欧阳__‟;2.查询名字中第2个字为"阳"字的学生的姓名和学号。
select sname,sno from student where sname like '_阳%';3.查询所有不姓刘的学生姓名。
select sname,sno,ssexfrom studentwhere sname not like “刘%”;4.查询db_design课程的课程号和学分。
select cno,ccredit from coursewhere cname like 'db_design'5.查询以"db_"开头,且倒数第3个字符为i的课程的详细情况。
select * from course where cname like 'db%i_ _';6.某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。
查询缺少成绩的学生的学号和相应的课程号。
select sno,cno from sc where grade is null;7.查所有有成绩的学生学号和课程号。
select sno,cno from sc where grade is not null;8.查询计算机系年龄在20岁以下的学生姓名。
select sname from student where sdept= 'cs' and sage<20;9.查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。
sql查询语句练习(解析版)

sql查询语句练习(解析版)sql查询语句练习(解析版)BY DD表情况Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname,T#) 课程表SC(S#,C#,score) 成绩表Teacher(T#,Tname) 教师表create table Student(S# varchar2(20),Sname varchar2(10),Sage int,Ssex varchar2(2)) ;create table Course(C# varchar2(20),Cname varchar2(10),score varchar2(4)) ;create table SC(S# varchar2(20),C# varchar2(20),score varchar2(4)) ;create table Teacher(T# varchar2(20),Tname varchar2(10)) ;insert into Student(S#,Sname,Sage,Ssex) values('1001','李五','15','男');insert into Student(S#,Sname,Sage,Ssex) values('1002','张三','16','女');insert into Student(S#,Sname,Sage,Ssex) values('1003','李四','15','女');insert into Student(S#,Sname,Sage,Ssex) values('1004','陈二','14','男');insert into Student(S#,Sname,Sage,Ssex) values('1005','小四','15','男');问题:1、查询“001”课程比“002”课程成绩高的所有学生的学号;select a.S# from (select s#,score from SC where C#='001') a,(select s#,scorefrom SC where C#='002') bwhere a.score>b.score and a.s#=b.s#;解析:(select s#,score from SC where C#='001') a//从SC中查询C#=001的学生学号和分数,并定义为a表。
SQL查询语句 例题详解

1. 指定列[例4-1] 查询教师的学号与姓名。
SELECT t_no, t_nameFROM teacher2. SELECT子句使用*号表示选择所有的列[例4-2] 查询全体教师的详细记录。
SELECT t_no, t_name,t_sex,t_dutyFROM teacher或SELECT *FROM teacher[例4-3] 查询teacher表中的前三行数据,使用关键字“TOP”。
SELECT TOP 3 *FROM teacher3. 基于字段表达式的查询[例4-4] 查询全体学生的姓名及年龄sage(可以不显示该列名)。
⏹第一种情况:无列名。
SELECT s_name, datediff(year,s_birthday,getdate())FROM student⏹第二种情况:在列表达式前加“=”指定列名。
SELECT s_name, sage=datediff(year,s_birthday,getdate())FROM student⏹第三种情况:在列表达式后加“AS”指定列名的别名。
SELECT s_name AS 姓名, datediff(year,s_birthday,getdate()) AS sageFROM student4. SELECT语句使用集函数[例4-5] 统计student表中学生的总人数。
SELECT count(*)FROM student[例4-6] 查询选修课程的学生人数。
SELECT COUNT(DISTINCT s_no)FROM choice[例4-7] 查询choice表中成绩列的总合。
SELECT SUM(score) AS 总分FROM choice[例4-8] 查询choice表中分数的最高值。
SELECT MAX(score) AS 分数第一FROM choice[例4-9] 查询choice表中分数的最低值。
SELECT MIN(score) AS 分数最低FROM choice[例4-10] 查询choice表中成绩列的平均分。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
sql 语句大致可以分类两大类SQL(结构化查询语言)针对数据库database和表table的操作创建create查看show修改alter删除drop// 创建一个数据库create database mydb;// 查看所有的数据库库show databases;// 删除 mydbdrop database mydb;// 删除 user 表drop table user;针对表中的记录的操作增 insert删 delete改 update查 select一、操作数据库创建一个名称为mydb1的数据库create database mydb1;查看当前数据库服务器中的所有数据库show databases;删除前面创建的mydb3数据库drop database mydb3;二、操作表1. 创建表创建员工表Id 整形name 字符型sex 字符型或bit型brithday 日期型Entry_date 日期型job 字符型Salary 小数型resume 大文本型use database mydatabasecreate table employee(id int,name varchar(20),sex varchar(6),brithday date,entry_date date,job varchar(20),salary double,resume text);2. 修改表在上面员工表的基础上增加一个image列alter table employee add colm1 int;修改job列,使其长度为60alter table employee modify job varchar(60);删除sex列。
alter table employee drop sex;表名改为users。
rename table employee to user;列名name修改为usernamealter table user change column name username varchar(20);查看所有表show tables;删除 user 表drop table user;三、数据的增删改查1. insert语句create table employee(id int primary key,name varchar(20) not null,sex varchar(10),birthday date,salary float not null,entry_date date,resume text);// 向员工表插入三条记录insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(1,'zhangsan','male','1987-11-23',1500,'2010-2-18','good boy'); insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(2,'wangwu','male','1988-11-23',1200,'2010-2-18','good boy'); insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(3,'xiaohong','female','1983-11-23',1800,'2010-2-18','goodgirl');insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(4,'王小明','男','1986-11-23',3800,'2011-2-18','handsome boy');insert into employee values(5,'王小明','男','1986-11-23',3800,'2011-2-18','handsome boy');insert into employee(id,name, salary) values(6,'王小明', 3800);// 查看表的所有记录select * from employee;2. update语句将所有员工薪水修改为5000元update employee set salary=5000;将姓名为’zhangsan’的员工薪水修改为3000元。
update employee set salary=3000 where name='zhangsan';将姓名为’lisi’的员工薪水修改为4000元,sex改为female。
update employee set salary=4000,sex='female' where name='wangwu'; 将xiaohong的薪水在原有基础上增加1000元update employee set salary=salary+1000 where name='xiaohong';3. delete语句删除表中name为’赵楠’的记录。
delete from employee where name='赵楠';删除表中所有记录。
delete from employee;4. select语句student.sqlcreate table student(id int,name varchar(20),chinese float,english float,math float);insert into student(id,name,chinese,english,math) values(1,'张小明',89,78,90);insert into student(id,name,chinese,english,math) values(2,'李进',67,53,95);insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);insert into student(id,name,chinese,english,math) values(4,'李一',88,98,92);insert into student(id,name,chinese,english,math) values(5,'李来财',82,84,67);insert into student(id,name,chinese,english,math) values(6,'张进宝',55,85,45);insert into student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);查询表中所有学生的信息。
select * from student;查询表中所有学生的姓名和对应的英语成绩。
select name,english from student;过滤表中重复数据。
select distinct english from student;在所有学生分数上加10分特长分。
select name,english+10,chinese+10,math+10 from student;统计每个学生的总分。
select name,english+chinese+math as sum from student;使用别名表示学生分数。
where 子句查询姓名为李一的学生成绩select * from student where name='李一';查询英语成绩大于90分的同学select * from student where english>90;查询总分大于200分的所有同学select name,english+chinese+math from student whereenglish+chinese+math>200;运算符查询英语分数在 80-90之间的同学。
select * from student where english between 65 and 85;查询数学分数为89,90,91的同学。
select name,math from student where math in(89,90,91);查询所有姓李的学生成绩。
select * from student where name like '李%';// 查询姓李的两个字的学生select * from student where name like '李_';查询数学分>80,语文分>80的同学。
select * from student where math>80 and chinese>80;查询英语>80或者总分>200的同学select *,chinese+math+english from student where english>80 or chinese+english+math>200;order by 子句对数学成绩排序后输出。
select * from student order by math;对总分排序后输出,然后再按从高到低的顺序输出select *,chinese+math+english from student order bychinese+math+english desc;对姓李的学生成绩排序输出select * from student where name like '李%' order bychinese+math+english;合计函数count统计一个班级共有多少学生?select count(*) from student;统计数学成绩大于90的学生有多少个?select count(*) from student where math>90;统计总分大于230的人数有多少?select count(*) from student where chinese+math+english>230;sum统计一个班级数学总成绩?select sum(math) from student;统计一个班级语文、英语、数学各科的总成绩select sum(math),sum(chinese),sum(english) from student;统计一个班级语文、英语、数学的成绩总和select sum(math+chinese+english) from student;统计一个班级语文成绩平均分select sum(chinese)/count(*) from student;缺考的不参与计算select sum(chinese)/count(chinese) from student;avg语文平均分select avg(chinese) from student;max/min语文最高分select max(chinese) from student;group bycreate table orders(id int,product varchar(20),price float);insert into orders(id,product,price) values(1,'dianshi',900);insert into orders(id,product,price) values(2,'xiyiji',100);insert into orders(id,product,price) values(3,'xiyifen',90);insert into orders(id,product,price) values(4,'orange',9);insert into orders(id,product,price) values(5,'xiyifen',90);将商品归类select * from orders group by product;显示单类商品总结select *,sum(price) from orders group by product;商品分类显示单类商品总价大于100的select *,sum(price) from orders group by product havingsum(price)>100;// 将单价大于20 的商品进行归类显示按照价格排序select * from orders where price>20 group by product order by price;四、表的约束我们可以在创建表的同时为字段增加约束,对将来插入的数据做一些限定唯一约束 uniquecreate table a(name varchar(20) unique);insert into a values('aaa');insert into a values('aaa'); 错 name有唯一约束非空约束 not nullcreate table b(id int,name varchar(20) not null);insert into b values(1,'aaa');insert into b (id) values(2); 错,name有非空约束主键每张表都应该有个主键方便我们找到记录主键必须有唯一约束、非空约束主键约束 primary keycreate table c(id int primary key,name varchar(20) not null);insert into c (id,name) values(1,'aaaa');insert into c(id,name) values(1,'bbbb'); 错,主键重复insert into c(name) values('ccc'); 错,主键不能为null主键可以定义为自动增长,注意主键类型必须是intcreate table d(id int primary key auto_increment,name varchar(20));insert into d(name) values('ccc');insert into d(name) values('aaaa');delete from d where id=4;create table e(id int,name varchar(20));// 增加主键约束alter table e modify id int primary key;// 删除主键约束alter table e drop primary key;// 创建联合主键create table f(firstname varchar(20),lastname varchar(20),primary key(firstname, lastname));insert into f values('zhao','nan');insert into f values('li', 'nan');最重要的约束外键约束create table husband(id int primary key,name varchar(20));create table wife(id int primary key auto_increment,name varchar(20),husbandid int,constraint husbandid_FK foreign key(husbandid) references husband(id) );insert into husband (id,name) values(3,'liuxiaoshuai');insert into wife (name, husbandid) values('fengjie', 3);delete from husband where id=3;create table aaa(id int);// 为aaa 加外键约束alter table aaa add constraint husid_FK foreign key(id) references husband(id);// 删除外键约束alter table aaa drop foreign key husid_FK;五、对象和表的关系javabean 一张表多对一在多的一方建外键参照一的一方的主键多对多需要创建中间表描述关系中间表需要两列作为联合主键同时又是外键分别参照两张表的主键一对一分清主从关系在从的一方建外键参照主的一方的主键由于在一对一的关系中外键不允许为空和重复(必须要找到主的一方,否则从的一方就没有存在的意义)干脆将从的一方的主键直接作为外键六、多表操作创建表多表查询多对一多的一方加外键create table department(id int primary key auto_increment,name varchar(20));create table employee(id int primary key auto_increment,name varchar(20),departmentid int,constraint departmentid_FK foreign key(departmentid) references department(id));// 插入三个部门insert into department (name) values('开发部');insert into department (name) values('销售部');insert into department (name) values('人事部');// 插入5个员工insert into employee (name, departmentid) values ('张三', 1);insert into employee (name, departmentid) values ('王五', 2);insert into employee (name, departmentid) values ('李四', 3);insert into employee (name, departmentid) values ('赵六', 2);insert into employee (name, departmentid) values ('田七', 1);// 查询1号部门的员工select * from employee where departmentid=1;销售部所有的员工select id from department where name='销售部';select * from employee where departmentid=2;select * from employee where departmentid=(select id from department where name='销售部');// 多表查询select * from employee,department;+----+------+--------------+----+--------+| id | name | departmentid | id | name |+----+------+--------------+----+--------+| 1 | 张三 | 1 | 1 | 开发部 | | 1 | 张三 | 1 | 2 | 销售部 | | 1 | 张三 | 1 | 3 | 人事部 | | 2 | 王五 | 2 | 1 | 开发部 | | 2 | 王五 | 2 | 2 | 销售部 | | 2 | 王五 | 2 | 3 | 人事部 | | 3 | 李四 | 3 | 1 | 开发部 | | 3 | 李四 | 3 | 2 | 销售部 | | 3 | 李四 | 3 | 3 | 人事部 | | 4 | 赵六 | 2 | 1 | 开发部 | | 4 | 赵六 | 2 | 2 | 销售部 | | 4 | 赵六 | 2 | 3 | 人事部 | | 5 | 田七 | 1 | 1 | 开发部 | | 5 | 田七 | 1 | 2 | 销售部 | | 5 | 田七 | 1 | 3 | 人事部 | +----+------+--------------+----+--------+笛卡尔积结果中有很多不匹配的数据废数据去除不匹配数据 (废数据) 一张表的外键列=参照表的主键列select * from employee,department whereemployee.departmentid=department.id;+----+------+--------------+----+--------+| id | name | departmentid | id | name |+----+------+--------------+----+--------+| 1 | 张三 | 1 | 1 | 开发部 | | 5 | 田七 | 1 | 1 | 开发部 | | 2 | 王五 | 2 | 2 | 销售部 | | 4 | 赵六 | 2 | 2 | 销售部 | | 3 | 李四 | 3 | 3 | 人事部 | +----+------+--------------+----+--------+加上我们的帅选条件select * from employee,department whereemployee.departmentid=department.id and ='销售部';最终答案select e.* from employee e,department d where e.departmentid=d.id and ='销售部';多对多create table teacher(id int primary key auto_increment,name varchar(20));create table student(id int primary key auto_increment,name varchar(20));// 中间表create table tea_stu(teaid int,stuid int,primary key(teaid, stuid),constraint teaid_FK foreign key(teaid) references teacher(id),constraint stuid_FK foreign key(stuid) references student(id));// 插入三个老师记录insert into teacher (name) values('老张');insert into teacher (name) values('老黎');insert into teacher (name) values('老方');// 插入7个学生记录insert into student(name) values('张三');insert into student(name) values('李四');insert into student(name) values('王五');insert into student(name) values('赵楠');insert into student(name) values('刘小帅');insert into student(name) values('王芳');insert into student(name) values('刘红');// 插入中间表描述关系insert into tea_stu(teaid, stuid) values(1,1);insert into tea_stu(teaid, stuid) values(1,2);insert into tea_stu(teaid, stuid) values(1,3);insert into tea_stu(teaid, stuid) values(1,4);insert into tea_stu(teaid, stuid) values(2,3);insert into tea_stu(teaid, stuid) values(2,4);insert into tea_stu(teaid, stuid) values(2,5);insert into tea_stu(teaid, stuid) values(2,7);insert into tea_stu(teaid, stuid) values(3,2);insert into tea_stu(teaid, stuid) values(3,4);insert into tea_stu(teaid, stuid) values(3,1);insert into tea_stu(teaid, stuid) values(3,5);insert into tea_stu(teaid, stuid) values(3,6);// 查询查询2号老师的学生信息select s.* from student s, tea_stu ts where s.id=ts.stuid andts.teaid=2;查询老方的所有学生select s.* from student s, teacher t, tea_stu ts where ts.stuid=s.id and ts.teaid=t.id and ='老方';多表查询n张表联合查需要去掉废数据需要n-1个条件条件都是外键列=参照列一对一create table person(id int primary key auto_increment,name varchar(20));create table idcard(id int primary key,location varchar(20),constraint personid_FK foreign key(id) references person(id) );insert into person (name) values('zhangsan');insert into person (name) values('lisi');insert into idcard (id,location) values(2,'天津');insert into idcard (id,location) values(1,'上海');查李四的身份证select idcard.* from person,idcard where idcard.id=person.id and ='lisi';。