我们在上篇文章开始介绍MySQL经典的面试题,我们接着上篇文章进行介绍剩下的25题。
select c_id,count(s_id) 选修人数
from sc
group by c_id;
#要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c_id,count(s_id) 选修人数
from sc
group by c_id
having count(s_id)>5;
select s_name,count(s_name)-1 同名人数
from stu
group by s_name;
select s_name,c_name,score
from stu
join sc on stu.s_id=sc.s_id
join co on sc.c_id=co.c_id
where score>70;
select s_name,c_name,score
from stu
join sc on stu.s_id=sc.s_id
join co on sc.c_id=co.c_id
where stu.s_id in (select s_id from sc where score>70);
select c_name,sc.c_id,score
from sc join co on sc.c_id=co.c_id
where score<60;
select distinct t1.*
from sc t1 join sc t2 on t1.s_id=t2.s_id and t1.c_id<>t2.c_id and t1.score=t2.score;
select *
from stu
where week(s_birth)=week(curdate());
select *
from stu
where week(s_birth)=if(week(curdate())=54,1,week(curdate())+1);
select *
from stu
where month(s_birth)=month(curdate());
select *
from stu
where month(s_birth)=if(month(curdate())=12,1,month(curdate())+1);
select stu.s_id, stu.s_name, count(sc.c_id), sum(score)
from sc
left join co on sc.c_id = co.c_id
right join stu on sc.s_id = stu.s_id
group by stu.s_id, stu.s_name;
SELECT stu.s_id,stu.s_name,COUNT(sc.c_id),
CASE WHEN SUM(sc.score) IS NULL THEN 0.00 ELSE ROUND(SUM(sc.score),2) END AS '总成绩'
FROM stu
LEFT JOIN sc ON sc.s_id=stu.s_id
GROUP BY stu.s_id;
SELECT st.s_name
FROM stu st
WHERE st.s_id
NOT IN (
SELECT sc.s_id
FROM sc
INNER JOIN co ON co.c_id=sc.c_id
INNER JOIN te ON te.t_id=co.t_id AND te.t_name="张三"
);
select stu.s_name
from stu
where stu.s_id not in
(select sc.s_id
from sc,co,te
where sc.c_id=co.c_id and co.t_id=te.t_id and te.t_name='张三');
SELECT stu.s_id,stu.s_name,AVG(sc.score) 平均成绩
FROM stu
LEFT JOIN sc ON sc.s_id=stu.s_id
WHERE sc.s_id IN
(
SELECT sc.s_id
FROM sc
WHERE sc.score<60 OR sc.score IS NULL
GROUP BY sc.s_id HAVING COUNT(1)>=2
)
GROUP BY stu.s_id;
SELECT stu.*
FROM stu
LEFT JOIN sc ON sc.s_id=stu.s_id
WHERE sc.c_id='01' AND sc.score<60
ORDER BY sc.score DESC ;
select *
from stu
where s_id in (
select s_id
from sc
where c_id = '01' and score < 60
order by score desc
);
SELECT te.t_id,te.t_name,co.c_name,AVG(sc.score) 平均成绩
FROM te
LEFT JOIN co ON co.t_id=te.t_id
LEFT JOIN sc ON sc.c_id =co.c_id
GROUP BY te.t_id
ORDER BY AVG(sc.score) DESC;
执行的效果如下:
我们发现报错了,报了1055
错误,错误的原因大致是:在MySQL5.7之后,sql_mode
中默认存在ONLY_FULL_GROUP_BY
,SQL
语句未通过ONLY_FULL_GROUP_BY
语义检查所以报错。 这里的ONLY_FULL_GROUP_BY
要求select语句中查询出来的列必须是明确的。我们以SQL语句select columes from table group by list
为例:columns必须是聚集函数或者在group by后的表达式list中,并且list中必须包含主键,否则也会报错。当然,其他的insert
、update
、delete语句
都会报错(但不影响SQL语句的执行),因为这三种语句执行之前也会执行查询操作。通过网上查询,发现我们只需要将ONLY_FULL_GROUP_BY
关掉重新打开MySQL workbench即可;具体实现如下:
SELECT @@sql_mode;
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
select stu.s_id, s_name, count(sc.c_id) 选修课程
from stu left join sc on stu.s_id = sc.s_id
group by stu.s_id
having count(distinct sc.c_id) = 2;
select s_id, s_name
from stu
where s_id in (
select s_id
from sc
group by s_id
having count(distinct c_id) = 2
);
select s_sex, count(distinct s_id)
from stu
group by s_sex;
select *
from stu
where year(s_birth) = 1990 ;
SELECT *
FROM stu
WHERE s_birth LIKE "1990%";
select c_id,avg(score) 平均成绩
from sc
group by c_id
order by 平均成绩 desc, c_id;
SELECT co.c_id,co.c_name,AVG(sc.score) 平均成绩
FROM co JOIN sc ON sc.c_id=co.c_id
GROUP BY co.c_id
ORDER BY AVG(sc.score) DESC,co.c_id ASC;
SELECT stu.s_id,stu.s_name,AVG(sc.score) 平均成绩
FROM stu
LEFT JOIN sc ON sc.s_id=stu.s_id
GROUP BY stu.s_id
HAVING AVG(sc.score)>=85;
select c_name, s_name, score
from sc
left join stu on sc.s_id = stu.s_id
left join co on sc.c_id = co.c_id
where c_name = "数学" and score < 60;
SELECT stu.s_id,stu.s_name,sc.score
FROM stu JOIN sc ON sc.s_id=stu.s_id AND sc.c_id="01" AND sc.score>=80;
select c_id, count(c_id)
from sc
group by c_id;
SELECT s_id,COUNT(DISTINCT c_id)
FROM sc
GROUP BY s_id
HAVING COUNT(DISTINCT c_id)>=2;
SELECT stu.s_id, COUNT(1) 选修课程
FROM stu
LEFT JOIN sc ON sc.s_id=stu.s_id
GROUP BY stu.s_id
HAVING COUNT(1)>=2;
SELECT s_id,s_birth,FLOOR(DATEDIFF(CURDATE(),s_birth)/365)'年龄'
FROM stu;
select s_id,s_birth,
case when month(current_date())<month(s_birth) then year(current_date())-year(s_birth)-1
when month(current_date())=month(s_birth) and day(current_date())<day(s_birth) then year(current_date())-year(s_birth)-1
else year(current_date())-year(s_birth)
end 年龄
from stu;
执行的效果如下:
至此,我们MySQL经典的50道面试题全部给大家介绍完毕,希望大家多多练习,MySQL其实大多数都是用来查的,因此,我们要熟练掌握这些查询语句,不断提高自己书写MySQL语句的能力。