闲来无事,找个网站练习sql基础。
练习网站:
https://zh.sqlzoo.net/wiki/SQL_Tutorial
练习区域,左边导航栏
SELECT within SELECT Tutorial/zh
2.列出欧洲每国家的人均GDP,当中人均GDP要高于英国’United Kingdom’的数值。
select name
from world
where continent = 'Europe'
and (gdp / population) >
(select gdp / population from world where name = 'United Kingdom');
3.在阿根廷Argentina 及 澳大利亚 Australia所在的洲份中,列出当中的国家名字 name 及洲分 continent 。按国字名字顺序排序。
select name, continent
from world
where continent in (select continent
from world
where name = 'Argentina '
or name = 'Australia')
order by name
4.哪一个国家的人口比加拿大Canada的多,但比波兰Poland的少?列出国家名字name和人口population 。
select name, population
from world
where population > (select population from world where name = 'Canada')
and population < (select population from world where name = 'Poland')
5.Germany德国(人口8000万),在Europe欧洲国家的人口最多。Austria奥地利(人口850万)拥有德国总人口的11%。
显示欧洲的国家名称name和每个国家的人口population。以德国的人口的百分比作人口显示。
select name,
concat(round(population /(select population from world where name = 'Germany') * 100),'%')
from world
where continent = 'Europe'
6.哪些国家的GDP比Europe欧洲的全部国家都要高呢? [只需列出 name 。] (有些国家的记录中,GDP是NULL,沒有填入资料的。)
select name
from world
where gdp > all (select gdp
from world
where gdp > 0
and continent = 'Europe')
7.在每一个州中找出最大面积的国家,列出洲份 continent, 国家名字 name 及面积 area。 (有些国家的记录中,AREA是NULL,沒有填入资料的。)
select continent, name, area
from world x
where area >= all (select area
from world y
where y.continent = x.continent
and area > 0)
8.列出洲份名称,和每个洲份中国家名字按子母顺序是排首位的国家名。(即每洲只有列一国)
select continent, name
from (select continent,
name,
row_number() over(partition by continent order by name) as num
from world) a
where a.num = '1';
9.找出洲份,当中全部国家都有少于或等于 25000000 人口. 在这些洲份中,列出国家名字name,continent 洲份和population人口。
select name, continent, population
from world a
where (select max(population) from world b where a.continent = b.continent) <= 25000000
10.有些国家的人口是同洲份的所有其他国的3倍或以上。列出 国家名字name 和 洲份 continent。
select name, continent
from world a
where (select a.population / 3 >= all (select population
from world b
where a.continent = b.continent
and a.name != b.name))
2020/4/28更新
练习区域,左边导航栏
join
1.要找出德国队球员
SELECT matchid,player FROM goal
WHERE teamid = 'GER'
2.只显示赛事1012的 id, stadium, team1, team2
SELECT id,stadium,team1,team2
FROM game where id = '1012'
3.显示每一个德国入球的球员名,队伍名,场馆和日期。
SELECT player, teamid, stadium, mdate
FROM game
JOIN goal
ON (id = matchid)
and teamid = 'GER'
4.列出球员名字叫Mario (player LIKE ‘Mario%’)有入球的 队伍1 team1, 队伍2 team2 和 球员名 player
select team1, team2, player
from game
join goal
on player like 'Mario%'
and id = matchid
5.列出每场球赛中首10分种gtime<=10有入球的球员 player, 队伍teamid, 教练coach, 入球时间gtime
SELECT player, teamid, coach, gtime
FROM goal
join eteam
on teamid = id
WHERE gtime <= 10
6.列出’Fernando Santos’作为队伍1 team1 的教练的赛事日期,和队伍名。
select mdate, teamname
from eteam
join game
on (team1 = eteam.id)
and coach = 'Fernando Santos'
7.列出场馆 'National Stadium, Warsaw’的入球球员。
select player
from goal
join game
on matchid = id
and stadium = 'National Stadium, Warsaw'
8.列出全部赛事,射入德国龍門的球员名字。
SELECT distinct (player)
FROM game
JOIN goal
ON matchid = id
and ((team1 = 'GER' and team2 = teamid) or
(team1 = teamid and team2 = 'GER'))
9.列出队伍名称 teamname 和该队入球总数.
select teamname, count(teamname)
from eteam
join goal
on id = teamid
group by teamname
order by teamname
10.列出场馆名和在该场馆的入球数字。
select stadium, count(*)
from game
join goal
on id = matchid
group by stadium
order by stadium
11.每一场波兰’POL’有参与的赛事中,列出赛事编号 matchid, 日期date 和入球数字.
select matchid, mdate, count(*)
from game
join goal
on matchid = id
where (team1 = 'POL' or team2 = 'POL')
group by matchid, mdate
12.每一场德国’GER’有参与的赛事中,列出赛事编号 matchid, 日期date 和德国的入球数字。
select matchid, mdate, count(*)
from game
join (select matchid, teamid from goal where teamid = 'GER') a
on a.matchid = id
group by matchid, mdate
13.列出所有比赛的每个队的比分
select
b.mdate,
b.team1,
sum(case when teamid=team1 then 1 else 0 end) as score1,
b.team2,
sum(case when teamid=team2 then 1 else 0 end) as score2
from
(select id ,mdate, team1, team2 from game) b
left join
(select matchid, teamid from goal) a
on a.matchid = b.id
group by b.mdate, b.team1, b.team2
order by b.mdate, b.team1, b.team2;
2020/5/13更新
练习区域,左边导航栏
more join
1.列出電影北非諜影 'Casablanca’的演員名單。
select a.name
from actor a, casting c, movie m
where a.id = c.actorid
and c.movieid = m.id
and m.title = 'Casablanca'
2.顯示電影異型’Alien’ 的演員清單。
select a.name
from actor a, casting c, movie m
where a.id = c.actorid
and c.movieid = m.id
and m.title = 'Alien'
3.列出演員夏里遜福 ‘Harrison Ford’ 曾演出的電影。
select m.title
from actor a, casting c, movie m
where a.id = c.actorid
and c.movieid = m.id
and a.name = 'Harrison Ford'
4.列出演員夏里遜福 ‘Harrison Ford’ 曾演出的電影,但他不是第1主角。