sql语句集锦

  1. 创建练习需要的表

    create table student(
    sid int,
    sname varchar(32),
    sage int,
    ssex varchar(32)
    );
    create table course(
    cid int,
    cname varchar(32),
    tid int
    );
    create table sc(
    sid int,
    cid int,
    score int
    );
    create table teacher(
    tid int,
    tname varchar(16)
    );
    
  2. 添加数据

    insert into Student select 1,N'刘一',18,N'男' union all
    select 2,N'钱二',19,N'女' union all
    select 3,N'张三',17,N'男' union all
    select 4,N'李四',18,N'女' union all
    select 5,N'王五',17,N'男' union all
    select 6,N'赵六',19,N'女' ;
    
    insert into Teacher select 1,N'叶平' union all
    select 2,N'贺高' union all
    select 3,N'杨艳' union all
    select 4,N'周磊';
    
    insert into teacher values(7,'李三'),(8,'李四');
    
    insert into Course select 1,N'语文',1 union all
    select 2,N'数学',2 union all
    select 3,N'英语',3 union all
    select 4,N'物理',4;
    
    insert into SC 
    select 1,1,56 union all 
    select 1,2,78 union all 
    select 1,3,67 union all 
    select 1,4,58 union all 
    select 2,1,79 union all 
    select 2,2,81 union all 
    select 2,3,92 union all 
    select 2,4,68 union all 
    select 3,1,91 union all 
    select 3,2,47 union all 
    select 3,3,88 union all 
    select 3,4,56 union all 
    select 4,2,88 union all 
    select 4,3,90 union all 
    select 4,4,93 union all 
    select 5,1,46 union all 
    select 5,3,78 union all 
    select 5,4,53 union all 
    select 6,1,35 union all 
    select 6,2,68 union all 
    select 6,4,71;
    
    
  3. sql语句

    #1、查询“1”课程比“2”课程成绩高的所有学生的学号; 
    select a.sid from (select sid,score from sc where cid=1) as a,
    (select sid,score from sc where cid=2) as b 
    where a.sid=b.sid and a.score>b.score;
    
    # 2.查询平均成绩大于60的同学的学号和平均成绩
    select sid,avg(score) from sc group by sid having avg(score)>60;
    
    #3. 查询所有同学的学号,姓名,选课数,总成绩
    # 3.1.使用where 会先产生笛卡尔积,再从笛卡尔积中筛选,效率低
    select a.sid,a.sname,count(b.cid),sum(score)
    from sc as b,student as a where a.sid=b.sid group by b.sid;
    # 3.2.使用join更好一些
    select a.sid,a.sname,count(b.cid),sum(score)
    from student as a left join sc as b on a.sid=b.sid group by b.sid;
    
    
    #4. 查询姓李老师的个数
    select count(distinct(tname)) from teacher where tname like '李%';
    
    #5. 查询没学过叶平老师的课程的学生的学号,姓名
    
    select sid,sname from student where sid not in(
    select sc.sid from sc,course,teacher where teacher.tid=course.tid
    and teacher.tname='叶平' and course.cid=sc.cid
    );
    
    #6. 查询学过编号为‘1’,也学过编号为‘2’课程的同学的学号,姓名
    
    select student.sid,student.sname 
    from student
    where student.sid in (select sid  from sc where sc.cid=2) and student.sid in (select sid  from sc where sc.cid=1);
    
    #7. 查询学过学过叶平老师所有课的同学的学号,姓名
    select sid,sname
    from student
    where sid in (select sid from sc,course,teacher where sc.cid=course.cid and teacher.tid=course.tid
    and teacher.tname='叶平' group by sid having count(sc.cid)
    =(select count(a.cid) from (select course.cid, teacher.tname as name from (course inner join teacher on course.tid=teacher.tid)) as a 
    where a.name='叶平'));
    
    #8. 查询课程编号2的成绩逼课程编号1低的同学的学号,姓名
    select student.sid,student.sname
    from student ,(select sid,score from sc where cid=2) as a,
    (select sid,score from sc where cid=1) as b
    where a.sid=b.sid and a.score<b.score and student.sid=a.sid;
    
    #9. 查询所有课程成绩小于60分的同学的学号,姓名
    select sid,sname
    from student
    where sid not in (select sid from sc where score>60);
    
    #10. 查询没有学全所有课的同学的学号,姓名
    select student.sid,student.sname
    from student,sc
    where student.sid=sc.sid group by sc.sid having count(sc.cid) < (select count(cid) from course);
    
    # 11. 查询至少有一门课程与学号为1的同学所学相同的学号,姓名
    select distinct student.sid,student.sname
    from student,sc
    where sc.sid=student.sid and sc.sid !=1 and sc.cid in (select cid from sc where sid=1);
    
    
    # 13. 将sc表中 叶平老师教的课的成绩更改为此课程的平均成绩
    
    update sc,(select sc.cid as cid,avg(sc.score) as score
    from teacher,course,sc
    where teacher.tname='叶平' and teacher.tid=course.tid and sc.cid=course.cid
    group by sc.cid) as a 
    set sc.score=a.score
    where a.cid=sc.cid;
    
    # 16. 向sc表中插入一些数据,这些记录要符合以下条件:没有上过3号课的同学学号,2号课的平时成绩alter
    insert into sc 
    (select student.sid,2,(select avg(score) from sc where sid=2)
    from student
    where student.sid not in (select sid from sc where cid=3)
    );
    
    # 18. 查询各科成绩最高分和最低分,按以下形式显示:课程id,最高分,最低分
    
    select cid as '课程id',max(score) as '最高分',min(score) as '最低分'
    from sc
    group by cid; 
    
    # 19. 按各科平均成绩从低到高和及格率的百分比从低到高顺序
    
    select sc.cid as '课程id',course.cname as '课程名称',avg(sc.score) as average,
    (100*sum(case when score>60 then 1 else 0 end)/count(*)) as sums
    from sc,course
    where sc.cid=course.cid
    group by sc.cid
    order by average,sums desc;
    
    # 20. 查询如下课程平均成绩和及格率的百分数(用一行表示):java(1),uml(2)
    select sum(case when cid=1 then score else 0 end)/sum(case when cid=1 then 1 else 0 end) as 'java平均分',
    sum(case when cid=1 and score>60 then 1 else 0 end)*100/sum(case when cid=1 then 1 else 0 end) as 'java及格率',
    sum(case when cid=2 then score else 0 end)/sum(case when cid=2 then 1 else 0 end) as 'uml平均分',
    sum(case when cid=2 and score>60 then 1 else 0 end)*100/sum(case when cid=1 then 1 else 0 end) as 'uml及格率'
    from sc;
    
    #21. 查询不同老师所教不同课程平均分由高到低排序
    
    select teacher.tid,course.cid,avg(score) as av
    from teacher,course,sc
    where teacher.tid=course.tid and course.cid=sc.cid
    group by course.tid,course.cid
    order by av desc;
    
    #23. 统计出各科成绩各分段人数
    
    select sc.cid as '课程id', 
    sum(case when sc.score between 70 and 100 then 1 else 0 end) as '70--100',
    sum(case when score between 0 and 70 then 1 else 0 end) as '0--70'
    from sc
    group by sc.cid;
    
    #33. 查询平均成绩大于85的学生的学号,平均成绩
    select sid,avg(score) as av
    from sc 
    group by score having av>80;
    
    #40. 查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 
    
    select student.sname,score
    from teacher,course,student inner join sc on student.sid=sc.sid
    where teacher.tname='叶平' and teacher.tid=course.tid and course.cid=sc.cid and sc.score=(
    select max(score)
    from sc,teacher,course
    where teacher.tname='叶平' and teacher.tid=course.tid and course.cid=sc.cid);
    
    #46. 查询全部学生都选择的课程的id
    select cid from course
    where cid in(
    select cid from sc group by cid having count(sid)= (
    select count(*) from student));
    
    #47. 为student表定义主键
    alter table student add primary key (sid);
    
    #48. 为sc表的sid增加外键
    alter table sc  add foreign key (sid) references student(sid);
    
    #49. 为teacher表添加新字段
    alter table teacher add tsex char(1) not null;
    
    #50. 为teacher表tsex字段重命名
    alter table teacher change tsex tsex0 char(2);
    
    #51. 修改teacher表tsex1字段;
    alter table teacher modify tsex1 char(3) not null;
    
    
原文地址:https://www.cnblogs.com/yloved/p/11826755.html