Kathuko › SQL › Part 1: SELECT basics

1.7 19c+

Practice: three SELECT problems

Three questions using only Part 1. Expected rows first, then the answer behind a tap. First ball of the innings: play yourself in.

1. Spinners, alphabetically

All spin bowlers (any bowling style containing "spin"), with their team code and style, sorted by style and then name.

Expected result · 17 rows
PLAYER_NAMETEAM_CODEBOWLING_STYLE
Arvind SelvamCHNLeft-arm spin
Asif MirzaKHILeft-arm spin
Imraan PatelDBNLeft-arm spin
Sione FifitaAKLLeft-arm spin
Tyler VanceTPALeft-arm spin
Will AshdownLDSLeft-arm spin
Archie MuirAKLRight-arm leg-spin
Ethan ParkTPARight-arm leg-spin
Freddie LoweLDSRight-arm leg-spin
Senthil MuruganCHNRight-arm leg-spin
Yusuf MoosaDBNRight-arm leg-spin
Zubair LodhiKHIRight-arm leg-spin
Danish AnsariKHIRight-arm off-spin
George PemburyLDSRight-arm off-spin
Harish KumarCHNRight-arm off-spin
Nikhil RaoTPARight-arm off-spin
Rory PatersonAKLRight-arm off-spin
Try itShow the answer
select p.player_name, t.team_code, p.bowling_style
from   cr_players p
join   cr_teams   t on t.team_id = p.team_id
where  p.bowling_style like '%spin%'
order  by p.bowling_style, p.player_name;
select p.player_name, t.team_code, p.bowling_style
from   cr_players p
join   cr_teams   t on t.team_id = p.team_id
where  instr(p.bowling_style, 'spin') > 0
order  by p.bowling_style, p.player_name;

2. Close finishes

Every match won by fewer than 10 runs or with only 1 or 2 wickets left, plus any tie. Show match, season, how it ended and the margin ("tie" for a tie).

Expected result · 5 rows
MATCH_IDSEASONRESULTMARGIN
102025tietie
152025runs5
202026wickets2
212026wickets2
312026wickets2
Try itShow the answer
select match_id, season,
       case win_type when 'TIE' then 'tie' else lower(win_type) end as result,
       nvl(to_char(win_margin), 'tie')                               as margin
from   cr_matches
where  (win_type = 'RUNS'    and win_margin < 10)
or     (win_type = 'WICKETS' and win_margin <= 2)
or      win_type = 'TIE'
order  by match_id;
select match_id, season,
       decode(win_type, 'TIE', 'tie', lower(win_type))   as result,
       coalesce(to_char(win_margin), 'tie')              as margin
from   cr_matches
where  win_type = 'TIE'
or     win_margin < case win_type when 'RUNS' then 10 when 'WICKETS' then 3 end
order  by match_id;

NVL(TO_CHAR(win_margin), 'tie'): the margin is a number, so it's turned into text first; NVL needs both arguments to be the same type.

3. Born in the 2000s

Players born on or after January 1, 2000, youngest first, with their birth year.

Expected result · 12 rows
PLAYER_NAMEBIRTH_YEAR
Alfie Dunn2002
Isaac Ruane2002
Dinesh Varma2001
Omer Farooq2001
Devon Price2001
Ethan Naidoo2001
Archie Muir2000
Naveen Reddy2000
Freddie Lowe2000
Waqas Jamil2000
Yusuf Moosa2000
Ethan Park2000
Try itShow the answer
select player_name, extract(year from born) as birth_year
from   cr_players
where  born >= date '2000-01-01'
order  by born desc;
select player_name, to_number(to_char(born, 'YYYY')) as birth_year
from   cr_players
where  to_char(born, 'YYYY') >= '2000'
order  by born desc;

The first way is better: born >= date '2000-01-01' can use an index on born; wrapping the column in TO_CHAR usually can't. Same rows today, different speed on a big table.

Part 1 quiz

5 questions on all of Part 1. Get 4 right to pass. Retake it as often as you like.