Skip to content

Commit 822b433

Browse files
authored
Add files via upload
1 parent dae36da commit 822b433

5 files changed

+256
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
-- Question 87
2+
-- A university uses 2 data tables, student and department, to store data about its students
3+
-- and the departments associated with each major.
4+
5+
-- Write a query to print the respective department name and number of students majoring in each
6+
-- department for all departments in the department table (even ones with no current students).
7+
8+
-- Sort your results by descending number of students; if two or more departments have the same number of students,
9+
-- then sort those departments alphabetically by department name.
10+
11+
-- The student is described as follow:
12+
13+
-- | Column Name | Type |
14+
-- |--------------|-----------|
15+
-- | student_id | Integer |
16+
-- | student_name | String |
17+
-- | gender | Character |
18+
-- | dept_id | Integer |
19+
-- where student_id is the student's ID number, student_name is the student's name, gender is their gender, and dept_id is the department ID associated with their declared major.
20+
21+
-- And the department table is described as below:
22+
23+
-- | Column Name | Type |
24+
-- |-------------|---------|
25+
-- | dept_id | Integer |
26+
-- | dept_name | String |
27+
-- where dept_id is the department's ID number and dept_name is the department name.
28+
29+
-- Here is an example input:
30+
-- student table:
31+
32+
-- | student_id | student_name | gender | dept_id |
33+
-- |------------|--------------|--------|---------|
34+
-- | 1 | Jack | M | 1 |
35+
-- | 2 | Jane | F | 1 |
36+
-- | 3 | Mark | M | 2 |
37+
-- department table:
38+
39+
-- | dept_id | dept_name |
40+
-- |---------|-------------|
41+
-- | 1 | Engineering |
42+
-- | 2 | Science |
43+
-- | 3 | Law |
44+
-- The Output should be:
45+
46+
-- | dept_name | student_number |
47+
-- |-------------|----------------|
48+
-- | Engineering | 2 |
49+
-- | Science | 1 |
50+
-- | Law | 0 |
51+
52+
-- Solution
53+
select dept_name, count(s.dept_id) as student_number
54+
from department d
55+
left join student s
56+
on d.dept_id = s.dept_id
57+
group by d.dept_id
58+
order by count(s.dept_id) desc, dept_name
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- Question 86
2+
-- Get the highest answer rate question from a table survey_log with these columns: id, action, question_id, answer_id, q_num, timestamp.
3+
4+
-- id means user id; action has these kind of values: "show", "answer", "skip"; answer_id is not null when action column is "answer",
5+
-- while is null for "show" and "skip"; q_num is the numeral order of the question in current session.
6+
7+
-- Write a sql query to identify the question which has the highest answer rate.
8+
9+
-- Example:
10+
11+
-- Input:
12+
-- +------+-----------+--------------+------------+-----------+------------+
13+
-- | id | action | question_id | answer_id | q_num | timestamp |
14+
-- +------+-----------+--------------+------------+-----------+------------+
15+
-- | 5 | show | 285 | null | 1 | 123 |
16+
-- | 5 | answer | 285 | 124124 | 1 | 124 |
17+
-- | 5 | show | 369 | null | 2 | 125 |
18+
-- | 5 | skip | 369 | null | 2 | 126 |
19+
-- +------+-----------+--------------+------------+-----------+------------+
20+
-- Output:
21+
-- +-------------+
22+
-- | survey_log |
23+
-- +-------------+
24+
-- | 285 |
25+
-- +-------------+
26+
-- Explanation:
27+
-- question 285 has answer rate 1/1, while question 369 has 0/1 answer rate, so output 285.
28+
29+
30+
-- Note: The highest answer rate meaning is: answer number's ratio in show number in the same question.
31+
32+
-- Solution
33+
with t1 as(
34+
select a.question_id, coalesce(b.answer/a.show_1,0) as rate
35+
from
36+
(select question_id, coalesce(count(*),0) as show_1
37+
from survey_log
38+
where action != 'answer'
39+
group by question_id) a
40+
left join
41+
(select question_id, coalesce(count(*),0) as answer
42+
from survey_log
43+
where action = 'answer'
44+
group by question_id) b
45+
on a.question_id = b.question_id)
46+
47+
select a.question_id as survey_log
48+
from
49+
( select t1.question_id,
50+
rank() over(order by rate desc) as rk
51+
from t1) a
52+
where a.rk = 1
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- Question 90
2+
-- Table: Sales
3+
4+
-- +-------------+-------+
5+
-- | Column Name | Type |
6+
-- +-------------+-------+
7+
-- | sale_id | int |
8+
-- | product_id | int |
9+
-- | year | int |
10+
-- | quantity | int |
11+
-- | price | int |
12+
-- +-------------+-------+
13+
-- sale_id is the primary key of this table.
14+
-- product_id is a foreign key to Product table.
15+
-- Note that the price is per unit.
16+
-- Table: Product
17+
18+
-- +--------------+---------+
19+
-- | Column Name | Type |
20+
-- +--------------+---------+
21+
-- | product_id | int |
22+
-- | product_name | varchar |
23+
-- +--------------+---------+
24+
-- product_id is the primary key of this table.
25+
26+
27+
-- Write an SQL query that selects the product id, year, quantity, and price for the first year of every product sold.
28+
29+
-- The query result format is in the following example:
30+
31+
-- Sales table:
32+
-- +---------+------------+------+----------+-------+
33+
-- | sale_id | product_id | year | quantity | price |
34+
-- +---------+------------+------+----------+-------+
35+
-- | 1 | 100 | 2008 | 10 | 5000 |
36+
-- | 2 | 100 | 2009 | 12 | 5000 |
37+
-- | 7 | 200 | 2011 | 15 | 9000 |
38+
-- +---------+------------+------+----------+-------+
39+
40+
-- Product table:
41+
-- +------------+--------------+
42+
-- | product_id | product_name |
43+
-- +------------+--------------+
44+
-- | 100 | Nokia |
45+
-- | 200 | Apple |
46+
-- | 300 | Samsung |
47+
-- +------------+--------------+
48+
49+
-- Result table:
50+
-- +------------+------------+----------+-------+
51+
-- | product_id | first_year | quantity | price |
52+
-- +------------+------------+----------+-------+
53+
-- | 100 | 2008 | 10 | 5000 |
54+
-- | 200 | 2011 | 15 | 9000 |
55+
-- +------------+------------+----------+-------+
56+
57+
-- Solution
58+
select a.product_id, a.year as first_year, a.quantity, a.price
59+
from
60+
( select product_id, quantity, price, year,
61+
rank() over(partition by product_id order by year) as rk
62+
from sales
63+
) a
64+
where a.rk = 1
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- Question 89
2+
-- Table point_2d holds the coordinates (x,y) of some unique points (more than two) in a plane.
3+
4+
5+
-- Write a query to find the shortest distance between these points rounded to 2 decimals.
6+
7+
8+
-- | x | y |
9+
-- |----|----|
10+
-- | -1 | -1 |
11+
-- | 0 | 0 |
12+
-- | -1 | -2 |
13+
14+
15+
-- The shortest distance is 1.00 from point (-1,-1) to (-1,2). So the output should be:
16+
17+
18+
-- | shortest |
19+
-- |----------|
20+
-- | 1.00 |
21+
22+
23+
-- Note: The longest distance among all the points are less than 10000.
24+
25+
-- Solution
26+
select round(a.shortest,2) as shortest
27+
from(
28+
select sqrt(pow((p1.x-p2.x),2)+pow((p1.y-p2.y),2)) as shortest
29+
from point_2d p1
30+
cross join point_2d p2
31+
where p1.x!=p2.x or p1.y!=p2.y
32+
order by sqrt(pow((p1.x-p2.x),2)+pow((p1.y-p2.y),2))
33+
limit 1) a

Medium/Winning Candidate.sql

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- Question 88
2+
-- Table: Candidate
3+
4+
-- +-----+---------+
5+
-- | id | Name |
6+
-- +-----+---------+
7+
-- | 1 | A |
8+
-- | 2 | B |
9+
-- | 3 | C |
10+
-- | 4 | D |
11+
-- | 5 | E |
12+
-- +-----+---------+
13+
-- Table: Vote
14+
15+
-- +-----+--------------+
16+
-- | id | CandidateId |
17+
-- +-----+--------------+
18+
-- | 1 | 2 |
19+
-- | 2 | 4 |
20+
-- | 3 | 3 |
21+
-- | 4 | 2 |
22+
-- | 5 | 5 |
23+
-- +-----+--------------+
24+
-- id is the auto-increment primary key,
25+
-- CandidateId is the id appeared in Candidate table.
26+
-- Write a sql to find the name of the winning candidate, the above example will return the winner B.
27+
28+
-- +------+
29+
-- | Name |
30+
-- +------+
31+
-- | B |
32+
-- +------+
33+
-- Notes:
34+
35+
-- You may assume there is no tie, in other words there will be only one winning candidate
36+
37+
-- Solution
38+
with t1 as (
39+
select *, rank() over(order by b.votes desc) as rk
40+
from candidate c
41+
join
42+
(select candidateid, count(*) as votes
43+
from vote
44+
group by candidateid) b
45+
on c.id = b.candidateid)
46+
47+
select t1.name
48+
from t1
49+
where t1.rk=1

0 commit comments

Comments
 (0)