HackerRank


2.1. SQL Project Planning

input:

1 2015-10-01 2015-10-02
24 2015-10-02 2015-10-03
2 2015-10-03 2015-10-04
23 2015-10-04 2015-10-05 -> 3 days
3 2015-10-11 2015-10-12
22 2015-10-12 2015-10-13 -> 2 days
4 2015-10-15 2015-10-16 -> 1 days
21 2015-10-17 2015-10-18 -> 1 days
5 2015-10-19 2015-10-20 -> 1 days
20 2015-10-21 2015-10-22 -> 1 days
6 2015-10-25 2015-10-26
19 2015-10-26 2015-10-27
7 2015-10-27 2015-10-28
18 2015-10-28 2015-10-29
8 2015-10-29 2015-10-30
17 2015-10-30 2015-10-31
9 2015-11-01 2015-11-02
16 2015-11-04 2015-11-05
10 2015-11-07 2015-11-08
15 2015-11-06 2015-11-07
11 2015-11-05 2015-11-06
14 2015-11-11 2015-11-12
12 2015-11-12 2015-11-13
13 2015-11-17 2015-11-18

output:

2015-10-15 2015-10-16
2015-10-17 2015-10-18
2015-10-19 2015-10-20
2015-10-21 2015-10-22
2015-11-01 2015-11-02
2015-11-17 2015-11-18
2015-10-11 2015-10-13
2015-11-11 2015-11-13
2015-10-01 2015-10-05
2015-11-04 2015-11-08
2015-10-25 2015-10-31

SELECT Start_Date, MIN(End_Date)
FROM 
    (SELECT Start_Date FROM Projects WHERE Start_Date NOT IN (SELECT End_Date FROM Projects)) a,
    (SELECT End_date FROM PROJECTS WHERE End_date NOT IN (SELECT Start_Date FROM Projects)) b
where Start_Date < End_date
GROUP BY Start_Date
ORDER BY (Start_Date - MIN(End_date)) DESC, Start_Date
;

2.2. Interviews

input:

contest_id total_submissions total_accepted_submissions total_views total_unique_views
845 38 17 38 19
845 94 13 22 22
845 73 21 22 22
845 40 21 22 22
845 72 9 98 11
845 38 25 98 11
845 67 14 98 11
845 70 19 98 11
845 17 14 79 17
845 17 14 17 16
845 38 17 65 12
845 49 20 80 24
845 41 16 80 24
845 NULL NULL 40 25
845 25 21 35 17
845 70 8 35 17
845 NULL NULL 84 17
845 38 17 50 14
845 73 6 33 25
845 48 25 33 25
845 17 14 22 18
845 72 9 31 22
845 38 25 31 22
845 67 14 31 22
845 70 19 31 22
845 26 14 33 25
845 31 16 33 25
845 52 7 56 12
845 NULL NULL 20 20
845 85 23 31 18
845 23 21 31 18
845 49 20 98 17
845 41 16 98 17
845 26 14 39 16
845 31 16 39 16
845 73 6 46 10
845 48 25 46 10
845 52 7 22 19
845 49 20 41 17
845 41 16 41 17
845 17 14 17 10
845 96 20 24 15
845 97 25 24 15
845 73 6 22 12
845 48 25 22 12
845 NULL NULL 63 19
845 27 17 91 25
845 73 6 85 12
845 48 25 85 12
845 85 23 32 23
845 23 21 32 23
845 49 20 82 17
845 41 16 82 17
845 NULL NULL 93 7
845 38 17 17 7
845 52 7 29 6
845 81 6 NULL NULL
845 46 20 NULL NULL
845 93 19 NULL NULL
845 42 25 NULL NULL
845 86 25 NULL NULL
845 55 25 NULL NULL
845 95 9 NULL NULL
845 77 8 NULL NULL
845 14 10 NULL NULL
845 29 9 NULL NULL
845 25 16 NULL NULL
845 92 16 NULL NULL

output:

845 579 Rose 1987 580 1635 566

select  con.contest_id,
        con.hacker_id, 
        con.name, 
        sum(total_submissions), 
        sum(total_accepted_submissions), 
        sum(total_views), sum(total_unique_views)
from contests con 
join colleges col on con.contest_id = col.contest_id 
join challenges cha on  col.college_id = cha.college_id 
left join
(select challenge_id, sum(total_views) as total_views, sum(total_unique_views) as total_unique_views
   from view_stats 
 group by challenge_id) vs on cha.challenge_id = vs.challenge_id 
left join
(select challenge_id, sum(total_submissions) as total_submissions, sum(total_accepted_submissions) as total_accepted_submissions 
   from submission_stats 
 group by challenge_id) ss on cha.challenge_id = ss.challenge_id
group by con.contest_id, con.hacker_id, con.name
order by con.contest_id
;

2.3. Placements

input:

Students:
1 Samantha
2 Julia
3 Britney
4 Kristeen
5 Dyana
6 Jenny
7 Christene
8 Meera
9 Priya
10 Priyanka
11 Paige
12 Jane
13 Belvet
14 Scarlet
15 Salma
16 Amanda
17 Maria
18 Stuart
19 Aamina
20 Amina

Friends:
1 14
2 15
3 18
4 19
5 20
6 5
7 6
8 7
9 8
10 1
11 2
12 3
13 4
14 9
15 10
16 11
17 12
18 13
19 16
20 17

Packages:
1 15.5
2 15.6
3 16.7
4 18.8
5 31.5
6 45
7 47
8 46
9 39
10 11.1
11 12.1
12 13.1
13 14.1
14 15.1
15 17.1
16 21.1
17 31.1
18 13.15
19 33.33
20 22.16

output:

Samantha
Julia
Scarlet

select s.name
     -- , s.id
     -- , mysal.salary
     -- , f.friend_id
     -- , frdsal.salary
from Students s
join Packages mysal on s.id = mysal.id
join Friends f on s.id = f.id
join Packages frdsal on f.friend_id = frdsal.id
where mysal.salary < frdsal.salary
order by frdsal.salary
;

2.4. Symmetric Pairs

input:

11 11
11 15 Symmetric(11,15)
12 12
13 13 Symmetric(13,13)
13 13 Symmetric(13,13) 14 14
14 12
15 15
15 11 Symmetric(11,15)

output:

11 15
13 13


select outf.x
     , outf.y
     , inf.x
     , inf.y
join까지
>   11 11 11 11
    11 15 15 11
    12 12 12 12
    13 13 13 13
    13 13 13 13
    13 13 13 13
    13 13 13 13
    14 14 14 14
    15 11 11 15
    15 15 15 15


select outf.x
     , outf.y
     , count(inf.x)
group by까지  
>   11 11 1
    11 15 1
    12 12 1
    13 13 4
    14 14 1
    15 11 1
    15 15 1

select outf.x
     , outf.y
     , count(inf.x)
from Functions outf
join Functions inf on ( outf.x = inf.y and outf.y = inf.x) 
group by outf.x, outf.y
having outf.x < outf.y 
    or count(outf.x) > 1
order by 1
;

> 11 15 
  13 13 


2.5. Print Prime numbers

소수 구하기

input:

X

output:

2&3&5&7

소수 아닌 애들 거르는 작업
with num as 
(select level as no
        from dual
        connect by level <= 10)
select n1.no, n2.no, mod(n1.no, n2.no)
from num n1
   , num n2
where n1.no > n2.no
  and ( n2.no <> 1 and mod(n1.no, n2.no) = 0 )
-- group by n1.no
order by 1
 ;

> 4 2 0
  6 2 0
  6 3 0
  8 2 0
  8 4 0
  9 3 0
  10 2 0
  10 5 0
  
  
pivot 전
with num as 
(select level as no
        from dual
        connect by level <= 10)
select num.no
from num
where num.no not in (select n1.no
                from num n1
                   , num n2
                where n1.no > n2.no
                  and ( n2.no <> 1 and mod(n1.no, n2.no) = 0 )
                group by n1.no)
and num.no <> 1
order by 1
;
> 2
  3
  5
  7
  
LISTAGG로 피봇팅
with num as 
(select level as no
        from dual
        connect by level <= 10)
select LISTAGG(num.no,'&') within group(order by num.no)
from num
where num.no not in (select n1.no
                from num n1
                   , num n2
                where n1.no > n2.no
                  and ( n2.no <> 1 and mod(n1.no, n2.no) = 0 )
                group by n1.no)
and num.no <> 1
order by 1
;
> 2&3&5&7