mysql五十题及作答

补充:
题目描述
查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序
(注:可能有employees表和salaries表里存在记录的员工,有对应的员工编号和涨薪记录,但是已经离职了,离职的员工salaries表的最新的to_date!='9999-01-01',这样的数据不显示在查找结果里面)
CREATE TABLE employees (
emp_no int(11) NOT NULL,
birth_date date NOT NULL,
first_name varchar(14) NOT NULL,
last_name varchar(16) NOT NULL,
gender char(1) NOT NULL,
hire_date date NOT NULL, -- '入职时间'
PRIMARY KEY (emp_no));
CREATE TABLE salaries (
emp_no int(11) NOT NULL,
salary int(11) NOT NULL,
from_date date NOT NULL, -- '一条薪水记录开始时间'
to_date date NOT NULL, -- '一条薪水记录结束时间'
PRIMARY KEY (emp_no,from_date));

解:select a.emp_no,(a.salary-b.salary)as growth from

(select emp_no,salary from salaries where to_date='9999-01-01') as a join

(select s.emp_no,s.salary from salaries s inner join employees e on e.emp_no=s.emp_no and e.hire_date=s.from_date)as b

on a.emp_no=b.emp_no order by growth

原50题:
——————————————————————————————————
题目及表:https://blog.csdn.net/fashion2014/article/details/78826299/

解:1、

SELECT

t1.s_id as id,t1.s_name as name,t1.s_birth as birth ,t1.s_sex as sex,t2.*

FROM

Student AS t1

INNER JOIN

(SELECT

s1.s_id,

s1.c_id AS cid_01,

s2.c_id AS cid_02,

s1.s_score AS score_01,

s2.s_score AS score_02

FROM

score AS s1

INNER JOIN score AS s2 ON s1.s_id = s2.s_id AND s1.c_id = 01

AND s2.c_id = 02

AND s1.s_score > s2.s_score) t2 ON t1.s_id = t2.s_id

2、

SELECT

t1.s_id as id,t1.s_name as name,t1.s_birth as birth ,t1.s_sex as sex,t2.*

FROM

Student AS t1

INNER JOIN

(SELECT

s1.s_id,

s1.c_id AS cid_01,

s2.c_id AS cid_02,

s1.s_score AS score_01,

s2.s_score AS score_02

FROM

score AS s1

INNER JOIN score AS s2 ON s1.s_id = s2.s_id AND s1.c_id = 01

AND s2.c_id = 02

AND s1.s_score < s2.s_score) t2 ON t1.s_id = t2.s_id

4、

(select s_id as id,s_name as name,0 as avg from Student where s_id not in (select distinct s_id from score))

union

(select t1.s_id as id,t2.s_name as name ,t1.avg as avg from (select s_id,AVG(s_score) as avg from score group by s_id having avg<60 ) as t1 left join student as t2 on t1.s_id=t2.s_id)

order by id;

5、

SELECT

t1.s_id, t1.s_name, COUNT(t2.c_id), SUM(t2.s_score)

FROM

student AS t1

LEFT JOIN

score AS t2 ON t1.s_id = t2.s_id

GROUP BY t1.s_id‌

6、

SELECT

count(t_id)

FROM

teacher where t_name like '李%'

7、

SELECT

s.*

FROM

Student AS s

INNER JOIN

score AS sc ON s.s_id = sc.s_id

AND sc.c_id = (SELECT

c_id

FROM

course

WHERE

t_id = (SELECT

t_id

FROM

teacher

WHERE

t_name = '张三'))

8、

select * from student where s_id not in (SELECT

s.s_id

FROM

Student AS s

INNER JOIN

score AS sc ON s.s_id = sc.s_id

AND sc.c_id = (SELECT

c_id

FROM

course

WHERE

t_id = (SELECT

t_id

FROM

teacher

WHERE

t_name = '张三')))

9、

select * from student where s_id in(SELECT

t1.s_id

FROM

company.score t1

INNER JOIN

score t2 ON t1.s_id = t2.s_id AND t1.c_id = 01

AND t2.c_id = 02)

10、

select * from student where s_id=(select s_id from (select * from score where s_id not in (select distinct s_id from score where c_id = 02 )) as t where c_id=01)

11、

select student.s_id,student.s_name,count(score.c_id) from student left join score on student.s_id =score.s_id group by student.s_id having count(score.c_id)<(select count(*) from course)

12、

select * from student where s_id in(select distinct s_id from score where c_id in( select c_id from score where s_id=01) )

13、

select * from student where s_id in (SELECT

s_id

FROM

score

GROUP BY s_id

HAVING GROUP_CONCAT(c_id) IN (SELECT

GROUP_CONCAT(c_id)

FROM

score

WHERE

s_id = 01

GROUP BY s_id))

14、

SELECT

FROM

student

WHERE

s_id NOT IN (SELECT

s_id

FROM

score

WHERE

c_id = (SELECT

course.c_id

FROM

course

INNER JOIN

teacher ON course.t_id = teacher.t_id

AND teacher.t_name = '张三'))

15、

SELECT

t1.s_id, t1.s_name, t2.avg

FROM

student AS t1

INNER JOIN

(SELECT

s_id, round(AVG(s_score)) AS avg

FROM

company.score

WHERE

s_score < 60

GROUP BY s_id

HAVING COUNT(c_id) >= 2) AS t2 ON t1.s_id = t2.s_idd = t2.s_id

16

select s.*, t.c_id,t.s_score from student s inner join (select * from score where s_score<60 and c_id=01) as t on s.s_id=t.s_id order by s_score desc

17、

SELECT

a.s_id,

(SELECT

s_score

FROM

score

WHERE

s_id = a.s_id AND c_id = '03') as '英语',

(SELECT

s_score

FROM

score

WHERE

s_id = a.s_id AND c_id = '02') as '数学',

(SELECT

s_score

FROM

score

WHERE

s_id = a.s_id AND c_id = '01') AS '语文',

round(avg(s_score)) as 'avg',group_concat(s_score)

FROM

score a group by a.s_id order by 'avg' desc ;

18:

SELECT

c.c_name,

MAX(s.s_score),

MIN(s.s_score),

ROUND(AVG(s.s_score), 2),

CASE

WHEN ROUND(AVG(s.s_score), 2) < 60 THEN '不及格'

WHEN

ROUND(AVG(s.s_score), 2) >= 60

AND ROUND(AVG(s.s_score), 2) < 80

THEN

'中等'

WHEN

ROUND(AVG(s.s_score), 2) >= 80

AND ROUND(AVG(s.s_score), 2) < 90

THEN

'良好'

ELSE '优秀'

END AS '等级'

FROM

score s

INNER JOIN

course c ON s.c_id = c.c_id

GROUP BY s.c_id

19、

SELECT *,rank() over(order by s_score) as 'rank',row_number() over(order by s_score) as 'row_num_rank' FROM company.score;

20、

select s_id,sum(s_score),dense_rank() over(order by sum(s_score) DESC) AS 'RANK' from score group by s_id

21、

select s.c_id as '课程id',c.t_id as '教师id',t.t_name as '教师name',round(avg(s.s_score),2) as '平均分' from score s inner join course c on s.c_id=c.c_id left join teacher t on t.t_id=c.t_id group by s.c_id order by avg(s.s_score) desc

22、

select t1.*,t2.c_id,t2.s_score,t2.rank1 from (select * from (select *,dense_rank() over(partition by c_id order by s_score desc ) as 'rank1' from score ) as t where rank1 in (2,3)) as t2 left join student t1 on t1.s_id=t2.s_id

25、

select * from (select *,row_number() over(partition by c_id order by s_score desc) as r from score) as t2 where r in(1,2,3)

26、SELECT c_id,count(s_id) FROM company.score group by c_id;

27. select s.* from (SELECT s_id,count(c_id) FROM company.score group by s_id having count(c_id)=2) as t left join student s on s.s_id=t.s_id

28、select s_sex,count(1) from student group by s_sex

29、select * from student where s_name like '%风%';

30、select *,count(1) from student as t1 inner join student as t2 on t1.s_name=t2.s_name and t1.s_id!=t2.s_id

32、

select c_id,avg(s_score) as 'avg' from score group by c_id order by avg(s_score) desc , c_id asc

33、select s.*,t.Avg from student s right join (select s_id ,avg(s_score) as Avg from score group by s_id having avg(s_score)>85 ) as t on s.s_id=t.s_id

SELECT

st.*,

(SELECT

s_score

FROM

score

WHERE

c_id = 01 AND s_id = sc.s_id) AS '语文',

(SELECT

s_score

FROM

score

WHERE

c_id = 02 AND s_id = sc.s_id) AS '数学',

(SELECT

s_score

FROM

score

WHERE

c_id = 03 AND s_id = sc.s_id) AS '英语'

FROM

score sc

RIGHT JOIN

student st ON st.s_id = sc.s_id

LEFT JOIN

course co ON co.c_id = sc.c_id

GROUP BY s_id

35、

select st.s_name ,c.c_name,score.s_score from score left join student as st on st.s_id=score.s_id left join course as c on score.c_id=c.c_id where s_score>=70

36、select st.s_name ,c.c_name,score.s_score from score left join student as st on st.s_id=score.s_id left join course as c on score.c_id=c.c_id where s_score<60

40、select student.*,t1.s_score from student inner join (select * from score where c_id =(select c.c_id from teacher t inner join course c on t.t_id=c.t_id where t.t_name='张三') order by s_score desc limit 1) as t1 on student.s_id=t1.s_id

41、

select distinct * from score t2 inner join score as t1 on t1.s_score=t2.s_score and t1.s_id!=t2.s_id and t1.c_id!=t2.c_id

42、select * from (select *,row_number() over(partition by c_id order by s_score desc) as r from score ) as t where r in (1,2)

43、select *,count(1) from score group by c_id having count(1)>5 order by count(1) desc ,c_id

Asc

44、select s_id,count(1) as count from score group by s_id having count(1)>=2

45select s_id,count(1) as count from score group by s_id having count(1)=(select count(*) from course)

46、select s_birth,((date_format(now(),'%Y')-date_format(s_birth,'%Y'))-(case when (date_format(now(),'%m')>date_format(s_birth,'%m')) then 0 else 1 end)) as age

from student;

47、select * from student where week(date_format(now(),'%Y-%m-%d'))=week(s_birth)

48、select * from student where week(date_format(now(),'%Y-%m-%d'))+1=week(s_birth)

49、

select * from student where month(date_format(now(),'%Y-%m-%d'))=month(s_birth)

50、

select * from student where month(date_format(now(),'%Y-%m-%d'))+1=month(s_birth)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,922评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,591评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,546评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,467评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,553评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,580评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,588评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,334评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,780评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,092评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,270评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,925评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,573评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,194评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,437评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,154评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,127评论 2 352