【MySQL】SQL查询练习——数据库原理与应用(SQL Server 2012)——基于计算思维

书本P114,习题4(4)
建立如下数据库,包括4个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

CREATE TABLE Student (
    Sno CHAR(3) Primary Key COMMENT '学号(主码)',
    Sname CHAR(8) NOT NULL COMMENT '学生姓名',
    Ssex CHAR(2) NOT NULL COMMENT '学生性别',
    Sbirthday DATE NULL COMMENT '学生出生年月',
    Class CHAR(20) NULL COMMENT '学生所在年级'
);

CREATE TABLE Teacher (
    Tno CHAR(3) Primary Key COMMENT '教工编号(主码)',
    Tname CHAR(4) NOT NULL COMMENT '教工姓名',
    Tsex CHAR(2) NOT NULL COMMENT '教工性别',
    Tbirthday DATE NULL COMMENT '教工出生年月',
    Prof CHAR(6) NULL COMMENT '职称',
    Depart VARCHAR(10) NOT NULL COMMENT '教工所在部门'
);

CREATE TABLE Course (
    Cno CHAR(5) Primary Key COMMENT '课程号(主码)',
    Cname VARCHAR(10) NOT NULL COMMENT '课程名称',
    Tno CHAR(3) NOT NULL COMMENT '教工编号(外码)',
    FOREIGN KEY(Tno) REFERENCES Teacher(Tno)
);

CREATE TABLE Score (
    Sno CHAR(3) NOT NULL COMMENT '学号(外码)',
    Cno CHAR(5) NOT NULL COMMENT '课程号(外码)',
    Degree DECIMAL(4,1) NULL COMMENT '成绩',
    PRIMARY KEY(Sno, Cno),
    FOREIGN KEY(Sno) REFERENCES Student(Sno),
    FOREIGN KEY(Cno) REFERENCES Course(Cno)
);

INSERT INTO Student (Sno, Sname, Ssex, Sbirthday, Class)
VALUES
('001', '张三', '男', '2000-01-01', '一年级'),
('002', '李四', '女', '2001-02-03', '二年级'),
('003', '王五', '男', '2002-03-05', '三年级'),
('004', '赵六', '女', '2003-04-01', '四年级'),
('005', '钱七', '男', '2004-05-03', '五年级'),
('006', '孙八', '女', '2005-06-05', '六年级');
INSERT INTO Teacher (Tno, Tname, Tsex, Tbirthday, Prof, Depart)
VALUES
('101', '张老师', '男', '1980-01-01', '教授', '计算机科学'),
('102', '李老师', '女', '1985-02-03', '副教授', '人文学科'),
('103', '王老师', '男', '1990-03-05', '讲师', '自然科学'),
('104', '刘老师', '女', '1983-01-01', '副教授', '外语系'),
('105', '陈老师', '男', '1988-02-03', '讲师', '经济管理'),
('106', '周老师', '女', '1993-03-05', '教授', '物理系');
INSERT INTO Course (Cno, Cname, Tno)
VALUES
('0001', '数据库', '101'),
('0002', '数据结构', '102'),
('0003', '算法分析', '103'),
('0004', '英语', '104'),
('0005', '管理学', '105'),
('0006', '物理实验', '106');
INSERT INTO Score (Sno, Cno, Degree)
VALUES
('001', '0001', 85),
('001', '0002', 90),
('002', '0001', 92),
('002', '0002', 87),
('003', '0001', 88),
('003', '0003', 91),
('004', '0004', 89),
('004', '0005', 92),
('005', '0004', 85),
('005', '0005', 88),
('006', '0005', 91),
('006', '0006', 93);

(1)查询Student表中的所有记录的Sname、 Ssex和Class列。

select Sname, Ssex, Class
from Student;
+--------+------+-----------+
| Sname  | Ssex | Class     |
+--------+------+-----------+
| 张三   || 一年级    |
| 李四   || 二年级    |
| 王五   || 三年级    |
| 赵六   || 四年级    |
| 钱七   || 五年级    |
| 孙八   || 六年级    |
+--------+------+-----------+
6 rows in set (0.00 sec)

(2)查询所有教工部门,即不重复的Depart列。

select distinct Depart
from Teacher;
+-----------------+
| Depart          |
+-----------------+
| 计算机科学      |
| 人文学科        |
| 自然科学        |
| 外语系          |
| 经济管理        |
| 物理系          |
+-----------------+
6 rows in set (0.00 sec)

(3)查询Student表的所有记录。

select *
from Student;
+-----+--------+------+------------+-----------+
| Sno | Sname  | Ssex | Sbirthday  | Class     |
+-----+--------+------+------------+-----------+
| 001 | 张三   || 2000-01-01 | 一年级    |
| 002 | 李四   || 2001-02-03 | 二年级    |
| 003 | 王五   || 2002-03-05 | 三年级    |
| 004 | 赵六   || 2003-04-01 | 四年级    |
| 005 | 钱七   || 2004-05-03 | 五年级    |
| 006 | 孙八   || 2005-06-05 | 六年级    |
+-----+--------+------+------------+-----------+
6 rows in set (0.00 sec)

(4)查询Score表中成绩在80到90之间的所有记录。

select *
from Score
where Degree between 80 and 90;
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 001 | 0001 |   85.0 |
| 001 | 0002 |   90.0 |
| 002 | 0002 |   87.0 |
| 003 | 0001 |   88.0 |
| 004 | 0004 |   89.0 |
| 005 | 0004 |   85.0 |
| 005 | 0005 |   88.0 |
+-----+------+--------+
7 rows in set (0.00 sec)

(5)查询Score表中成绩为85,86或88的记录。

select *
from Score
where Degree in (85, 86, 88);
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 001 | 0001 |   85.0 |
| 003 | 0001 |   88.0 |
| 005 | 0004 |   85.0 |
| 005 | 0005 |   88.0 |
+-----+------+--------+
4 rows in set (0.00 sec)

(6)查询Student表中“三年级”或性别为“女”的同学记录。

select *
from Student
where Class = '三年级' or Ssex = '女';
+-----+--------+------+------------+-----------+
| Sno | Sname  | Ssex | Sbirthday  | Class     |
+-----+--------+------+------------+-----------+
| 002 | 李四   || 2001-02-03 | 二年级    |
| 003 | 王五   || 2002-03-05 | 三年级    |
| 004 | 赵六   || 2003-04-01 | 四年级    |
| 006 | 孙八   || 2005-06-05 | 六年级    |
+-----+--------+------+------------+-----------+
4 rows in set (0.00 sec)

(7)以Sbirthday降序查询Student表的所有记录。

select *
from Student
order by Sbirthday desc;
+-----+--------+------+------------+-----------+
| Sno | Sname  | Ssex | Sbirthday  | Class     |
+-----+--------+------+------------+-----------+
| 006 | 孙八   || 2005-06-05 | 六年级    |
| 005 | 钱七   || 2004-05-03 | 五年级    |
| 004 | 赵六   || 2003-04-01 | 四年级    |
| 003 | 王五   || 2002-03-05 | 三年级    |
| 002 | 李四   || 2001-02-03 | 二年级    |
| 001 | 张三   || 2000-01-01 | 一年级    |
+-----+--------+------+------------+-----------+
6 rows in set (0.00 sec)

(8)以Cno升序、Degree降序查询Score表的所有记录。

select *
from Score
order by Cno, Degree desc;
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 002 | 0001 |   92.0 |
| 003 | 0001 |   88.0 |
| 001 | 0001 |   85.0 |
| 001 | 0002 |   90.0 |
| 002 | 0002 |   87.0 |
| 003 | 0003 |   91.0 |
| 004 | 0004 |   89.0 |
| 005 | 0004 |   85.0 |
| 004 | 0005 |   92.0 |
| 006 | 0005 |   91.0 |
| 005 | 0005 |   88.0 |
| 006 | 0006 |   93.0 |
+-----+------+--------+
12 rows in set (0.00 sec)

(9)查询“一年级”的学生人数。

select count(*)
from Student
where Class = '一年级';
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

(10)查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

  1. 子查询
select Sno, Degree
from Score
where Degree = (
    select max(Degree)
    from Score
);
+-----+--------+
| Sno | Degree |
+-----+--------+
| 006 |   93.0 |
+-----+--------+
1 row in set (0.00 sec)
  1. 排序
select Sno, Degree
from Score
order by Degree desc
limit 1;
+-----+--------+
| Sno | Degree |
+-----+--------+
| 006 |   93.0 |
+-----+--------+
1 row in set (0.00 sec)

(11)查询每门课的平均成绩。

select Cno, avg(Degree)
from Score
group by Cno;
+------+-------------+
| Cno  | avg(Degree) |
+------+-------------+
| 0001 |    88.33333 |
| 0002 |    88.50000 |
| 0003 |    91.00000 |
| 0004 |    87.00000 |
| 0005 |    90.33333 |
| 0006 |    93.00000 |
+------+-------------+
6 rows in set (0.00 sec)

(12)查询Score表中至少有3名学生选修的并以1结尾的课程的平均分数。

select Cno, avg(Degree)
from Score
where Cno like '%1'
group by Cno
having count(*) > 2;
+------+-------------+
| Cno  | avg(Degree) |
+------+-------------+
| 0001 |    88.33333 |
+------+-------------+
1 row in set (0.00 sec)

(13)查询分数大于70,小于90的Sno列。

select distinct Sno
from Score
where Degree > 70 and Degree < 90;
+-----+
| Sno |
+-----+
| 001 |
| 002 |
| 003 |
| 004 |
| 005 |
+-----+
5 rows in set (0.00 sec)

(14)查询所有学生的Sname、Cno和Degree列。

select Sname, Cno, Degree
from Student st
left join Score sc
on st.Sno = sc.Sno;
+--------+------+--------+
| Sname  | Cno  | Degree |
+--------+------+--------+
| 张三   | 0001 |   85.0 |
| 张三   | 0002 |   90.0 |
| 李四   | 0001 |   92.0 |
| 李四   | 0002 |   87.0 |
| 王五   | 0001 |   88.0 |
| 王五   | 0003 |   91.0 |
| 赵六   | 0004 |   89.0 |
| 赵六   | 0005 |   92.0 |
| 钱七   | 0004 |   85.0 |
| 钱七   | 0005 |   88.0 |
| 孙八   | 0005 |   91.0 |
| 孙八   | 0006 |   93.0 |
+--------+------+--------+
12 rows in set (0.00 sec)

(15)查询所有学生的Sno、Cname和Degree列。

select Sno, Cname, Degree
from Course co
right join Score sc
on co.Cno = sc.Cno;
+-----+--------------+--------+
| Sno | Cname        | Degree |
+-----+--------------+--------+
| 001 | 数据库       |   85.0 |
| 001 | 数据结构     |   90.0 |
| 002 | 数据库       |   92.0 |
| 002 | 数据结构     |   87.0 |
| 003 | 数据库       |   88.0 |
| 003 | 算法分析     |   91.0 |
| 004 | 英语         |   89.0 |
| 004 | 管理学       |   92.0 |
| 005 | 英语         |   85.0 |
| 005 | 管理学       |   88.0 |
| 006 | 管理学       |   91.0 |
| 006 | 物理实验     |   93.0 |
+-----+--------------+--------+
12 rows in set (0.00 sec)

(16)查询所有学生的Sname、Cname和Degree列。

select Sname, Cname, Degree
from Student st
left join Score sc
on st.Sno = sc.Sno
left join Course co
on co.Cno = sc.Cno;
+--------+--------------+--------+
| Sname  | Cname        | Degree |
+--------+--------------+--------+
| 张三   | 数据库       |   85.0 |
| 张三   | 数据结构     |   90.0 |
| 李四   | 数据库       |   92.0 |
| 李四   | 数据结构     |   87.0 |
| 王五   | 数据库       |   88.0 |
| 王五   | 算法分析     |   91.0 |
| 赵六   | 英语         |   89.0 |
| 赵六   | 管理学       |   92.0 |
| 钱七   | 英语         |   85.0 |
| 钱七   | 管理学       |   88.0 |
| 孙八   | 管理学       |   91.0 |
| 孙八   | 物理实验     |   93.0 |
+--------+--------------+--------+
12 rows in set (0.00 sec)

(17)查询“二年级”学生的平均分。

select Class, avg(Degree)
from Score sc
join Student st
on sc.Sno = st.Sno
where Class = '二年级';
+-----------+-------------+
| Class     | avg(Degree) |
+-----------+-------------+
| 二年级    |    89.50000 |
+-----------+-------------+
1 row in set (0.00 sec)

(18)查询所有同学的Sno、Cno、degree和rank列。(其中rank为成绩的等级,成绩转换成为等级的规则是:大于等于90分为A、小于90且大于等于80分为B、小于80且大于等于70分为C、小于70且大于等于60分为D、小于60分为E)

Select sc.Sno, Cno, Degree, 
case
	WHEN Degree >= 90 THEN 'A'
    WHEN Degree between 80 and 89 THEN 'B'
    WHEN Degree between 70 and 79 THEN 'C'
    WHEN Degree between 60 and 69 THEN 'D'
    WHEN Degree < 60 THEN 'E'
END rank
from Student st
join Score sc
on st.Sno = sc.Sno;
+-----+------+--------+------+
| Sno | Cno  | Degree | rank |
+-----+------+--------+------+
| 001 | 0001 |   85.0 | B    |
| 001 | 0002 |   90.0 | A    |
| 002 | 0001 |   92.0 | A    |
| 002 | 0002 |   87.0 | B    |
| 003 | 0001 |   88.0 | B    |
| 003 | 0003 |   91.0 | A    |
| 004 | 0004 |   89.0 | B    |
| 004 | 0005 |   92.0 | A    |
| 005 | 0004 |   85.0 | B    |
| 005 | 0005 |   88.0 | B    |
| 006 | 0005 |   91.0 | A    |
| 006 | 0006 |   93.0 | A    |
+-----+------+--------+------+
12 rows in set (0.00 sec)

(20)查询score中选学多门课程的同学中分数为非最高分成绩的记录。

  1. 子查询找出最高分,然后选出排除最高分的成绩的记录
select sc.Sno, Cno, Degree
from Score sc
join (
    select Sno, max(Degree) maxdegree
    from Score
    group by Sno
    having count(*) > 1
) sg
on sc.Sno = sg.Sno
where degree < maxdegree;
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 001 | 0001 |   85.0 |
| 002 | 0002 |   87.0 |
| 003 | 0001 |   88.0 |
| 004 | 0004 |   89.0 |
| 005 | 0004 |   85.0 |
| 006 | 0005 |   91.0 |
+-----+------+--------+
6 rows in set (0.01 sec)

(21)查询成绩高于学号为“001”、课程号为“0001”的成绩的所有记录。

select *
from Score
where Degree > (
    select Degree
    from Score
    where Sno = '001' and Cno = '0001'
);
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 001 | 0002 |   90.0 |
| 002 | 0001 |   92.0 |
| 002 | 0002 |   87.0 |
| 003 | 0001 |   88.0 |
| 003 | 0003 |   91.0 |
| 004 | 0004 |   89.0 |
| 004 | 0005 |   92.0 |
| 005 | 0005 |   88.0 |
| 006 | 0005 |   91.0 |
| 006 | 0006 |   93.0 |
+-----+------+--------+
10 rows in set (0.00 sec)

(22)查询和学号为001的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select Sno, Sname, Sbirthday
from Student
where year(Sbirthday) = (
    select year(Sbirthday)
    from Student
    where Sno = '001'
);
+-----+--------+------------+
| Sno | Sname  | Sbirthday  |
+-----+--------+------------+
| 001 | 张三   | 2000-01-01 |
+-----+--------+------------+
1 row in set (0.00 sec)

(23)查询“张老师”教师任课的学生成绩。

select Sno, sc.Cno, Degree, Tname
from Teacher te
join Course co
on te.Tno = co.Tno
join Score sc
on co.Cno = sc.Cno
where Tname = '张老师';
+-----+------+--------+-----------+
| Sno | Cno  | Degree | Tname     |
+-----+------+--------+-----------+
| 001 | 0001 |   85.0 | 张老师    |
| 002 | 0001 |   92.0 | 张老师    |
| 003 | 0001 |   88.0 | 张老师    |
+-----+------+--------+-----------+
3 rows in set (0.00 sec)

(24)查询选修某课程的同学人数多于2人的教师姓名。

  1. 子查询找选修人数大于2的课程号,然后和教师表和课程表做内连接
select Tname
from Teacher te
join Course co
on te.Tno = co.Tno
join (
    select Cno
    from Score
    group by Cno
    having count(*) > 2
) sg
on co.Cno = sg.Cno;
+-----------+
| Tname     |
+-----------+
| 张老师    |
| 陈老师    |
+-----------+
2 rows in set (0.00 sec)
  1. 子查询找选修人数大于2的课程号,连接教师表和课程表,通过 where 找出在子查询结果集中的课程号
select Tname
from Teacher te
join Course co
on te.Tno = co.Tno
where Cno in (
    select Cno
    from Score
    group by Cno
    having count(*) > 2
);
+-----------+
| Tname     |
+-----------+
| 张老师    |
| 陈老师    |
+-----------+
2 rows in set (0.00 sec)

(25)查询一年级和二年级全体学生的记录。

select *
from Student
where Class in ('一年级', '二年级');
+-----+--------+------+------------+-----------+
| Sno | Sname  | Ssex | Sbirthday  | Class     |
+-----+--------+------+------------+-----------+
| 001 | 张三   || 2000-01-01 | 一年级    |
| 002 | 李四   || 2001-02-03 | 二年级    |
+-----+--------+------+------------+-----------+
2 rows in set (0.00 sec)

(26)查询存在有90分以上成绩的课程Cno.

  1. 使用 where 和 distinct
select distinct Cno
from Score
where Degree > 90;
+------+
| Cno  |
+------+
| 0001 |
| 0003 |
| 0005 |
| 0006 |
+------+
4 rows in set (0.00 sec)
  1. 使用 group by 和 having
select Cno
from Score
group by Cno
having max(Degree) > 90;
+------+
| Cno  |
+------+
| 0001 |
| 0003 |
| 0005 |
| 0006 |
+------+
4 rows in set (0.00 sec)

(27)查询出“计算机科学”教师所教课程的成绩表。

select Sno, sc.Cno, Degree, Depart
from Teacher te
join Course co
on te.Tno = co.Tno
join Score sc
on co.Cno = sc.Cno
where Depart = '计算机科学';
+-----+------+--------+-----------------+
| Sno | Cno  | Degree | Depart          |
+-----+------+--------+-----------------+
| 001 | 0001 |   85.0 | 计算机科学      |
| 002 | 0001 |   92.0 | 计算机科学      |
| 003 | 0001 |   88.0 | 计算机科学      |
+-----+------+--------+-----------------+
3 rows in set (0.00 sec)

(28)查询“计算机科学”与“物理系”各职称的教师人数。

select Depart, Prof, count(*) 人数
from Teacher
where Depart in ('计算机科学', '物理系')
group by Depart, Prof;
+-----------------+--------+--------+
| Depart          | Prof   | 人数   |
+-----------------+--------+--------+
| 物理系          | 教授   |      1 |
| 计算机科学      | 教授   |      1 |
+-----------------+--------+--------+
2 rows in set (0.00 sec)

(29)查询“0001”号课程中成绩高于“0004”号课程的任意一个成绩的Cno、Sno和Degree,并按Degree从高到低次序排序。

  1. 使用 any
select Cno, Sno, Degree
from Score
where Cno = '0001' and Degree > any (
    select Degree
    from Score
    where Cno = '0004'
)
order by Degree desc;
+------+-----+--------+
| Cno  | Sno | Degree |
+------+-----+--------+
| 0001 | 002 |   92.0 |
| 0001 | 003 |   88.0 |
+------+-----+--------+
2 rows in set (0.00 sec)
  1. 使用 min
select Cno, Sno, Degree
from Score
where Cno = '0001' and Degree > (
    select min(Degree)
    from Score
    where Cno = '0004'
)
order by Degree desc;
+------+-----+--------+
| Cno  | Sno | Degree |
+------+-----+--------+
| 0001 | 002 |   92.0 |
| 0001 | 003 |   88.0 |
+------+-----+--------+
2 rows in set (0.01 sec)

(30)查询“0001”号课程中成绩高于“0004”号课程的所有成绩的Cno、Sno和Degree

  1. 使用 all
select Cno, Sno, Degree
from Score
where Cno = '0001' and Degree > all (
    select Degree
    from Score
    where Cno = '0004'
)
order by Degree desc;
+------+-----+--------+
| Cno  | Sno | Degree |
+------+-----+--------+
| 0001 | 002 |   92.0 |
+------+-----+--------+
1 row in set (0.00 sec)
  1. 使用 max
select Cno, Sno, Degree
from Score
where Cno = '0001' and Degree > (
    select max(Degree)
    from Score
    where Cno = '0004'
)
order by Degree desc;
+------+-----+--------+
| Cno  | Sno | Degree |
+------+-----+--------+
| 0001 | 002 |   92.0 |
+------+-----+--------+
1 row in set (0.00 sec)

(31)查询所有教师和同学的name、sex和birthday.

select Sname name, Ssex sex, Sbirthday birthday
from Student
union
select Tname, Tsex, Tbirthday
from Teacher;
+-----------+-----+------------+
| name      | sex | birthday   |
+-----------+-----+------------+
| 张三      || 2000-01-01 |
| 李四      || 2001-02-03 |
| 王五      || 2002-03-05 |
| 赵六      || 2003-04-01 |
| 钱七      || 2004-05-03 |
| 孙八      || 2005-06-05 |
| 张老师    || 1980-01-01 |
| 李老师    || 1985-02-03 |
| 王老师    || 1990-03-05 |
| 刘老师    || 1983-01-01 |
| 陈老师    || 1988-02-03 |
| 周老师    || 1993-03-05 |
+-----------+-----+------------+
12 rows in set (0.00 sec)

(32)查询所有“女”教师和“女”同学的name、sex和birthday.

select Sname name, Ssex sex, Sbirthday birthday
from Student
where Ssex = '女'
union
select Tname, Tsex, Tbirthday
from Teacher
where Tsex = '女';
+-----------+-----+------------+
| name      | sex | birthday   |
+-----------+-----+------------+
| 李四      || 2001-02-03 |
| 赵六      || 2003-04-01 |
| 孙八      || 2005-06-05 |
| 李老师    || 1985-02-03 |
| 刘老师    || 1983-01-01 |
| 周老师    || 1993-03-05 |
+-----------+-----+------------+
6 rows in set (0.00 sec)

(33) 查询成绩比该课程平均成绩低的同学的成绩表。

select Sno, sc.Cno, Degree
from Score sc
join (
    select Cno, avg(Degree) avgDegree
    from Score
    group by Cno
) cg
on sc.Cno = cg.Cno
where Degree < avgDegree;
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 001 | 0001 |   85.0 |
| 002 | 0002 |   87.0 |
| 003 | 0001 |   88.0 |
| 005 | 0004 |   85.0 |
| 005 | 0005 |   88.0 |
+-----+------+--------+
5 rows in set (0.00 sec)

(34)查询所有任课教师的Tname和Depart.

select Tname, Depart
from Teacher
where Tno in (
    select distinct Tno
    from Course
);
+-----------+-----------------+
| Tname     | Depart          |
+-----------+-----------------+
| 张老师    | 计算机科学      |
| 李老师    | 人文学科        |
| 王老师    | 自然科学        |
| 刘老师    | 外语系          |
| 陈老师    | 经济管理        |
| 周老师    | 物理系          |
+-----------+-----------------+
6 rows in set (0.00 sec)

(35)查询所有未讲课的教师的Tname和Depart.

select Tname, Depart
from Teacher
where Tno not in (
    select distinct Tno
    from Course
);

(36)查询至少有2名男生的班级。

select Class
from Student
where Ssex = '男'
group by Class
having count(*) >= 2;

(37)查询Student表中不姓“王”的同学记录。

select *
from Student
where Sname not like '王%';
+-----+--------+------+------------+-----------+
| Sno | Sname  | Ssex | Sbirthday  | Class     |
+-----+--------+------+------------+-----------+
| 001 | 张三   || 2000-01-01 | 一年级    |
| 002 | 李四   || 2001-02-03 | 二年级    |
| 004 | 赵六   || 2003-04-01 | 四年级    |
| 005 | 钱七   || 2004-05-03 | 五年级    |
| 006 | 孙八   || 2005-06-05 | 六年级    |
+-----+--------+------+------------+-----------+
5 rows in set (0.01 sec)

(38)查询Student表中每个学生的姓名和年龄。

select Sname, floor(datediff(now(), Sbirthday) / 365) age
from Student;
+--------+------+
| Sname  | age  |
+--------+------+
| 张三   |   23 |
| 李四   |   22 |
| 王五   |   21 |
| 赵六   |   20 |
| 钱七   |   18 |
| 孙八   |   17 |
+--------+------+
6 rows in set (0.00 sec)

(39)查询Student表中最大和最小的Sbirthday日期值。

select max(Sbirthday), min(Sbirthday)
from Student;
+----------------+----------------+
| max(Sbirthday) | min(Sbirthday) |
+----------------+----------------+
| 2005-06-05     | 2000-01-01     |
+----------------+----------------+
1 row in set (0.00 sec)

(40)以年龄从小到大的顺序查询Student表中的全部记录。

select Sno, Sname, Ssex, Class, floor(datediff(now(), Sbirthday) / 365) age
from Student
order by age;
+-----+--------+------+-----------+------+
| Sno | Sname  | Ssex | Class     | age  |
+-----+--------+------+-----------+------+
| 006 | 孙八   || 六年级    |   17 |
| 005 | 钱七   || 五年级    |   18 |
| 004 | 赵六   || 四年级    |   20 |
| 003 | 王五   || 三年级    |   21 |
| 002 | 李四   || 二年级    |   22 |
| 001 | 张三   || 一年级    |   23 |
+-----+--------+------+-----------+------+
6 rows in set (0.00 sec)

(41)查询“男”教师及其所上的课程。

select te.Tno, Tname, Tsex, Cno, Cname
from Teacher te
join Course co
on te.Tno = co.Tno
where Tsex = '男';
+-----+-----------+------+------+--------------+
| Tno | Tname     | Tsex | Cno  | Cname        |
+-----+-----------+------+------+--------------+
| 101 | 张老师    || 0001 | 数据库       |
| 103 | 王老师    || 0003 | 算法分析     |
| 105 | 陈老师    || 0005 | 管理学       |
+-----+-----------+------+------+--------------+
3 rows in set (0.00 sec)

(42)查询最高分同学的Sno、Cno和Degree列。

select *
from Score
order by Degree desc
limit 1;
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 006 | 0006 |   93.0 |
+-----+------+--------+
1 row in set (0.00 sec)

(43)查询和“李四”同性别的所有同学的Sname.

select Sname
from Student
where Ssex = (
    select Ssex
    from Student
    where Sname = '李四'
);
+--------+
| Sname  |
+--------+
| 李四   |
| 赵六   |
| 孙八   |
+--------+
3 rows in set (0.00 sec)

(44)查询和“李四”同性别并同班的同学Sname.

select Sname
from Student
where (Ssex, Class) = (
    select Ssex, Class
    from Student
    where Sname = '李四'
);
+--------+
| Sname  |
+--------+
| 李四   |
+--------+
1 row in set (0.00 sec)

(45)查询所有选修“数据库”课程的“女”同学的成绩表。

select sc.Sno, sc.Cno, Degree
from Score sc
join Student st
on sc.Sno = st.Sno
join Course co
on sc.Cno = co.Cno
where Cname = '数据库' and Ssex = '女';
+-----+------+--------+
| Sno | Cno  | Degree |
+-----+------+--------+
| 002 | 0001 |   92.0 |
+-----+------+--------+
1 row in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世真

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值