数据库小练习

Q1:20120007,
SELECT
stu_name,stu_gender,stu_birthday
FROM
t_student
WHERE
stu_id = 20127007

Q2:会计1201,find student"s id & name
SELECT
a.stu_id,a.stu_name
FROM
t_student as a
INNER JOIN t_class b ON b.cls_id = a.cls_id
WHERE b.cls_name = '会计1201'

Q3:会计,2012,find classes with students' couunt
SELECT
count(b.stu_id)
FROM
t_class a
inner join t_student b ON b.cls_id = a.cls_id
WHERE
a.grade = 2012
AND
    a.subject= '会计'
第二种

SELECT
a.cls_name,
count(*)
FROM t_class a
INNER JOIN t_student b ON a.cls_id = b.cls_id
WHERE
a.subject = '会计'
AND
a.grade = 2012
GROUP BY
    a.cls_name

Q4:20127007 find all failured course

SELECT
c.course_name
FROM t_mark a
INNER JOIN t_course c ON c.course_id = a.course_id

WHERE
a.stu_id = 20127001
AND
	 a.scroll < 60


Q5:Add a new course,17317,'Course Foundation',3.5
INSERT INTO t_course (
course_id,course_name,course_credit)
VALUES
    (17317,'DB Foundation',3.5)

Q6:会计,2012,英语[1],FIND avg
SELECT DISTINCT
avg(c.scoll)
FROM
t_class a
inner join t_student b ON b.cls_id = a.cls_id
inner join t_mark c ON b.stu_id = c.stu_id
inner join t_course d ON d.course_id = c.course_id
WHERE
a.grade = 2012
AND
a.subject = '会计'
AND
	 d.course_name = '英语[1]'

Q7:会计,2012,英语[1],failure students
SELECT DISTINCT
b.*
FROM
t_class a
inner join t_student b ON b.cls_id = a.cls_id
inner join t_mark c ON b.stu_id = c.stu_id
inner join t_course d ON d.course_id = c.course_id
WHERE
c.scroll < 60
AND
a.subject = '会计'
AND
	 d.course_name = '英语[1]'
、方法2
SELECT
a.stu_name,b.cls_name
FROM
t_student a
INNER JOIN t_class b ON a.cls_id=b.cls_id
WHERE
b.subject='会计'
AND
b.grade=2012
AND
EXISTS(
SELECT 1 FROM t_mark c
INNER JOIN t_course d ON d.course_id=c.course_id
WHERE
c.stu_id=a.stu_id
AND
d.course_name='英语[1]'
AND
c.scroll<60
)

Q8:会计,2012,5-course-failure
SELECT DISTINCT
b.stu_name/*按分组的列显示*/
FROM
t_class a
inner join t_student b ON b.cls_id = a.cls_id
inner join t_mark c ON b.stu_id = c.stu_id
inner join t_course d ON d.course_id = c.course_id
WHERE
c.scroll < 60
AND
a.subject = '会计'
AND
a.grade =2012
GROUP BY b.stu_name
HAVING count(distinct c.course_id) >=5


方法2
SELECT DISTINCT
b.stu_name,count(*),count(c.course_id)/*按分组的列显示*/
FROM
t_class a
inner join t_student b ON b.cls_id = a.cls_id
inner join t_mark c ON b.stu_id = c.stu_id
inner join t_course d ON d.course_id = c.course_id
WHERE
c.scroll < 60
AND
a.subject = '会计'
AND
a.grade =2012
GROUP BY b.stu_name
HAVING count(distinct c.course_id) >=5
posted @ 2018-11-25 14:10  qianxinggz  阅读(180)  评论(1编辑  收藏  举报