SQL Interview Questions and Answers

Student transcript (stuscore):
Name: name Course: subject Score: score Student ID: stuid
Zhang San Math 89 1 Li Si English
80 2
English 70 1
Li Si Mathematics 90 2
Li
Si English 80 2

1. Calculate the total score of each person and rank (requires display fields: name, total score)

答案:select name,sum(score) as allscore from stuscore group by name order by allscore

2. Calculate the total score of each person and rank (required display fields: student number, name, total score)

答案:select distinct t1.name,t1.stuid,t2.allscore from  stuscore t1,(    select stuid,sum(score) as allscore from stuscore group by stuid)t2where t1.stuid=t2.stuidorder by t2.allscore desc

3. Calculate the highest grade of each individual subject (required display fields: student number, name, course, highest grade)

答案:select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(select stuid,max(score) as maxscore from stuscore group by stuid) t2where t1.stuid=t2.stuid and t1.score=t2.maxscore

4. Calculate the average grade of each person (required display fields: student ID, name, average grade)

答案:select distinct t1.stuid,t1.name,t2.avgscore from stuscore t1,(select stuid,avg(score) as avgscore from stuscore group by stuid) t2where t1.stuid=t2.stuid

5. List the students with the best grades in each course (required display fields: student number, name, subject, grade)

答案:select  t1.stuid,t1.name,t1.subject,t2.maxscore from stuscore t1,(select subject,max(score) as maxscore from stuscore group by subject) t2where t1.subject=t2.subject and t1.score=t2.maxscore

6. List the two students with the best grades in each course (required display fields: student number, name, subject, grade)

答案:select distinct t1.* from stuscore t1 where t1.id in (select top 2 stuscore.id from stuscore where subject = t1.subject order by score desc) order by t1.subject

7. The statistics are as follows: student number name Chinese mathematics English total score average score

Answer: select stuid as student number, name as name, sum(case when subject=\'Chinese\' then score else 0 end) as Chinese, sum(case when subject=\'math\' then score else 0 end) as Math,sum(case when subject=\'English\' then score else 0 end) as English,sum(score) as total score,(sum(score)/count(*)) as average score from stuscoregroup by stuid,name order by total score desc

8. List the grade point average for each course (requires display fields: course, grade point average)

答案:select subject,avg(score) as avgscore from stuscoregroup by subject

9. List the rank of math grades (requires display fields: student ID, name, grade, rank)

Answer:

declare @tmp table(pm int,name varchar(50),score int,stuid int)
insert into @tmp select null,name,score,stuid from stuscore where subject=\’数学\’ order by score desc
declare @id int
set @id=0;
update @tmp set @id=@id+1,pm=@id
select * from @tmp

oracle:
select  DENSE_RANK () OVER(order by score desc) as row,name,subject,score,stuid from stuscore where subject=\’数学\’order by score desc
ms sql(最佳选择)
select (select count(*) from stuscore t1 where subject =\’数学\’ and t1.score>t2.score)+1 as row ,stuid,name,score from stuscore t2 where subject =\’数学\’ order by score desc

10. List students with math scores in 2-3 (requires display fields: student ID, name, subject, grade)

答案:select t3.*  from(select top 2 t2.*  from (select top 3 name,subject,score,stuid from stuscore where subject=\’数学\’order by score desc) t2 order by t2.score) t3 order by t3.score desc

11. Find the rank of Li Si's math scores

Answer:

declare @tmp table(pm int,name varchar(50),score int,stuid int)insert into @tmp select null,name,score,stuid from stuscore where subject=\’数学\’ order by score descdeclare @id intset @id=0;update @tmp set @id=@id+1,pm=@idselect * from @tmp where name=\’李四\’

12. The statistics are as follows: Failed courses (0-59) Good (60-80) Excellent (81-100)

答案:select subject, (select count(*) from stuscore where score<60 and subject=t1.subject) as 不及格,(select count(*) from stuscore where score between 60 and 80 and subject=t1.subject) as 良,(select count(*) from stuscore where score >80 and subject=t1.subject) as 优from stuscore t1 group by subject

13. Statistics are as follows: Mathematics: Zhang San (50 points), Li Si (90 points), Wang Wu (90 points), Zhao Liu (76 points)

Answer:

declare @s varchar(1000)set @s=\’\’select @s =@s+\’,\’+name+\'(\’+convert(varchar(10),score)+\’分)\’ from stuscore where subject=\’数学\’ set @s=stuff(@s,1,1,\’\’)print \’数学:\’+@s

14. Calculate the average grade of those who pass the subjects

答案: select distinct t1.stuid,t2.avgscore  from stuscore t1,(select stuid,avg(score) as avgscore from stuscore   group by stuid  ) t2,(select stuid from stuscore where score<60 group by stuid) t3 where t1.stuid=t2.stuid and t1.stuid!=t3.stuid;
select  name,avg(score) as avgscore   from stuscore s  where (select sum(case when i.score>=60 then 1 else 0 end) from stuscore i where  i.name= s.name)=3   group by name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326246340&siteId=291194637