GARENA笔试sql20190926
阅读原文时间:2023年07月11日阅读:1

create database garena;
use garena;

create table players(
account_id int,
name varchar(20),
country varchar(20)
) ;

desc players;

insert into players values(10001,'N1','US');
insert into players values(10002,'N2','SG');
insert into players values(10003,'N3','US');
insert into players values(10004,'N4','TH');

SELECT * from players;

delete from players where account_id is null;

create table stats(
account_id int,
weapon varchar(20),
score int
) ;

desc stats;

insert into stats values(10001,'GATLING',3);
insert into stats values(10001,'M4A1',5);
insert into stats values(10001,'AN94',1);
insert into stats values(10002,'MP5',1);
insert into stats values(10002,'P90',2);
insert into stats values(10002,'M4A1',2);

SELECT * from stats;

1. 报名的玩家中来自US的玩家名单(account_id,name)

select account_id,name from players where country='US';

2. 每个国家使用M4A1击杀的平均分,按照平均分从大到小排列(country,avg_score)

(1)先获得B.account_id,weapon,score,country的表
select B.account_id,weapon,score,country from stats B left join players A on A.account_id=B.account_id;

(2)获得每个国家的平均分
select C.country,avg(C.score) as avg_score from
(select B.account_id,weapon,score,country from stats B left join players A on A.account_id=B.account_id) as C
where C.weapon='M4A1' group by country order by avg_score desc;

#3.报名了但是没有参加比赛的玩家名单(account_id,name,country)
#方法1:
select A.account_id,A.name,A.country from players A left join stats B on A.account_id=B.account_id where B.account_id is null;
#方法2:
select account_id, name, country from players where account_id not in (select account_id from stats);

4. 每个国家使用M4A1击杀得分最高的玩家信息(account_id,name,country,score)

5. 每个国家击杀得分最高的2个玩家信息,按照国家,分数的顺序从大到小排序。(account_id,name,country,score)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章