Skip to content

Commit 18bf77d

Browse files
authored
Attempted 5 more today
1 parent ead2159 commit 18bf77d

5 files changed

+247
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Question 21
2+
-- Table: ActorDirector
3+
4+
-- +-------------+---------+
5+
-- | Column Name | Type |
6+
-- +-------------+---------+
7+
-- | actor_id | int |
8+
-- | director_id | int |
9+
-- | timestamp | int |
10+
-- +-------------+---------+
11+
-- timestamp is the primary key column for this table.
12+
13+
14+
-- Write a SQL query for a report that provides the pairs (actor_id, director_id) where the actor have cooperated with the director at least 3 times.
15+
16+
-- Example:
17+
18+
-- ActorDirector table:
19+
-- +-------------+-------------+-------------+
20+
-- | actor_id | director_id | timestamp |
21+
-- +-------------+-------------+-------------+
22+
-- | 1 | 1 | 0 |
23+
-- | 1 | 1 | 1 |
24+
-- | 1 | 1 | 2 |
25+
-- | 1 | 2 | 3 |
26+
-- | 1 | 2 | 4 |
27+
-- | 2 | 1 | 5 |
28+
-- | 2 | 1 | 6 |
29+
-- +-------------+-------------+-------------+
30+
31+
-- Result table:
32+
-- +-------------+-------------+
33+
-- | actor_id | director_id |
34+
-- +-------------+-------------+
35+
-- | 1 | 1 |
36+
-- +-------------+-------------+
37+
-- The only pair is (1, 1) where they cooperated exactly 3 times.
38+
39+
-- Solution
40+
Select actor_id, director_id
41+
from actordirector
42+
group by actor_id, director_id
43+
having count(*)>=3

Easy/Biggest Single number.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- Question 24
2+
-- Table my_numbers contains many numbers in column num including duplicated ones.
3+
-- Can you write a SQL query to find the biggest number, which only appears once.
4+
5+
-- +---+
6+
-- |num|
7+
-- +---+
8+
-- | 8 |
9+
-- | 8 |
10+
-- | 3 |
11+
-- | 3 |
12+
-- | 1 |
13+
-- | 4 |
14+
-- | 5 |
15+
-- | 6 |
16+
-- For the sample data above, your query should return the following result:
17+
-- +---+
18+
-- |num|
19+
-- +---+
20+
-- | 6 |
21+
-- Note:
22+
-- If there is no such number, just output null.
23+
24+
-- Solution
25+
Select max(a.num) as num
26+
from
27+
(
28+
select num, count(*)
29+
from my_numbers
30+
group by num
31+
having count(*)=1
32+
) a

Easy/Shortest Distance.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- Question 25
2+
-- Table point holds the x coordinate of some points on x-axis in a plane, which are all integers.
3+
4+
5+
-- Write a query to find the shortest distance between two points in these points.
6+
7+
8+
-- | x |
9+
-- |-----|
10+
-- | -1 |
11+
-- | 0 |
12+
-- | 2 |
13+
14+
15+
-- The shortest distance is '1' obviously, which is from point '-1' to '0'. So the output is as below:
16+
17+
18+
-- | shortest|
19+
-- |---------|
20+
-- | 1 |
21+
22+
23+
-- Note: Every point is unique, which means there is no duplicates in table point
24+
25+
-- Solution
26+
select min(abs(abs(a.x)-abs(a.next_closest))) as shortest
27+
from(
28+
select *,
29+
lead(x) over(order by x) as next_closest
30+
from point) a

Easy/Students and Examinations.sql

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
-- Question 23
2+
-- Table: Students
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | student_id | int |
8+
-- | student_name | varchar |
9+
-- +---------------+---------+
10+
-- student_id is the primary key for this table.
11+
-- Each row of this table contains the ID and the name of one student in the school.
12+
13+
14+
-- Table: Subjects
15+
16+
-- +--------------+---------+
17+
-- | Column Name | Type |
18+
-- +--------------+---------+
19+
-- | subject_name | varchar |
20+
-- +--------------+---------+
21+
-- subject_name is the primary key for this table.
22+
-- Each row of this table contains the name of one subject in the school.
23+
24+
25+
-- Table: Examinations
26+
27+
-- +--------------+---------+
28+
-- | Column Name | Type |
29+
-- +--------------+---------+
30+
-- | student_id | int |
31+
-- | subject_name | varchar |
32+
-- +--------------+---------+
33+
-- There is no primary key for this table. It may contain duplicates.
34+
-- Each student from the Students table takes every course from Subjects table.
35+
-- Each row of this table indicates that a student with ID student_id attended the exam of subject_name.
36+
37+
38+
-- Write an SQL query to find the number of times each student attended each exam.
39+
40+
-- Order the result table by student_id and subject_name.
41+
42+
-- The query result format is in the following example:
43+
44+
-- Students table:
45+
-- +------------+--------------+
46+
-- | student_id | student_name |
47+
-- +------------+--------------+
48+
-- | 1 | Alice |
49+
-- | 2 | Bob |
50+
-- | 13 | John |
51+
-- | 6 | Alex |
52+
-- +------------+--------------+
53+
-- Subjects table:
54+
-- +--------------+
55+
-- | subject_name |
56+
-- +--------------+
57+
-- | Math |
58+
-- | Physics |
59+
-- | Programming |
60+
-- +--------------+
61+
-- Examinations table:
62+
-- +------------+--------------+
63+
-- | student_id | subject_name |
64+
-- +------------+--------------+
65+
-- | 1 | Math |
66+
-- | 1 | Physics |
67+
-- | 1 | Programming |
68+
-- | 2 | Programming |
69+
-- | 1 | Physics |
70+
-- | 1 | Math |
71+
-- | 13 | Math |
72+
-- | 13 | Programming |
73+
-- | 13 | Physics |
74+
-- | 2 | Math |
75+
-- | 1 | Math |
76+
-- +------------+--------------+
77+
-- Result table:
78+
-- +------------+--------------+--------------+----------------+
79+
-- | student_id | student_name | subject_name | attended_exams |
80+
-- +------------+--------------+--------------+----------------+
81+
-- | 1 | Alice | Math | 3 |
82+
-- | 1 | Alice | Physics | 2 |
83+
-- | 1 | Alice | Programming | 1 |
84+
-- | 2 | Bob | Math | 1 |
85+
-- | 2 | Bob | Physics | 0 |
86+
-- | 2 | Bob | Programming | 1 |
87+
-- | 6 | Alex | Math | 0 |
88+
-- | 6 | Alex | Physics | 0 |
89+
-- | 6 | Alex | Programming | 0 |
90+
-- | 13 | John | Math | 1 |
91+
-- | 13 | John | Physics | 1 |
92+
-- | 13 | John | Programming | 1 |
93+
-- +------------+--------------+--------------+----------------+
94+
-- The result table should contain all students and all subjects.
95+
-- Alice attended Math exam 3 times, Physics exam 2 times and Programming exam 1 time.
96+
-- Bob attended Math exam 1 time, Programming exam 1 time and didn't attend the Physics exam.
97+
-- Alex didn't attend any exam.
98+
-- John attended Math exam 1 time, Physics exam 1 time and Programming exam 1 time.
99+
100+
-- Solution
101+
Select a.student_id as student_id, a.student_name as student_name, a.subject_name as subject_name, coalesce(attended_exams,0) as attended_exams
102+
from(
103+
select *
104+
from students
105+
cross join subjects
106+
group by student_id, student_name, subject_name) a
107+
left join
108+
(Select e.student_id, student_name, subject_name, count(*) as attended_exams
109+
from examinations e join students s
110+
on e.student_id = s.student_id
111+
group by e.student_id, student_name, subject_name) b
112+
on a.student_id = b.student_id and a.subject_name =b.subject_name
113+
order by a.student_id asc, a.subject_name asc

Easy/Swap Salary.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- Question 22
2+
-- Given a table salary, such as the one below, that has m=male and f=female values.
3+
-- Swap all f and m values (i.e., change all f values to m and vice versa) with
4+
-- a single update statement and no intermediate temp table.
5+
6+
-- Note that you must write a single update statement, DO NOT write any select statement for this problem.
7+
8+
9+
-- Example:
10+
11+
-- | id | name | sex | salary |
12+
-- |----|------|-----|--------|
13+
-- | 1 | A | m | 2500 |
14+
-- | 2 | B | f | 1500 |
15+
-- | 3 | C | m | 5500 |
16+
-- | 4 | D | f | 500 |
17+
-- After running your update statement, the above salary table should have the following rows:
18+
-- | id | name | sex | salary |
19+
-- |----|------|-----|--------|
20+
-- | 1 | A | f | 2500 |
21+
-- | 2 | B | m | 1500 |
22+
-- | 3 | C | f | 5500 |
23+
-- | 4 | D | m | 500 |
24+
25+
-- Solution
26+
Update salary
27+
set sex = Case when sex = 'm' then 'f'
28+
when sex = 'f' then 'm'
29+
end;

0 commit comments

Comments
 (0)