mysql sql查询语句练习

转自:https://blog.csdn.net/cyjs1988/article/details/74908905

Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
--建表:

CREATE TABLE Student
(
`S#` int auto_increment primary key,
`Sname` varchar(20),
`Sage` int,
`Ssex` varchar(2)
)

 

CREATE TABLE Course(
`C#` int auto_increment primary key,
`Cname` varchar(255),
`T#` int);

 

CREATE TABLE SC
(
`S#` int,
`C#` int,
`score` varchar(50)
)

 

CREATE TABLE Teacher
(
`T#` int auto_increment primary key,
`Tname` varchar(20)
)




--1、查询“001”课程比“002”课程成绩高的所有学生的学号;

select *
from (select `s#`,`c#`,`score` from sc where `C#`='1') a,
(select `s#`,`c#`,`score` from SC where `C#`='2') b
where a.score+0>b.score+0 and a.`S#`=b.`S#`;

 

select *
from (select `s#`,`c#`,`score` s1 from sc where `C#`='1') a
JOIN (select `s#`,`c#`,`score` s2 from SC where `C#`='2') b
ON a.`S#`=b.`S#`
where s1+0>s2+0 ;

 因为字符串中是数字时是按照最后一位比较的,解决方法也很简单——+0。

 

--2、查询平均成绩大于60分的同学的学号和平均成绩;
select S#,avg(score)
from sc
group by S# having avg(score) >60;

HAVING语句通常与GROUP BY语句联合使用,用来过滤由GROUP BY语句返回的记录集。

HAVING语句的存在弥补了WHERE关键字不能与聚合函数联合使用的不足。


--3、查询所有同学的学号、姓名、选课数、总成绩;

select Student.`S#`,Student.Sname,count(SC.`C#`),sum(score)
from Student left Outer join SC on Student.`S#`=SC.`S#`
group by Student.`S#`,Sname

 

select a.`S#`,Sname,count(`C#`),sum(score)
FROM student a
left JOIN sc b
ON a.`S#`=b.`S#`
GROUP BY a.`S#`


--4、查询姓“李”的老师的个数;

select count(distinct(Tname))
from Teacher
where Tname like '李%';

distinct去重


--5、查询没学过“叶平”老师课的同学的学号、姓名;

select Student.`S#`,Student.Sname
from Student
where `S#` not in (select distinct( SC.`S#`) from SC,Course,Teacher where SC.`C#`=Course.`C#` and Teacher.`T#`=Course.`T#` and Teacher.Tname='叶平');


--6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select Student.`S#`,Student.Sname
FROM Student,SC
WHERE Student.`S#`=SC.`S#` and SC.`C#`='1' and exists( Select * from SC as SC_2 where SC_2.`S#`=SC.`S#` and SC_2.`C#`='2');


--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select S#,Sname
from Student
where S# in
(select S#
from SC ,Course ,Teacher
where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=’叶平’ group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname=’叶平’));

 

select DISTINCT(student.`S#`),student.Sname,sc.`C#`
from sc,student
where `C#` in
(select course.`C#`
from teacher,course
where teacher.`T#`=course.`T#` and Tname='叶平');

 


--8、查询所有课程成绩小于60分的同学的学号、姓名;
select S#,Sname
from Student
where S# not in (select Student.S# from Student,SC where S.S#=SC.S# and score>60);

 

select student.`S#`,student.Sname,sc.score
from sc,student
where Student.`S#`=SC.`S#` and score<60

 


--9、查询没有学全所有课的同学的学号、姓名;

select student.`S#`,student.Sname
from sc,student
where Student.`S#`=SC.`S#`
GROUP BY student.`S#`,student.Sname HAVING COUNT(`C#`) <(SELECT COUNT(`C#`) FROM course);


--10、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

select DISTINCT(student.`S#`),Student.Sname
from Student,SC
where Student.`S#`=SC.`S#`
and SC.`C#` in (SELECT `C#` FROM SC WHERE SC.`S#`='1');


--11、删除学习“叶平”老师课的SC表记录;
Delect SC
from course ,Teacher
where Course.C#=SC.C# and Course.T#= Teacher.T# and Tname='叶平';
--12、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT L.C# --课程ID,L.score 最高分,R.score 最低分
FROM SC L ,SC R
WHERE L.C# = R.C#
and
L.score = (SELECT MAX(IL.score)
FROM SC IL,Student IM
WHERE IL.C# = L.C# and IM.S#=IL.S#
GROUP BY IL.C#)
and
R.Score = (SELECT MIN(IR.score)
FROM SC IR
WHERE IR.C# = R.C#
GROUP BY IR.C# );

 

select SC.`C#` 课程ID,MAX(score+0) 最高分,min(score+0) 最低分
FROM sc
GROUP BY SC.`C#`


--13、查询学生平均成绩及其名次

SELECT 1+(SELECT COUNT( distinct 平均成绩)
FROM (SELECT `S#`,AVG(score) 平均成绩
FROM SC
GROUP BY `S#` ) T1
WHERE 平均成绩 > T2.平均成绩) 名次, `S#` 学生学号,平均成绩
FROM (SELECT `S#`,AVG(score) 平均成绩 FROM SC GROUP BY `S#` ) T2
ORDER BY 平均成绩 desc;


--14、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数
FROM SC t1
WHERE score IN (SELECT TOP 3 score
FROM SC
WHERE t1.C#= C#
ORDER BY score DESC)
ORDER BY t1.C#;
--15、查询每门功成绩最好的前两名
SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数
FROM SC t1
WHERE score IN (SELECT TOP 2 score
FROM SC
WHERE t1.C#= C#
ORDER BY score DESC )
ORDER BY t1.C#;
--补充:
--已经知道原表
year salary
——————
2000 1000
2001 2000
2002 3000
2003 4000
--解:
select b.year,sum(a.salary)
from salary a,salary b
where a.year<=b.year
group by b.year
order by b.year;
--在面试过程中多次碰到一道SQL查询的题目,查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:
--方法一:
select top 10 *
from A
where ID >(select max(ID) from (select top 30 ID from A order by ID ) T) order by ID
--方法二:
select top 10 *
from A
where ID not In (select top 30 ID from A order by ID)
order by ID

 

注意:

我们知道,在ms sql server中或access中,
若要查询前10条记录,使用top 10即可,
但在mysql中不支持这个写法,它用limit 10。 
 
我们可以利用MySQL中SELECT支持的一个子句——LIMIT——来完成这项功能。 
LIMIT可以实现top N查询,也可以实现M至N(某一段)的记录查询,具体语法如下: 

SELECT * FROM MYTABLE 

ORDER BY ID 

LIMIT offset, recnum

其中offset为从第几条(M+1)记录开始(即下标从0开始),recnum为返回的记录条数。例: 

select * from mytable
order by afield
limit 2, 5

即意为,从第3条记录开始的5条记录。

 

 

 

【第二部分】

 

 

Sutdent表的定义

字段名

字段描述

数据类型

主键

外键

非空

唯一

自增

Id

学号

INT(10)

Name

姓名

VARCHAR(20)

Sex

性别

VARCHAR(4)

Birth

出生年份

YEAR

Department

院系

VARCHAR(20)

Address

家庭住址

VARCHAR(50)

 

Score表的定义

字段名

字段描述

数据类型

主键

外键

非空

唯一

自增

Id

编号

INT(10)

Stu_id

学号

INT(10)

C_name

课程名

VARCHAR(20)

Grade

分数

INT(10)

 

--1.创建student和score表
CREATE TABLE student (
id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4) ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50)
);
--创建score表。SQL代码如下:
CREATE TABLE score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);
--2.为student表和score表增加记录
--向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
--向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);

--3.查询student表的所有记录
mysql> SELECT * FROM student;
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+

--4.查询student表的第2条到4条记录
mysql> SELECT * FROM student LIMIT 1,3;
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
+-----+--------+------+-------+------------+--------------+

--5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
mysql> SELECT id,name,department FROM student;
+-----+--------+------------+
| id | name | department |
+-----+--------+------------+
| 901 | 张老大 | 计算机系 |
| 902 | 张老二 | 中文系 |
| 903 | 张三 | 中文系 |
| 904 | 李四 | 英语系 |
| 905 | 王五 | 英语系 |
| 906 | 王六 | 计算机系 |
+-----+--------+------------+

--6.从student表中查询计算机系和英语系的学生的信息
mysql> SELECT * FROM student WHERE department IN ('计算机系','英语系');
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+

--7.从student表中查询年龄18~22岁的学生信息
mysql> SELECT id,name,sex,2013-birth AS age,department,address
-> FROM student
-> WHERE 2013-birth BETWEEN 18 AND 22;
+-----+------+------+------+------------+--------------+
| id | name | sex | age | department | address |
+-----+------+------+------+------------+--------------+
| 905 | 王五 | 女 | 22 | 英语系 | 福建省厦门市 |
+-----+------+------+------+------------+--------------+
mysql> SELECT id,name,sex,2013-birth AS age,department,address
-> FROM student
-> WHERE 2013-birth>=18 AND 2013-birth<=22;
+-----+------+------+------+------------+--------------+
| id | name | sex | age | department | address |
+-----+------+------+------+------------+--------------+
| 905 | 王五 | 女 | 22 | 英语系 | 福建省厦门市 |
+-----+------+------+------+------------+--------------+

--8.从student表中查询每个院系有多少人
mysql> SELECT department, COUNT(id) FROM student GROUP BY department;
+------------+-----------+
| department | COUNT(id) |
+------------+-----------+
| 计算机系 | 2 |
| 英语系 | 2 |
| 中文系 | 2 |
+------------+-----------+
--9.从score表中查询每个科目的最高分
mysql> SELECT c_name,MAX(grade) FROM score GROUP BY c_name;
+--------+------------+
| c_name | MAX(grade) |
+--------+------------+
| 计算机 | 98 |
| 英语 | 94 |
| 中文 | 95 |
+--------+------------+

--10.查询李四的考试科目(c_name)和考试成绩(grade)
mysql> SELECT c_name, grade
-> FROM score WHERE stu_id=
-> (SELECT id FROM student
-> WHERE name= '李四' );
+--------+-------+
| c_name | grade |
+--------+-------+
| 计算机 | 70 |
| 英语 | 92 |
+--------+-------+

--11.用连接的方式查询所有学生的信息和考试信息
mysql> SELECT student.id,name,sex,birth,department,address,c_name,grade
-> FROM student,score
-> WHERE student.id=score.stu_id;
+-----+--------+------+-------+------------+--------------+--------+-------+
| id | name | sex | birth | department | address | c_name | grade |
+-----+--------+------+-------+------------+--------------+--------+-------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 计算机 | 98 |
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 英语 | 80 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 计算机 | 65 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 中文 | 88 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 | 中文 | 95 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 计算机 | 70 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 英语 | 92 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 | 英语 | 94 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 计算机 | 90 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 英语 | 85 |
+-----+--------+------+-------+------------+--------------+--------+-------+

--12.计算每个学生的总成绩
mysql> SELECT student.id,name,SUM(grade) FROM student,score
-> WHERE student.id=score.stu_id
-> GROUP BY id;
+-----+--------+------------+
| id | name | SUM(grade) |
+-----+--------+------------+
| 901 | 张老大 | 178 |
| 902 | 张老二 | 153 |
| 903 | 张三 | 95 |
| 904 | 李四 | 162 |
| 905 | 王五 | 94 |
| 906 | 王六 | 175 |
+-----+--------+------------+

--13.计算每个考试科目的平均成绩
mysql> SELECT c_name,AVG(grade) FROM score GROUP BY c_name;
+--------+------------+
| c_name | AVG(grade) |
+--------+------------+
| 计算机 | 80.7500 |
| 英语 | 87.7500 |
| 中文 | 91.5000 |
+--------+------------+

--14.查询计算机成绩低于95的学生信息
mysql> SELECT * FROM student
-> WHERE id IN
-> (SELECT stu_id FROM score
-> WHERE c_name="计算机" and grade<95);
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+

--15.查询同时参加计算机和英语考试的学生的信息
mysql> SELECT * FROM student
-> WHERE id =ANY
-> ( SELECT stu_id FROM score
-> WHERE stu_id IN (
-> SELECT stu_id FROM
-> score WHERE c_name= '计算机')
-> AND c_name= '英语' );
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+
mysql> SELECT a.* FROM student a ,score b ,score c
-> WHERE a.id=b.stu_id
-> AND b.c_name='计算机'
-> AND a.id=c.stu_id
-> AND c.c_name='英语';
+-----+--------+------+-------+------------+--------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+------------+--------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+

--16.将计算机考试成绩按从高到低进行排序
mysql> SELECT stu_id, grade
-> FROM score WHERE c_name= '计算机'
-> ORDER BY grade DESC;
+--------+-------+
| stu_id | grade |
+--------+-------+
| 901 | 98 |
| 906 | 90 |
| 904 | 70 |
| 902 | 65 |
+--------+-------+

--17.从student表和score表中查询出学生的学号,然后合并查询结果
mysql> SELECT id FROM student
-> UNION
-> SELECT stu_id FROM score;
+-----+
| id |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+-----+

--18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
mysql> SELECT student.id, name,sex,birth,department, address, c_name,grade
-> FROM student, score
-> WHERE
-> (name LIKE '张%' OR name LIKE '王%')
-> AND
-> student.id=score.stu_id ;
+-----+--------+------+-------+------------+--------------+--------+-------+
| id | name | sex | birth | department | address | c_name | grade |
+-----+--------+------+-------+------------+--------------+--------+-------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 计算机 | 98 |
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 英语 | 80 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 计算机 | 65 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 中文 | 88 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 | 中文 | 95 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 | 英语 | 94 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 计算机 | 90 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 英语 | 85 |
+-----+--------+------+-------+------------+--------------+--------+-------+

--19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
mysql> SELECT student.id, name,sex,birth,department, address, c_name,grade
-> FROM student, score
-> WHERE address LIKE '湖南%' AND
-> student.id=score.stu_id;
+-----+------+------+-------+------------+--------------+--------+-------+
| id | name | sex | birth | department | address | c_name | grade |
+-----+------+------+-------+------------+--------------+--------+-------+
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 | 中文 | 95 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 计算机 | 90 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 英语 | 85 |
+-----+------+------+-------+------------+--------------+--------+-------+

 

 

 

#查询所有科目都及格的学生
SELECT * FROM stu
SELECT * FROM stu WHERE name not in (SELECT name FROM stu WHERE score < 60)

 

#查找各科成绩前两名

SELECT a.*
FROM stu a
WHERE 2 > (
SELECT COUNT(DISTINCT t.name) FROM stu t WHERE t.subject = a.subject AND t.score > a.score
)
ORDER BY
a.subject,a.score desc;

 

#语文考得比数学好的学生
SELECT * from stu as a,stu as b
where a.name=b.name
and a.subject="语文"
and b.subject="数学"
and a.score > b.score

 

#用一条SQL语句查询出每门课都大于80分的学生姓名

SELECT DISTINCT name FROM stu WHERE name not in(SELECT name FROM stu where score < 60)

 

#courseid coursename score mark
select *,(CASE WHEN a.score<60 THEN 'fail' ELSE 'pass' END) as mark from stu a

posted @ 2018-08-02 17:41  sjxm2017  阅读(600)  评论(0编辑  收藏  举报