建表语句
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
50题
SELECT
st.s_id
FROM Student st
INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id = '01') a ON st.s_id = a.s_id
INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id = '02') b ON a.s_id = b.s_id
WHERE a.s_score > b.s_score
SELECT
s_id,
avg(s_score)
FROM Score
GROUP BY s_id
HAVING avg(s_score) > 60
SELECT
st.s_id, st.s_name,
count(sc.c_id) 'num',
sum(sc.s_score) 'sum'
FROM Student st
LEFT JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
SELECT
count(t_id)
FROM Teacher
WHERE t_name like '猴%'
SELECT s_id, s_name
FROM Student
WHERE s_id not in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)
SELECT s_id, s_name
FROM Student
WHERE s_id in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)
SELECT st.*
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id = '01'
AND st.s_id in (SELECT s_id FROM Score WHERE c_id = '02')
SELECT sum(s_score) 'total'
FROM Score
WHERE c_id = '02'
SELECT *
FROM Student
WHERE s_id not in (
SELECT s_id
FROM Score
WHERE s_score > 60
)
SELECT st.*
FROM Student st
LEFT JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING count(sc.c_id) < (SELECT count(c_id) FROM Course)
SELECT
DISTINCT st.*
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id in (SELECT c_id FROM Score WHERE s_id = '01')
AND st.s_id <> '01'
SELECT s_id
FROM Score
WHERE s_id <> '01'
GROUP BY s_id
HAVING GROUP_CONCAT(c_id) = (SELECT GROUP_CONCAT(c_id) FROM Score WHERE s_id = '01')
SELECT s_id, s_name
FROM Student
WHERE s_id not in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)
SELECT
st.s_id, st.s_name,
avg(sc.s_score)
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING count(IF(sc.s_score < 60, 1, null)) >= 2
SELECT
*
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id = '01' AND sc.s_score < 60
ORDER BY sc.s_score desc
SELECT
s_id,
max(case when c_id = '01' then s_score else null end) '01',
max(case when c_id = '02' then s_score else null end) '02',
max(case when c_id = '03' then s_score else null end) '03',
avg(s_score)
FROM Score
GROUP BY s_id
ORDER BY avg(s_score) desc
SELECT
sc.c_id, co.c_name,
max(sc.s_score) 'max',
min(sc.s_score) 'min',
avg(sc.s_score) 'avg',
avg(case when sc.s_score >= 60 then 1 else 0 end) '及格率',
avg(case when sc.s_score >= 70 then 1 else 0 end) '中等率',
avg(case when sc.s_score >= 80 then 1 else 0 end) '优良率',
avg(case when sc.s_score >= 90 then 1 else 0 end) '优秀率'
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
GROUP BY sc.c_id
SELECT
*,
ROW_NUMBER() over(ORDER BY s_score desc) 'r'
FROM Score
SELECT
s_id,
sum(s_score)
FROM Score
GROUP BY s_id
ORDER BY sum(s_score) desc
SELECT
sc.c_id,
te.t_name,
avg(s_score)
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
GROUP BY sc.c_id
ORDER BY avg(sc.s_score) desc
SELECT
st.*,
a.s_score,
a.r
FROM Student st
INNER JOIN (
SELECT
s_id, s_score,
ROW_NUMBER() over(PARTITION BY c_id ORDER BY s_score desc) 'r'
FROM Score) a ON st.s_id = a.s_id AND r in (2, 3)
SELECT
sc.c_id, co.c_name,
sum(case when sc.s_score >= 0 and sc.s_score < 60 then 1 else 0 end) '[<60]',
sum(case when sc.s_score >= 60 and sc.s_score < 70 then 1 else 0 end) '[70-60]',
sum(case when sc.s_score >= 70 and sc.s_score < 85 then 1 else 0 end) '[85-70]',
sum(case when sc.s_score >= 85 and sc.s_score <= 100 then 1 else 0 end) '[100-85]'
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
GROUP BY sc.c_id
SELECT
s_id,
avg(s_score),
ROW_NUMBER() over(ORDER BY avg(s_score) desc) 'r'
FROM Score
GROUP BY s_id
SELECT
c_id,
max(case when a.r = 1 then a.s_score else null end) 'NO.1',
max(case when a.r = 2 then a.s_score else null end) 'NO.2',
max(case when a.r = 3 then a.s_score else null end) 'NO.3'
FROM(
SELECT
*,
ROW_NUMBER() over(PARTITION BY c_id ORDER BY s_score desc) 'r'
FROM Score) a
GROUP BY c_id
SELECT
count(distinct s_id) 'num'
FROM Score
GROUP BY c_id
SELECT
st.s_id, st.s_name
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING count(sc.c_id) = 2
SELECT
s_sex,
count(s_id)
FROM Student
GROUP BY s_sex
SELECT *
FROM Student
WHERE s_name like '%风%'
SELECT *
FROM Student
WHERE YEAR(s_birth) = '1990'
SELECT
st.s_id, st.s_name,
avg(sc.s_score) 'avg'
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING avg(sc.s_score) >= 85
SELECT
c_id, avg(s_score)
FROM Score
GROUP BY c_id
ORDER BY avg(s_score), c_id desc
SELECT st.s_name, sc.s_score
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
INNER JOIN Course co ON sc.c_id = co.c_id
WHERE co.c_name = '数学' AND sc.s_score < 60
SELECT
s_id,
max(case when c_id = '01' then s_score else null end) '01',
max(case when c_id = '02' then s_score else null end) '02',
max(case when c_id = '03' then s_score else null end) '03'
FROM Score
GROUP BY s_id
SELECT
st.s_name, co.c_name, sc.s_score
FROM Student st
INNER JOIN Score sc on st.s_id = sc.s_id
INNER JOIN Course co on sc.c_id = co.c_id
WHERE sc.s_score > 70
SELECT
c_id,
s_score
FROM Score
WHERE s_score < 60
ORDER BY c_id desc
SELECT
st.s_id, st.s_name
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id = '03' AND sc.s_score > 80
SELECT
c_id,
count(s_id)
FROM Score
GROUP BY c_id
SELECT
st.s_name, sc.s_score
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
ORDER BY sc.s_score desc
limit 1
SELECT
s1.s_id, s1.c_id, s2.c_id, s1.s_score
FROM Score s1
INNER JOIN Score s2 ON s1.s_id = s2.s_id
AND s1.c_id <> s2.c_id AND s1.s_score = s2.s_score
SELECT *
FROM
(
SELECT
*,
ROW_NUMBER() over(PARTITION BY c_id ORDER BY s_score desc) 'r'
FROM Score)a
WHERE a.r in (1,2)
SELECT
c_id, count(s_id)
FROM Score
GROUP BY c_id
HAVING count(s_id) > 5
ORDER BY count(s_id), c_id
SELECT s_id
FROM Score
GROUP BY s_id
HAVING count(c_id) >= 2
SELECT
st.*
FROM Score sc
INNER JOIN Student st on st.s_id = sc.s_id
GROUP BY sc.s_id
HAVING count(c_id) = (SELECT count(1) FROM Course)
SELECT s_id, s_name
FROM Student
WHERE s_id not in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)
SELECT
s_id, avg(s_score)
FROM Score
GROUP BY s_id
HAVING count(if(s_score < 60, 1, null)) >= 2