熟练掌握使用SELECT语句进行数据查询。
1.对数据库stuinfo使用T-SQL命令进行如下操作:
查询student表中的学号、姓名和年龄并为列设置别名,结果按学号升序排。
USE stuinfo;
SELECT sno AS 学号, sname AS 姓名, YEAR(GETDATE()) - YEAR(sbirthday) AS 年龄 FROM student ORDER BY sno;
查询班级(要求不重复)。
SELECT DISTINCT sclass AS 班级 FROM student;
查询姓“王”的学生信息。
SELECT * FROM student WHERE sname LIKE '王%';
查询成绩在80-100之间的学号、课程号及成绩,结果先按课程号升序排,课程号一样的再按成绩降序排。
SELECT sno AS 学号, cno AS 课程号, degree AS 成绩 FROM score WHERE degree >= 80 AND degree <= 100 ORDER BY cno ASC, degree DESC;
查询所有缺考的学生的学号及课程号。
SELECT sno AS 学号, cno AS 课程号 FROM score WHERE degree is NULL;
查询 ‘3-105’课的选课人数、最高分、最低分和平均分。
SELECT COUNT(*) AS 选课人数, MAX(degree) AS 最高分 , MIN(degree) AS 最低分, AVG(degree) AS 平均分 FROM score where cno = '3-105';
查询每位同学的平均成绩,包括学号和平均成绩两列,结果按学号升序排。
SELECT sno AS 学号, AVG(degree) AS 平均成绩 FROM score GROUP BY sno ORDER BY sno;
查询各班各门课的平均成绩,包括班号、课程号和平均成绩三列,结果先按班升序排,班一样的再按课程号升序排。
SELECT AVG(degree) AS 平均成绩, sclass AS 班号, cno AS 课程号 FROM score, student WHERE score.sno = student.sno GROUP BY sclass, cno ORDER BY sclass ASC, cno ASC;
查询score表中至少有5名学生选修的课程号及平均分。
SELECT cno AS 课程号, AVG(degree) AS 平均分 FROM score GROUP BY cno HAVING COUNT(*) >= 5;
查询其平均分高于80的学生的学号,姓名和平均分。
SELECT student.sno, sname, AVG(degree) AS avg FROM score, student WHERE student.sno = score.sno GROUP BY student.sno,sname HAVING AVG(degree) > 80;
查询“95031”班所选课程的平均分,包括课程名和平均分。
SELECT cname, AVG(degree) AS avg FROM course, score, student WHERE course.cno = score.cno AND score.sno = student.sno AND student.sclass = '95031' GROUP BY score.cno, cname;
查询所有教师的任课情况,包括教师姓名和课程名两列,如果某位教师没有任课则课程名列显示NULL。
SELECT tname, cname FROM teacher LEFT JOIN course ON teacher.tno = course.tno;
查询其最低分大于70,最高分小于90的学生的学号、所选课程号及其分数。
SELECT sno, cno, degree FROM score WHERE sno in (SELECT sno FROM score group by sno having MIN(degree) > 70 ) AND sno in (SELECT sno FROM score group by sno having MAX(degree) < 90 )
# 没有满足条件的行,故无截图
查询成绩高于所选课平均分的学生学号、姓名、课程号和成绩。
SELECT student.sno,sname, cno, degree FROM student, score a WHERE student.sno = a.sno AND degree > (SELECT AVG(degree) FROM score b WHERE a.cno = b.cno);
查询每门课最高分的课程名、学生姓名和成绩。
SELECT cname, sname, degree FROM student, course, score a WHERE student.sno = a.sno AND course.cno = a.cno AND degree = (SELECT MAX(degree) FROM score b WHERE b.cno = a.cno);
查询选修其课程的学生人数多于5人的教师姓名。
SELECT tname FROM teacher, course WHERE teacher.tno = course.tno AND (SELECT COUNT(*) FROM score WHERE score.cno = course.cno) > 5;
# 无符合条件的,不附截图
查询没选“张旭”教师课的学生成绩,并按成绩递增排列。
SELECT degree FROM score WHERE score.cno NOT IN (SELECT course.cno FROM course, teacher WHERE teacher.tname = '张旭' AND course.tno = teacher.tno) ORDER BY degree;
查询没有任课的教师的姓名。
SELECT tname FROM teacher EXCEPT SELECT tname FROM teacher,course WHERE teacher.tno=course.tno;
查询没有选修"6-166"课程的学生的学号和姓名。
SELECT student.sno, student.sname FROM student WHERE student.sno NOT IN (SELECT sno FROM score WHERE cno = '1-166');
查询出所有男生信息放入NS表中。
SELECT student.sno AS 学号, student.sname AS 学生姓名, ssex AS 性别, sbirthday AS 生日, sclass AS 班级, cname AS 课程
, degree AS 成绩 INTO NS FROM student, score, course WHERE student.sno = score.sno AND ssex = '男' AND course.cno = score.cno;
删除没人选的课程。
DELETE FROM course WHERE cno NOT IN(SELECT cno FROM score GROUP BY cno);
# 删除不附截图
将“95031”班学生的成绩全部减去10分。
UPDATE score SET degree = degree - 10 WHERE score.sno IN (SELECT sno FROM student WHERE sclass = '95031');
ordermanagement数据库中有三个表,其结构如下:(加下划线的为主键)
客户表customer(客户号,客户名,地址,电话)
订单表order_list(订单号,客户号,订购日期)
订单明细表Order_detail(订单号,器件号,器件名,单价,数量)
使用SELECT语句完成下列查询:
USE OrderManagement
SELECT 订单号, 客户号, 订购日期 FROM order_list;
2) 查询订单明细中有哪些器件(即查询不重复的器件号和器件名)。
SELECT DISTINCT 器件号, 器件名 FROM order_detail;
SELECT order_detail.订单号, 器件号, 器件名, 单价, 数量 FROM order_detail, customer, order_list WHERE customer.客户号 = order_list.客户号 AND order_list.订单号 = order_detail.订单号 AND customer.客户名 = '三益贸易公司' ORDER BY order_detail.订单号, 单价 DESC;
SELECT * FROM customer WHERE 客户号 NOT IN(SELECT 客户号 FROM order_list);
SELECT * FROM customer WHERE 客户名 LIKE '%科技%';
6) 查询每笔订单的订单号和总金额,查询结果按订单号升序排,查询结果存入表ZJE中。
SELECT 订单号, SUM(单价 * 数量) AS 总金额 INTO ZJE FROM order_detail GROUP BY order_detail.订单号 ORDER BY order_detail.订单号;
SELECT 客户号, COUNT(订单号) AS 订单数量 FROM order_list GROUP BY 客户号 HAVING COUNT(订单号) > 5 ORDER BY 订单数量 DESC;
SELECT * FROM order_detail a WHERE 单价 IN (SELECT MIN(单价) FROM order_detail b WHERE a.器件名 = b.器件名);
SELECT RIGHT(order_detail.订单号,1) AS 订单号, 器件号, MIN(单价) AS 单价, SUM(数量) AS 数量 FROM order_detail GROUP BY order_detail.订单号, 器件号 ORDER BY 订单号,器件号;
SELECT TOP 3 器件号, 器件名, SUM(数量) AS 数量 FROM order_detail GROUP BY 器件号,器件名 ORDER BY 数量 DESC;