sql语句语法

手写顺序

 执行顺序

join语句

  • 内连接:A、B表中共有的

  • 左连接:A表中所有的

  • 右连接:B表中所有的

  • 左连接+条件:A中有B中没有的

  • 右连接+条件 :B中有A中没有的

  • 全连接:A、B表中所有数据

  • 全不连接:A、B表中各自独有的

sql语句

#试题一、找出平均成绩大于60的所有学生的学号(即student_id)、姓名和平均分数
     
            SELECT a.`name`,a.student_id,AVG(b.number)
            FROM tbl_student a,tbl_score b
            WHERE a.student_id=b.`student_id`
            GROUP BY a.student_id
            HAVING AVG(b.number)>60
#试题二、查询所有学生的学号,姓名,选课数和总成绩;
            SELECT a.`name`,a.student_id,COUNT(c.course_id),SUM(b.`number`)
            FROM tbl_student a,tbl_score b,tbl_course c
            WHERE a.student_id=b.`student_id` AND b.`course_id`=c.`course_id`
            GROUP BY a.student_id
            
            select a.student_id,b.name,COUNT(a.course_id),SUM(a.number)
            from tbl_score a inner join
            tbl_student  b on a.student_id = b.student_id
            GROUP BY a.student_id;
      
#试题三、查找名字中含“封”字的老师的总数
            SELECT COUNT(NAME)
            FROM tbl_teacher
            WHERE NAME LIKE '%封%'
#试题四、查询没有学过小甄老师课的同学的学号、姓名
            SELECT a.student_id ,a.name
            from tbl_student a
            where a.student_id not in
            (select student_id from tbl_score where course_id=
             (SELECT course_id from tbl_course where teacher_id=
              (select teacher_id from tbl_teacher where name = '小甄')))
#试题五、查询学过"4"且学过编号"5"课程的同学的学号
            SELECT  a.student_id
            from (select * from tbl_score where course_id = 4) a
            INNER JOIN
                  (SELECT * from tbl_score where course_id = 5) b
            on a.student_id = b.student_id
                    
#试题六、查询课程编号"1"成绩比编号"2"成绩低的学生的学号
                
        SELECT t1.student_id
        from  (select student_id,course_id,number from tbl_score where course_id =1) t1
        INNER JOIN
              (select student_id ,course_id,number from tbl_score where course_id =2) t2
        on t1.student_id = t2.student_id
        where t1.number < t2.number
        order by t1.student_id
    
#试题七、找出有一门课程低于60分的学生的学号和名字
                select b.student_id,b.`name`
                from tbl_score a,tbl_student b
                where a.number<60 and a.student_id =b.student_id
                GROUP BY b.student_id
                select  t1.student_id ,t1.name
                from tbl_student t1
                INNER JOIN  
                (select student_id,MIN(number) from tbl_score
                 GROUP BY student_id
                 HAVING MIN(number)<60
                 ) t2
                 on t1.student_id = t2.student_id
#试题八、查询选完全部课程的学生的学号
                select a.student_id
                from tbl_score a,tbl_course b
                where  a.course_id=b.course_id
                GROUP BY a.student_id
                HAVING COUNT(a.course_id)=(select COUNT(*) from tbl_course)
                
                SELECT t1.student_id , count(t2.course_id) as totalCourse
                from tbl_score t1
                INNER JOIN
                tbl_course t2
                on  t1.course_id = t2.course_id
                GROUP BY t1.student_id
                HAVING totalCourse = (select COUNT(course_id) from tbl_course)
#试题九、按平均成绩从高到低,显示所有学生的各科课程成绩
        SELECT *
from tbl_student a,tbl_score b
where a.student_id =b.student_id
and a.student_id in(
SELECT a.student_id
            FROM tbl_student a,tbl_score b
            WHERE a.student_id=b.`student_id`
            GROUP BY a.student_id
            ORDER BY AVG(b.number) desc
)            
select t.student_id , t.course_id,t.number,b.avgScore
from tbl_score t
LEFT JOIN (select student_id,AVG(number) as avgScore from tbl_score GROUP BY student_id) b
on t.student_id = b.student_id
ORDER BY avgScore desc
#试题十、查询各科成绩的最高分和最低分及对应的学生姓名学号。
            
            SELECT  c.name,c.student_id,a.number
            from tbl_score a,
            (
            (
            SELECT MAX(number) as num ,a.course_id as course_id
            from tbl_course a,tbl_score b
            where a.course_id=b.course_id
            GROUP BY a.course_id
            )
            UNION
            (
            SELECT MIN(number) as num ,a.course_id as course_id
            from tbl_course a,tbl_score b
            where a.course_id=b.course_id
            GROUP BY a.course_id
            )
            ) b,tbl_student c
            where a.course_id=b.course_id and a.number=b.num
             and a.student_id=c.student_id
select *
from
(select a.course_id , a.student_id,b.max_number
from tbl_score a
INNER JOIN(select course_id , MAX(number) max_number from tbl_score GROUP BY course_id ) b
on a.course_id = b.course_id
where a.number = b.max_number) x1
INNER JOIN
(select c.course_id,c.student_id,d.min_number
  from tbl_score c
  INNER JOIN (select course_id,MIN(number) min_number from tbl_score GROUP BY course_id) d
  on c.course_id =d.course_id
  where c.number = d.min_number) x2
on x1.course_id = x2.course_id
ORDER BY x1.course_id

猜你喜欢

转载自blog.csdn.net/qq_36042938/article/details/111476515
今日推荐