Skip to content

Commit a11fcd6

Browse files
committed
add: new sql added
1 parent e8e0a54 commit a11fcd6

11 files changed

+140
-0
lines changed

HackerRank/SQL/New Companies.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
select
2+
company.company_code,
3+
company.founder,
4+
count(distinct lead_manager.lead_manager_code),
5+
count(distinct senior_manager.senior_manager_code),
6+
count(distinct manager.manager_code),
7+
count(distinct employee.employee_code)
8+
from company
9+
inner join lead_manager on lead_manager.company_code = company.company_code
10+
inner join senior_manager on senior_manager.company_code = company.company_code
11+
inner join manager on manager.company_code = company.company_code
12+
inner join employee on employee.company_code = company.company_code
13+
group by company.company_code, company.founder
14+
order by company.company_code
15+
asc
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
with whole_data as (
2+
select
3+
hackers.hacker_id as hacker_id,
4+
hackers.name as name,
5+
max(score) as max_score,
6+
submissions.challenge_id
7+
from hackers
8+
inner join submissions on submissions.hacker_id = hackers.hacker_id
9+
group by submissions.challenge_id, hackers.hacker_id, hackers.name
10+
having max(score) > 0
11+
)
12+
13+
select
14+
hacker_id,
15+
name,
16+
sum(max_score) as total_score
17+
from whole_data
18+
group by hacker_id, name
19+
order by total_score desc, hacker_id asc
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare @no_of_stars INT
2+
set @no_of_stars = 20
3+
while @no_of_stars > 0
4+
begin
5+
print replicate("* ", @no_of_stars)
6+
set @no_of_stars = @no_of_stars - 1
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare @no_of_stars INT
2+
set @no_of_stars = 1
3+
while @no_of_stars < 21
4+
begin
5+
print replicate("* ", @no_of_stars)
6+
set @no_of_stars = @no_of_stars + 1
7+
end

HackerRank/SQL/occupations.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
with data as (
2+
select
3+
name, occupation,
4+
row_number() over(partition by occupation order by name asc) as row_num
5+
from occupations
6+
)
7+
8+
select
9+
max(case when occupation ="Doctor" then name end) as doc_n,
10+
max(case when occupation ="Professor" then name end) as pro_n,
11+
max(case when occupation ="Singer" then name end) as sin_n,
12+
max(case when occupation ="Actor" then name end) as act_n
13+
from
14+
data
15+
group by row_num

HackerRank/SQL/the-blunder.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select ceil(avg(salary)- avg(replace(salary,'0',''))) from employees;

HackerRank/SQL/the-pads.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
select
2+
concat(name,'(',left(occupation,1),')')
3+
from occupations
4+
order by name;
5+
select
6+
"There are a total of",count(occupation) as cnt,concat(lower(occupation),'s.')
7+
from occupations
8+
group by occupation
9+
order by cnt asc;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
select
2+
round(abs(min(lat_n)-max(lat_n)) + abs(min(long_w)-max(long_w)),4) as ans
3+
from station
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
select
2+
round(sqrt(
3+
power(max(lat_n)-min(lat_n),2) +
4+
power(max(long_w)-min(long_w),2)
5+
),4) as ans
6+
from
7+
station
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
with rank_data as (
2+
select
3+
lat_n,
4+
ROW_NUMBER() over (order by lat_n asc) as cnt,
5+
count(*) over () as total
6+
from station order by lat_n asc
7+
)
8+
9+
select
10+
round(src.lat_n,4)
11+
from
12+
rank_data as src
13+
where src.cnt = (
14+
case
15+
when src.total%2=1 then (src.total+1)/2
16+
when src.total%2=0 then src.total/2 + ((src.total/2)+1)/2
17+
end
18+
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
const ll INF=1e18;
7+
const ll mod1=1e9+7;
8+
const ll mod2=998244353;
9+
//Add main code here
10+
11+
class Solution
12+
{
13+
public:
14+
vector<int> queryResults(int limit, vector<vector<int>> &queries)
15+
{
16+
map<int, int> cnt, color;
17+
18+
int n = queries.size();
19+
vector<int> ans(n);
20+
21+
for (int i = 0; i < n; i++)
22+
{
23+
int x = queries[i][0];
24+
int y = queries[i][1];
25+
if (color.find(x) != color.end())
26+
{
27+
cnt[color[x]]--;
28+
if (cnt[color[x]] == 0)
29+
cnt.erase(color[x]);
30+
}
31+
32+
color[x] = y;
33+
cnt[y]++;
34+
ans[i] = cnt.size();
35+
}
36+
37+
return ans;
38+
}
39+
};

0 commit comments

Comments
 (0)