sql语句——经典的数据库面试题

经典的数据库面试题

  • 1、Student(S#,Sname,Sage,Ssex)学生表;Course(C#,Cname,T#)课程表;SC(S#,C#,Score)成绩表;Teacher(T#,Tname)教师表;
  • (1)查询平均成绩大于60分的同学的学号和平均成绩;
  • (2)查询学过“叶平”老师所教的所有课的同学的学号,姓名;
  • 2、 从events表中,查找refer,user_id,去掉重复的用户名后的用户个数,且refer 类似 “www.baidu.com”

1、Student(S#,Sname,Sage,Ssex)学生表;Course(C#,Cname,T#)课程表;SC(S#,C#,Score)成绩表;Teacher(T#,Tname)教师表;

(1)查询平均成绩大于60分的同学的学号和平均成绩;

(2)查询学过“叶平”老师所教的所有课的同学的学号,姓名;

第一个查询语句:

select s.S#,avg(e.Score) as average from SC e
inner join Student s on e.S# = s.S#
Group by s.S#
HAVING average > 60;

第二个查询语句:

with C as(
select C# from Course
inner join Teacher  on Course.T# = Teacher.T# and Teacher.Tname='叶平'
order by Course.C#)
select SC.S#,Student.Sname from SC
inner join C on SC.C# = C.C#
inner join Student on SC.S# = Student.S#
order by SC.S#;

2、 从events表中,查找refer,user_id,去掉重复的用户名后的用户个数,且refer 类似 “www.baidu.com”

with e as(
select distinct username from events )
select count(username) from e
inner join events on e.username = events.username and where envents.refer like "%www.baidu.com%";
group by enents.user_id

注:如果大家有其他实现方法可以讨论。。。

你可能感兴趣的:(SQL语句)