├── Advanced-Join ├── 15 Days of Learning SQL ├── Interviews ├── Placements ├── Projects └── Symmetric Pairs ├── Advanced-Select ├── Binary Search Tree ├── New Companies ├── Occupations ├── The PADS └── Type of Triangle ├── Aggregation ├── Average Population ├── Japan Population ├── Population Density Difference ├── Revising Aggregations - Averages ├── Revising Aggregations - The Count Function ├── Revising Aggregations - The Sum Function ├── The Blunder ├── Top Earners ├── Weather Observation Station 13 ├── Weather Observation Station 14 ├── Weather Observation Station 15 ├── Weather Observation Station 16 ├── Weather Observation Station 17 ├── Weather Observation Station 18 ├── Weather Observation Station 19 ├── Weather Observation Station 2 └── Weather Observation Station 20 ├── Alternate-Queries ├── Draw The Triangle 1 ├── Draw The Triangle 2 └── Print Prime Numbers ├── Basic-Join ├── African Cities ├── Asian Population ├── Average Population of Each Continent ├── Challenges ├── Contest Leaderboard ├── Ollivander's Inventory ├── The Report └── Top Competitors ├── Basic-Select-Challenges ├── Employee Names ├── Employee Salaries ├── Higher Than 75 Marks ├── Japanese Cities' Detail ├── Japanese Cities' Name ├── Revising the Select Query - 1 ├── Revising the Select Query - 2 ├── Select All ├── Select by ID ├── Weather Observation Station 1 ├── Weather Observation Station 10 ├── Weather Observation Station 11 ├── Weather Observation Station 12 ├── Weather Observation Station 3 ├── Weather Observation Station 4 ├── Weather Observation Station 5 ├── Weather Observation Station 6 ├── Weather Observation Station 7 ├── Weather Observation Station 8 └── Weather Observation Station 9 └── README.md /Advanced-Join/15 Days of Learning SQL: -------------------------------------------------------------------------------- 1 | select X.submission_date,Y.cts,X.hacker_id,X.name FROM (select submission_date ,hacker_id, name,ctz from ( select submission_date ,hacker_id, name,ctz, row_number() OVER (PARTITION BY submission_date ORDER BY ctz desc,hacker_id asc ) as rown from ( select a.submission_date as submission_date ,a.hacker_id as hacker_id,b.name as name, count(*) ctz from submissions a inner join hackers b on a.hacker_id=b.hacker_id group by a.submission_date,a.hacker_id,b.name)) where rown=1 ) X, (select submission_date,count(distinct hacker_id) as cts from (SELECT submission_date,hacker_id,suma from ( SELECT submission_date,hacker_id , SUM(brk_flag) OVER (PARTITION BY hacker_id ORDER BY submission_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) as suma FROM (SELECT submission_date,hacker_id, prev_date,(CASE WHEN ( submission_date - prev_date) > 1 THEN 1 ELSE 0 END) AS brk_flag FROM (SELECT submission_date,hacker_id , MAX(submission_date) OVER( PARTITION BY hacker_id ORDER BY submission_date ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS prev_date FROM submissions where hacker_id in ( select distinct hacker_id from submissions where submission_date= to_date('01-03-2016' , 'DD-MM-YYYY') ) order by hacker_id,submission_date))) where suma=0 ) group by submission_date order by submission_date ) Y where x.submission_date=y.submission_date; 2 | -------------------------------------------------------------------------------- /Advanced-Join/Interviews: -------------------------------------------------------------------------------- 1 | Select c.Contest_ID, hacker_id, name, SUM(ISNULL(total_views,0)) as total_views,SUM(ISNULL(total_unique_views,0)) as total_unique_views into #View from Contests c inner join Colleges coll on coll.Contest_ID = c.Contest_ID inner join Challenges ch on ch.College_ID = coll.College_ID left join View_Stats v on v.Challenge_ID = ch.Challenge_ID group by c.Contest_ID, hacker_id, name 2 | Select c.Contest_ID, hacker_id, name, SUM(ISNULL(total_submissions,0)) as total_submissions ,SUM(ISNULL(total_accepted_submissions,0)) as total_accepted_submissions into #Submissions from Contests c inner join Colleges coll on coll.Contest_ID = c.Contest_ID inner join Challenges ch on ch.College_ID = coll.College_ID left join Submission_Stats s on s.Challenge_ID = ch.Challenge_ID group by c.Contest_ID, hacker_id, name 3 | Select v.Contest_ID, v.hacker_id, v.name,total_submissions,total_accepted_submissions,total_views,total_unique_views into #temp from #View v inner join #Submissions s on s.Contest_ID = v.Contest_ID 4 | Select * from #temp t1 where not exists (Select 1 from #temp t where total_submissions = 0 and total_accepted_submissions = 0 and total_views = 0 and total_unique_views = 0 and t1.Contest_ID = t.Contest_ID) order by t1.Contest_ID 5 | drop table #View drop table #Submissions drop table #temp 6 | -------------------------------------------------------------------------------- /Advanced-Join/Placements: -------------------------------------------------------------------------------- 1 | 2 | select s.name from students s inner join friends f on s.id = f.id 3 | inner join packages p1 on f.friend_id = p1.id 4 | inner join packages p2 on f.id = p2.id and p1.salary > p2.salary 5 | order by p1.salary 6 | -------------------------------------------------------------------------------- /Advanced-Join/Projects: -------------------------------------------------------------------------------- 1 | SELECT Root, End_Date FROM 2 | (SELECT Start_Date, End_Date, CONNECT_BY_ROOT Start_Date AS Root, LEVEL AS duration FROM Projects WHERE CONNECT_BY_ISLEAF = 1 3 | CONNECT BY PRIOR End_Date = Start_Date) 4 | WHERE Root NOT IN (SELECT End_Date FROM Projects) ORDER BY duration, Root; 5 | -------------------------------------------------------------------------------- /Advanced-Join/Symmetric Pairs: -------------------------------------------------------------------------------- 1 | select distinct x,y from functions a where exists (select 1 from functions b where a.x = b.y and b.x = a.y and a.x <= a.y and a.rowid <> b.rowid ) order by x,y; 2 | -------------------------------------------------------------------------------- /Advanced-Select/Binary Search Tree: -------------------------------------------------------------------------------- 1 | select n, 2 | case 3 | when (p is null) then 'Root' 4 | when (n in (select p from bst)) then 'Inner' 5 | else 'Leaf' end from bst order by n; 6 | -------------------------------------------------------------------------------- /Advanced-Select/New Companies: -------------------------------------------------------------------------------- 1 | with 2 | LM as (select count (distinct lead_manager_code) as LM_count, company_code from Lead_Manager group by company_code), 3 | 4 | SM as (select count(distinct senior_manager_code) as SM_count, company_code from Senior_Manager group by company_code ), 5 | 6 | MC as (Select count(distinct manager_code) as MC_count, company_code from Manager group by company_code ), 7 | 8 | EC as (Select count(distinct employee_code) as EC_count, company_code from Employee group by company_code ) 9 | 10 | select 11 | C.company_code, founder, max(LM_count), max(SM_count), max(MC_count), max(EC_count) 12 | from Company C join LM on C.company_code = LM.company_code 13 | join SM on C.company_code = SM.company_code 14 | join MC on C.company_code = MC.company_code 15 | join EC on C.company_code = EC.company_code 16 | 17 | group by founder, C.company_code 18 | order by C.company_code 19 | -------------------------------------------------------------------------------- /Advanced-Select/Occupations: -------------------------------------------------------------------------------- 1 | select Doctor, Professor, Singer, Actor from 2 | (select name, occupation, row_number() 3 | over (partition by occupation order by name)rn from occupations) 4 | as source pivot 5 | (max(name) for occupation in (Doctor, Professor, Singer, Actor)) as pvt 6 | -------------------------------------------------------------------------------- /Advanced-Select/The PADS: -------------------------------------------------------------------------------- 1 | SELECT CONCAT(name,'(',SUBSTRING(occupation,1,1),')') from occupations order by name 2 | select concat('There are total',' ',count(occupation),' ',lower(occupation),'s.') as total 3 | from occupations 4 | group by occupation 5 | order by total 6 | -------------------------------------------------------------------------------- /Advanced-Select/Type of Triangle: -------------------------------------------------------------------------------- 1 | SELECT 2 | CASE WHEN A = B AND B = C THEN 'Equilateral' 3 | WHEN A + B <= C OR B + C <= A OR C + A <= B THEN 'Not A Triangle' 4 | WHEN A != B AND B != C AND C != A THEN 'Scalene' 5 | WHEN (A = B AND B != C) OR (A = C AND B != A) OR ( B = C AND C != A ) THEN 'Isosceles' 6 | END from TRIANGLES 7 | 8 | -------------------------------------------------------------------------------- /Aggregation/Average Population: -------------------------------------------------------------------------------- 1 | select round(avg(population),0) from city 2 | -------------------------------------------------------------------------------- /Aggregation/Japan Population: -------------------------------------------------------------------------------- 1 | select sum(population) from city where countrycode = 'JPN' 2 | -------------------------------------------------------------------------------- /Aggregation/Population Density Difference: -------------------------------------------------------------------------------- 1 | select max(population) - min(population) from city 2 | -------------------------------------------------------------------------------- /Aggregation/Revising Aggregations - Averages: -------------------------------------------------------------------------------- 1 | select avg(population) from city where district = 'california' 2 | -------------------------------------------------------------------------------- /Aggregation/Revising Aggregations - The Count Function: -------------------------------------------------------------------------------- 1 | select count(*) from city where population > 100000 2 | -------------------------------------------------------------------------------- /Aggregation/Revising Aggregations - The Sum Function: -------------------------------------------------------------------------------- 1 | select sum(population) from city where district = 'california' 2 | -------------------------------------------------------------------------------- /Aggregation/The Blunder: -------------------------------------------------------------------------------- 1 | select ceil(avg(salary) - avg(replace(salary, 0, ''))) from EMPLOYEES 2 | -------------------------------------------------------------------------------- /Aggregation/Top Earners: -------------------------------------------------------------------------------- 1 | select max(months*salary), count(employee_id) from employee where months*salary in (select max(months*salary) as earning from employee) 2 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 13: -------------------------------------------------------------------------------- 1 | select round(sum(lat_n),4) from station where lat_n > 38.7880 and lat_n < 137.2345 2 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 14: -------------------------------------------------------------------------------- 1 | select round(max(lat_n),4) from station where lat_n < 137.2345 2 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 15: -------------------------------------------------------------------------------- 1 | select round(long_w,4) from station where lat_n = (select max(lat_n) from station where lat_n < 137.2345 ) 2 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 16: -------------------------------------------------------------------------------- 1 | select round(min(lat_n),4) from station where lat_n > 38.7780 2 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 17: -------------------------------------------------------------------------------- 1 | select round(long_w,4) from station where lat_n = (select min(lat_n) from station where lat_n > 38.7780) 2 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 18: -------------------------------------------------------------------------------- 1 | select 2 | round((abs(min(long_w) - min(lat_n)) + abs(max(long_w) - max(lat_n))),4) 3 | from station 4 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 19: -------------------------------------------------------------------------------- 1 | select 2 | round(sqrt((power(min(long_w) - min(lat_n),2)) + (power(max(long_w) - max(lat_n),2))),4) 3 | from station 4 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 2: -------------------------------------------------------------------------------- 1 | select round(sum(lat_n),2), round(sum(long_w),2) from station 2 | -------------------------------------------------------------------------------- /Aggregation/Weather Observation Station 20: -------------------------------------------------------------------------------- 1 | select ROUND(MAX(median),4) from 2 | ( 3 | select lat_n , percentile_disc(0.5) within group (order by lat_n desc) over () median from station 4 | ) 5 | -------------------------------------------------------------------------------- /Alternate-Queries/Draw The Triangle 1: -------------------------------------------------------------------------------- 1 | declare @total int 2 | declare @string varchar(50) 3 | set @total = 20 4 | while(@total>=1) 5 | begin 6 | set @string = replicate('* ',@total) 7 | print @string 8 | set @total = @total - 1 9 | end 10 | -------------------------------------------------------------------------------- /Alternate-Queries/Draw The Triangle 2: -------------------------------------------------------------------------------- 1 | 2 | declare @total int 3 | declare @string varchar(50) 4 | set @total = 1 5 | while(@total<=20) 6 | begin 7 | set @string = replicate('* ',@total) 8 | print @string 9 | set @total = @total + 1 10 | end 11 | -------------------------------------------------------------------------------- /Alternate-Queries/Print Prime Numbers: -------------------------------------------------------------------------------- 1 | DECLARE 2 | @i INT, 3 | @a INT, 4 | @count INT, 5 | @output VARCHAR(6000) 6 | 7 | SET @i = 1 8 | SET @output = '' 9 | 10 | WHILE (@i <= 1000) 11 | BEGIN SET @count = 0 12 | SET @a = 1 13 | 14 | WHILE (@a <= @i) 15 | BEGIN 16 | IF (@i % @a = 0) 17 | SET @count = @count + 1 18 | 19 | SET @a = @a + 1 20 | END 21 | 22 | IF (@count = 2) 23 | SET @output = @output + CAST(@i as varchar(10)) +'&' 24 | SET @i = @i + 1 25 | END 26 | SET @output = substring(@output, 1,(len(@output)-1)) 27 | PRINT @output 28 | -------------------------------------------------------------------------------- /Basic-Join/African Cities: -------------------------------------------------------------------------------- 1 | select city.name from city 2 | inner join country on city.CountryCode = country.Code 3 | where country.continent = 'Africa'; 4 | -------------------------------------------------------------------------------- /Basic-Join/Asian Population: -------------------------------------------------------------------------------- 1 | select sum(city.population) from city 2 | inner join country on city.CountryCode = country.Code 3 | where country.continent = 'Asia'; 4 | 5 | -------------------------------------------------------------------------------- /Basic-Join/Average Population of Each Continent: -------------------------------------------------------------------------------- 1 | 2 | select country.continent, round(avg(City.Population)-0.5) from country 3 | inner join city on country.code = city.countrycode 4 | group by country.continent; 5 | 6 | -------------------------------------------------------------------------------- /Basic-Join/Challenges: -------------------------------------------------------------------------------- 1 | select ch.hacker_id,hack.name,count(distinct ch.challenge_id) from challenges ch join hackers hack on ch.hacker_id=hack.hacker_id group by ch.hacker_id,name having not exists ( select * from challenges ch2, (select count(distinct challenge_id) as nums from challenges group by hacker_id) P where ch2.hacker_id <> ch.hacker_id group by hacker_id having count(distinct ch2.challenge_id) = count(distinct ch.challenge_id) and count(distinct ch.challenge_id) < max(p.nums)) order by count(distinct ch.challenge_id) desc, ch.hacker_id asc; 2 | -------------------------------------------------------------------------------- /Basic-Join/Contest Leaderboard: -------------------------------------------------------------------------------- 1 | select h.hacker_id,h.name, 2 | SUM(TOTAL_SCORE) AS TOTAL from hackers h, 3 | ( 4 | Select h.name,h.hacker_id,s.challenge_id, 5 | max(s.score) AS TOTAL_SCORE from hackers h, submissions s 6 | where h.hacker_id = s.hacker_id and s.score > 0 7 | group by h.name,h.hacker_id,s.challenge_id) 8 | AS hot 9 | where h.hacker_id = hot.hacker_id group by h.hacker_id,h.name 10 | order by TOTAL desc,h.hacker_id asc; 11 | -------------------------------------------------------------------------------- /Basic-Join/Ollivander's Inventory: -------------------------------------------------------------------------------- 1 | select id, age, coins_needed, power from wands w 2 | inner join wands_property wp on w.code = wp.code 3 | where coins_needed = 4 | (select min(w1.coins_needed) from wands w1 inner join wands_property wp1 on w1.code = wp1.code 5 | where w.power = w1.power and wp.age = wp1.age and wp1.is_evil = 0 6 | ) 7 | order by power desc, age desc; 8 | -------------------------------------------------------------------------------- /Basic-Join/The Report: -------------------------------------------------------------------------------- 1 | select 2 | case when g.grade < 8 then null else s.name end, 3 | g.grade, 4 | s.marks 5 | from students s inner join grades g 6 | on s.marks >= g.min_mark and s.marks <= g.max_mark 7 | order by g.grade desc, s.name, s.marks 8 | -------------------------------------------------------------------------------- /Basic-Join/Top Competitors: -------------------------------------------------------------------------------- 1 | SELECT hacker_id, 2 | NAME 3 | FROM (SELECT s.hacker_id, 4 | h.NAME, 5 | Count(1) cnt 6 | FROM submissions s 7 | INNER JOIN hackers h 8 | ON s.hacker_id = h.hacker_id 9 | INNER JOIN challenges c 10 | ON s.challenge_id = c.challenge_id 11 | INNER JOIN difficulty d 12 | ON c.difficulty_level = d.difficulty_level 13 | AND s.score = d.score 14 | GROUP BY s.hacker_id, 15 | h.NAME)a 16 | WHERE cnt > 1 17 | ORDER BY cnt DESC, 18 | hacker_id; 19 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Employee Names: -------------------------------------------------------------------------------- 1 | select name from employee order by name 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Employee Salaries: -------------------------------------------------------------------------------- 1 | select name from employee where salary > 2000 and months < 10 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Higher Than 75 Marks: -------------------------------------------------------------------------------- 1 | Select Name from STUDENTS where Marks > 75 ORDER BY RIGHT(Name,3),ID ; 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Japanese Cities' Detail: -------------------------------------------------------------------------------- 1 | select * from city where countrycode = 'JPN' 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Japanese Cities' Name: -------------------------------------------------------------------------------- 1 | select name from city where countrycode = 'JPN' 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Revising the Select Query - 1: -------------------------------------------------------------------------------- 1 | select * from city where countrycode = 'USA' and population > 100000 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Revising the Select Query - 2: -------------------------------------------------------------------------------- 1 | select name from city where countrycode = 'USA' and population > 120000 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Select All: -------------------------------------------------------------------------------- 1 | select * from city 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Select by ID: -------------------------------------------------------------------------------- 1 | select * from city where id = 1661 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 1: -------------------------------------------------------------------------------- 1 | select city, state from station 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 10: -------------------------------------------------------------------------------- 1 | select distinct city from station where right(city, 1) not in ('a','e','i','o','u') 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 11: -------------------------------------------------------------------------------- 1 | select distinct city from station where left(city, 1) not in ('a','e','i','o','u') or right(city, 1) not in ('a','e','i','o','u') 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 12: -------------------------------------------------------------------------------- 1 | select distinct city from station where left(city, 1) not in ('a','e','i','o','u') and right(city, 1) not in ('a','e','i','o','u') 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 3: -------------------------------------------------------------------------------- 1 | select distinct city from station where id%2=0 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 4: -------------------------------------------------------------------------------- 1 | select count(city) - count(distinct city) from station 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 5: -------------------------------------------------------------------------------- 1 | select top 1 city, len(city) from station 2 | where len(city) in (select min(len(city)) from station) 3 | group by city 4 | 5 | select top 1 city, len(city) from station 6 | where len(city) in (select max(len(city)) from station) 7 | group by city 8 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 6: -------------------------------------------------------------------------------- 1 | 2 | select distinct city from station where SUBSTRING(city,1,1) in ('a','e','i','o','u') 3 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 7: -------------------------------------------------------------------------------- 1 | select distinct city from station where RIGHT(city,1) in ('a','e','i','o','u') 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 8: -------------------------------------------------------------------------------- 1 | Weather Observation Station 7 2 | -------------------------------------------------------------------------------- /Basic-Select-Challenges/Weather Observation Station 9: -------------------------------------------------------------------------------- 1 | select distinct city from station where left(city, 1) not in ('a','e','i','o','u') 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank_SQL 2 | Completely solved Hacker Rank SQL Domain 3 | --------------------------------------------------------------------------------