SQL面试题(持续更新)

一.

现有表TBStudentScore,定义如下

create table TBStudentScore
{
	student varchar(30),
	course varchar(10),
	score integer
}

表内容:
SQL面试题(持续更新)_第1张图片

1.韩梅梅的数学成绩登记出错,改为91

UPDATE tbstudentscore SET score=91 WHERE student='韩梅梅' AND course='数学'

2.Jim Green记录重复了,查出重复记录并删除

3.输出结果为下图的sql语句(不输出表头)

SELECT 
student,
SUM(CASE WHEN course='语文' THEN score ELSE 0 END) 语文,
SUM(CASE WHEN course='数学' THEN score ELSE 0 END) 数学,
SUM(CASE WHEN course='英语' THEN score ELSE 0 END) 英语
FROM tbstudentscore GROUP BY student

二.

表test
SQL面试题(持续更新)_第2张图片

1.统计a,b,c三列大于1的个数,结果如下

SELECT 
SUM(CASE WHEN a>1 THEN 1 ELSE 0 END) a,
SUM(CASE WHEN b>1 THEN 1 ELSE 0 END) b,
SUM(CASE WHEN c>1 THEN 1 ELSE 0 END) c
FROM test

2.按a分组,用b排序取top1,结果如下

在这里插入图片描述

SELECT test.*
FROM test JOIN (SELECT a,MAX(b)AS b FROM test GROUP BY a)AS t
ON t.b=test.b
GROUP BY test.a

三.

学生表 Student

CREATE TABLE Student(sid VARCHAR(10),Sname NVARCHAR(10),Sage DATETIME,Ssex NVARCHAR(10));
INSERT INTO Student VALUES('01' , N'赵雷' , '1990-01-01' , N'男');
INSERT INTO Student VALUES('02' , N'钱电' , '1990-12-21' , N'男');
INSERT INTO Student VALUES('03' , N'孙风' , '1990-05-20' , N'男');
INSERT INTO Student VALUES('04' , N'李云' , '1990-08-06' , N'男');
INSERT INTO Student VALUES('05' , N'周梅' , '1991-12-01' , N'女');
INSERT INTO Student VALUES('06' , N'吴兰' , '1992-03-01' , N'女');
INSERT INTO Student VALUES('07' , N'郑竹' , '1989-07-01' , N'女');
INSERT INTO Student VALUES('08' , N'王菊' , '1990-01-20' , N'女');

科目表 Course

CREATE TABLE Course(Cid VARCHAR(10),Cname NVARCHAR(10),Tid VARCHAR(10));
INSERT INTO Course VALUES('01' , N'语文' , '02');
INSERT INTO Course VALUES('02' , N'数学' , '01');
INSERT INTO Course VALUES('03' , N'英语' , '03');

教师表 Teacher

CREATE TABLE Teacher(Tid VARCHAR(10),Tname NVARCHAR(10));
INSERT INTO Teacher VALUES('01' , N'张三');
INSERT INTO Teacher VALUES('02' , N'李四');
INSERT INTO Teacher VALUES('03' , N'王五');

成绩表 SC

CREATE TABLE SC(Sid VARCHAR(10),Cid VARCHAR(10),score DECIMAL(18,1));
INSERT INTO SC VALUES('01' , '01' , 80);
INSERT INTO SC VALUES('01' , '02' , 90);
INSERT INTO SC VALUES('01' , '03' , 99);
INSERT INTO SC VALUES('02' , '01' , 70);
INSERT INTO SC VALUES('02' , '02' , 60);
INSERT INTO SC VALUES('02' , '03' , 80);
INSERT INTO SC VALUES('03' , '01' , 80);
INSERT INTO SC VALUES('03' , '02' , 80);
INSERT INTO SC VALUES('03' , '03' , 80);
INSERT INTO SC VALUES('04' , '01' , 50);
INSERT INTO SC VALUES('04' , '02' , 30);
INSERT INTO SC VALUES('04' , '03' , 20);
INSERT INTO SC VALUES('05' , '01' , 76);
INSERT INTO SC VALUES('05' , '02' , 87);
INSERT INTO SC VALUES('06' , '01' , 31);
INSERT INTO SC VALUES('06' , '03' , 34);
INSERT INTO SC VALUES('07' , '02' , 89);
INSERT INTO SC VALUES('07' , '03' , 98);

1.查询学生平均成绩及其名次

SELECT s.sid,s.av,(@rowNum:=@rowNum+1) AS top 
FROM (SELECT sid,AVG(score) AS av FROM sc GROUP BY Sid
 ORDER BY av DESC) s, (SELECT (@rowNum :=0)) b GROUP BY Sid

你可能感兴趣的:(sql,大数据,面试)