forked from MrLRTripp/HackerRank-LeetCode-StrataScratch-SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerRank Placements.sql
More file actions
80 lines (64 loc) · 1.47 KB
/
HackerRank Placements.sql
File metadata and controls
80 lines (64 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
set search_path to data_sci
select substring ('asubstring' from '.{3}$')
select distinct last_name FROM
employees
where lower(right(last_name,1)) in ('a','e','i','o','u')
order by last_name ASC
select
array_agg(job_title) over (partition by job_title order by job_title asc)
from employees
ORDER by right(job_title,1)
select job_title, count(id) as num_emp
from employees
group by job_title
order by num_emp desc
create table students(
id integer,
name text);
create table friends(
id integer,
friend_id integer);
create table packages(
id integer,
salary real);
INSERT into students
values
(1,'name1'),
(2, 'name2'),
(3, 'name3'),
(4, 'name4');
insert into friends
values
(1,2),
(2,3),
(3,4),
(4,1);
insert into packages
values
(1, 15.0),
(2,10.0),
(3, 11.0),
(4,12.0);
-- Find friend sal
with f_sal as(
select s.id s_id, f.friend_id f_id, p.salary friend_salary
from students s
join friends f on s.id = f.id
join packages p on f.friend_id = p.id
order by s.id asc
)
select s.name, p.salary
from students s
join packages p on s.id = p.id
join f_sal on s.id = f_sal.s_id
where p.salary < f_sal.friend_salary
order by f_sal.friend_salary
-- Can actually do this in one select by joining to packages twice
-- select s.id s_id, f.friend_id f_id, sp.salary student_salary, fp.salary friend_salary
select s.name
from students s
join friends f on s.id = f.id
join packages sp on s.id = sp.id
join packages fp on f.friend_id = fp.id
where sp.salary < fp.salary
order by fp.salary asc