麻瓜之我要学sql,啦啦啦啦

四张表

学生表:编号,姓名,性别,班级,生日

CREATE TABLE IF NOT EXISTS student(
sno TINYINT UNSIGNED NOT NULL,
sname VARCHAR(20) NOT NULL,
ssex ENUM('male','female') DEFAULT 'male',
sbirthday DATE NOT NULL,
class VARCHAR(20) NOT NULL,
PRIMARY KEY(sno)
)

 

课程表:课程编号,课程名称,教授课程的老师编号

CREATE TABLE IF NOT EXISTS course(
   cno TINYINT UNSIGNED NOT NULL,
   cname VARCHAR(20) NOT NULL,
   tno TINYINT UNSIGNED NOT NULL,
  PRIMARY KEY(cno)      
)

 

分数表:学生编号,课程编号,分数

CREATE TABLE IF NOT EXISTS score(
    sno TINYINT UNSIGNED NOT NULL,
    cno TINYINT UNSIGNED NOT NULL,
   degree DECIMAL(4, 1)     
);

 

老师表:老师编号,姓名,老师性别,生日,职称,部门

CREATE TABLE IF NOT EXISTS teacher(
   tno TINYINT UNSIGNED NOT NULL,
   tname VARCHAR(20) NOT NULL,
   tsex ENUM('male','female') DEFAULT 'male',
   tbirthday DATE,
   tprof  VARCHAR(10) NOT NULL,
   tdepart VARCHAR(20) NOT NULL,
   PRIMARY KEY(tno)          
);

 

 开始撸题:

  • 查询student表中的所有记录的sname,ssex,class

SELECT sname,ssex,class FROM student;

 

查询教师所在的所有单位(既不重复的单位)

SELECT tdepart is FROM teacher;(全部单位)

SELECT DISTINCT tdepart FROM teacher;(不同的单位)

查询student表的所有记录

SELECT * FROM student

查询score表中成绩在60-80之间的所有记录

SELECT * FROM score WHERE degree BETWEEN 60 AND 80

查询score表中成绩为30,66,10的记录

SELECT * FROM score WHERE score.degree = 30 OR score.degree = 66 OR score.degree = 10  

SELECT * FROM score WHERE degree IN (30,66,10)

查询student表中'class5'班或性别为'female'的同学记录。

 SELECT * FROM student WHERE class = 'class5' OR ssex = 'female'

以class降序查询student表中的数据

SELECT * FROM student ORDER BY class DESC

cno升序,degree降序查询score表中的记录

SELECT * FROM score ORDER BY cno ASC,degree DESC

查询“class5”班的学生人数

SELECT COUNT(*) FROM student WHERE class = 'class5'

查询score表中的最高分的学生学号和课程号

SELECT sno,cno FROM score WHERE degree = (SELECT MAX(degree) FROM score )

查询每门课的平均成绩,要按照课程进行分组,然后求每门课程的平均成绩。

SELECT course.cno,course.cname,AVG(degree) AS degree
FROM course
LEFT JOIN score ON
course.cno=score.cno
GROUP BY score.cno;\

查询score表中至少有5名学生选修的并以3开头的课程的平均分数。

SELECT AVG(degree) FROM score
WHERE cno LIKE '3%'
GROUP BY cno
HAVING COUNT(*)>=5

查询分数大于70,小于90sno

SELECT sno FROM score
WHERE degree BETWEEN 70 AND 90;

 

posted on 2017-12-21 21:01  张小泽的小号  阅读(148)  评论(0编辑  收藏  举报

导航