设为首页收藏本站

安徽论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8417|回复: 0

SQL数据库十四种案例介绍

[复制链接]

63

主题

789

回帖

1382

积分

金牌会员

Rank: 6Rank: 6

积分
1382
发表于 2022-3-26 10:58:25 | 显示全部楼层 |阅读模式
网站内容均来自网络,本站只提供信息平台,如有侵权请联系删除,谢谢!
数据表
  1. /*
  2. Navicat SQLite Data Transfer

  3. Source Server         : school
  4. Source Server Version : 30808
  5. Source Host           : :0

  6. Target Server Type    : SQLite
  7. Target Server Version : 30808
  8. File Encoding         : 65001

  9. Date: 2021-12-23 16:06:04
  10. */

  11. PRAGMA foreign_keys = OFF;

  12. -- ----------------------------
  13. -- Table structure for Course
  14. -- ----------------------------
  15. DROP TABLE IF EXISTS "main"."Course";
  16. CREATE TABLE Course(
  17.     courseid integer  primary key autoincrement,
  18.     courseme varchar(32),
  19.     teacherid int
  20. );

  21. -- ----------------------------
  22. -- Records of Course
  23. -- ----------------------------
  24. INSERT INTO "main"."Course" VALUES (3001, '语文', 1001);
  25. INSERT INTO "main"."Course" VALUES (3002, '数学', 1002);

  26. -- ----------------------------
  27. -- Table structure for Mark
  28. -- ----------------------------
  29. DROP TABLE IF EXISTS "main"."Mark";
  30. CREATE TABLE Mark(
  31.     userid integer,
  32.     courseid integer not null,
  33.     score int default 0
  34. );

  35. -- ----------------------------
  36. -- Records of Mark
  37. -- ----------------------------
  38. INSERT INTO "main"."Mark" VALUES (2001, 3001, 89);
  39. INSERT INTO "main"."Mark" VALUES (2001, 3002, 90);
  40. INSERT INTO "main"."Mark" VALUES (2002, 3001, 66);
  41. INSERT INTO "main"."Mark" VALUES (2003, 3002, 85);

  42. -- ----------------------------
  43. -- Table structure for sqlite_sequence
  44. -- ----------------------------
  45. DROP TABLE IF EXISTS "main"."sqlite_sequence";
  46. CREATE TABLE sqlite_sequence(name,seq);

  47. -- ----------------------------
  48. -- Records of sqlite_sequence
  49. -- ----------------------------
  50. INSERT INTO "main"."sqlite_sequence" VALUES ('Teacher', 1002);
  51. INSERT INTO "main"."sqlite_sequence" VALUES ('Student', 2002);
  52. INSERT INTO "main"."sqlite_sequence" VALUES ('Course', 3002);

  53. -- ----------------------------
  54. -- Table structure for Student
  55. -- ----------------------------
  56. DROP TABLE IF EXISTS "main"."Student";
  57. CREATE TABLE Student(
  58.     userid integer  primary key autoincrement,
  59.     username varchar(32),
  60.     userage int,
  61.     usersex varchar(32)
  62. );

  63. -- ----------------------------
  64. -- Records of Student
  65. -- ----------------------------
  66. INSERT INTO "main"."Student" VALUES (2001, '小明', 18, '男');
  67. INSERT INTO "main"."Student" VALUES (2002, '小红', 18, '女');

  68. -- ----------------------------
  69. -- Table structure for Teacher
  70. -- ----------------------------
  71. DROP TABLE IF EXISTS "main"."Teacher";
  72. CREATE TABLE Teacher(
  73.     teacherid integer primary key autoincrement,
  74.     teachername varchar(32)
  75. );

  76. -- ----------------------------
  77. -- Records of Teacher
  78. -- ----------------------------
  79. INSERT INTO "main"."Teacher" VALUES (1001, '张三');
  80. INSERT INTO "main"."Teacher" VALUES (1002, '李四');
复制代码
问题:

1、查询“语文”课程比“数学”课程成绩低的所有学生的学号
  1. select a.userid from
  2. (select userid,score from Mark where courseid ='3001')a,
  3. (select userid,score from Mark where courseid ='3002')b
  4. where a.userid = b.userid and a.score<b.score;
复制代码
2、查询平均成绩大于60分的同学的学号和平均成绩
  1. select userid,avg(score) from Mark
  2. group by userid
  3. having avg(score)>60;
复制代码
3、查询所有同学的学号、姓名、选课数、总成绩
  1. select s.userid ,s.username ,count_courseid as 选课数,
  2. sum_score  as 总成绩
  3. from Student s
  4. left join
  5. (select userid,count(courseid ) as count_courseid,sum(score) as sum_score
  6. from Mark group by userid  )sc
  7. on s.userid = sc.userid;
复制代码
4、查询姓‘李'的老师的个数:
  1. select count(teachername )
  2. from Teacher
  3. where teachername  like '张%';
复制代码
5、检索语文课程分数小于60,按分数降序排列的同学学号:
  1. select userid ,score
  2. from Mark
  3. where courseid ='3001'
  4. and score<60
  5. order by score desc;
复制代码
6、查询学/没学过”张三”老师讲授的任一门课程的学生姓名
  1. select username  
  2. from Student
  3. where userid  in (
  4.     select userid  
  5.     from Mark,Course,Teacher
  6.     where Course.teacherid  = Teacher.teacherid  and Mark.courseid = Course.courseid
  7.     and Teacher.teachername ='张三'
  8. );
复制代码
7、查询全部学生选修的课程和课程号和课程名:
  1. select courseid ,courseme
  2. from Course
  3. where courseid  in (select courseid from Mark group by courseid);
复制代码
8、检索选修两门课程的学生学号:
  1. select userid
  2. from Mark
  3. group by userid
  4. having count(8) == 2;
复制代码
9、查询各个课程及相应的选修人数
  1. select courseid ,count(*) from Course group by courseid ;
复制代码
10、查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
  1. select Student.username ,Mark.score
  2. from Mark
  3. left join Student  on Mark.userid = Student.userid
  4. left join Course  on Mark.courseid = Course.courseid
  5. left join Teacher  on Course.teacherid  = Teacher.teacherid
  6. where Teacher.teachername  = '张三'
  7. and Mark.score = (
  8. select max(score)
  9. from Mark sc_1
  10. where Mark.courseid = sc_1.courseid);
复制代码
11、求选了课程的学生人数:
  1. select count(2) from
  2. (select distinct userid from Mark)a;
复制代码
12、查询课程编号为“语文”且课程成绩在80分以上的学生的学号和姓名
  1. select Mark.userid,Student.username  
  2. from Mark
  3. left join Student on Mark.userid  = Student.userid
  4. where Mark.courseid  = '3001' and Mark.score>80;
复制代码
13、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
  1. select courseid ,avg(score)
  2. from Mark
  3. group by courseid
  4. order by avg(score),courseid desc;
复制代码
14、查询课程名称为“数学”,且分数高于85的学生名字和分数:
  1. select c.courseme ,Student.userid ,Student.username ,Mark.score
  2. from Course c
  3. left join Mark on Mark.courseid  = c.courseid
  4. LEFT JOIN Student  on Student.userid  = Mark.userid
  5. where c.courseme = '数学' and Mark.score>85;
复制代码
到此这篇关于SQL数据库十四种案例介绍的文章就介绍到这了,更多相关SQL数据库案例内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
                                                        
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
免责声明
1. 本论坛所提供的信息均来自网络,本网站只提供平台服务,所有账号发表的言论与本网站无关。
2. 其他单位或个人在使用、转载或引用本文时,必须事先获得该帖子作者和本人的同意。
3. 本帖部分内容转载自其他媒体,但并不代表本人赞同其观点和对其真实性负责。
4. 如有侵权,请立即联系,本网站将及时删除相关内容。
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表