Skip to content

Commit 1052cf7

Browse files
authored
8 done today
1 parent 07ef0ad commit 1052cf7

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

Medium/Active Businesses.sql

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
-- Question 65
2+
-- Table: Events
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | business_id | int |
8+
-- | event_type | varchar |
9+
-- | occurences | int |
10+
-- +---------------+---------+
11+
-- (business_id, event_type) is the primary key of this table.
12+
-- Each row in the table logs the info that an event of some type occured at some business for a number of times.
13+
14+
15+
-- Write an SQL query to find all active businesses.
16+
17+
-- An active business is a business that has more than one event type with occurences greater than the average occurences of that event type among all businesses.
18+
19+
-- The query result format is in the following example:
20+
21+
-- Events table:
22+
-- +-------------+------------+------------+
23+
-- | business_id | event_type | occurences |
24+
-- +-------------+------------+------------+
25+
-- | 1 | reviews | 7 |
26+
-- | 3 | reviews | 3 |
27+
-- | 1 | ads | 11 |
28+
-- | 2 | ads | 7 |
29+
-- | 3 | ads | 6 |
30+
-- | 1 | page views | 3 |
31+
-- | 2 | page views | 12 |
32+
-- +-------------+------------+------------+
33+
34+
-- Result table:
35+
-- +-------------+
36+
-- | business_id |
37+
-- +-------------+
38+
-- | 1 |
39+
-- +-------------+
40+
-- Average for 'reviews', 'ads' and 'page views' are (7+3)/2=5, (11+7+6)/3=8, (3+12)/2=7.5 respectively.
41+
-- Business with id 1 has 7 'reviews' events (more than 5) and 11 'ads' events (more than 8) so it is an active business.
42+
43+
-- Solution
44+
select c.business_id
45+
from(
46+
select *
47+
from events e
48+
join
49+
(select event_type as event, round(avg(occurences),2) as average from events group by event_type) b
50+
on e.event_type = b.event) c
51+
where c.occurences>c.average
52+
group by c.business_id
53+
having count(*) > 1

Medium/Apples & Oranges.sql

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- Question 66
2+
-- Table: Sales
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | sale_date | date |
8+
-- | fruit | enum |
9+
-- | sold_num | int |
10+
-- +---------------+---------+
11+
-- (sale_date,fruit) is the primary key for this table.
12+
-- This table contains the sales of "apples" and "oranges" sold each day.
13+
14+
15+
-- Write an SQL query to report the difference between number of apples and oranges sold each day.
16+
17+
-- Return the result table ordered by sale_date in format ('YYYY-MM-DD').
18+
19+
-- The query result format is in the following example:
20+
21+
22+
23+
-- Sales table:
24+
-- +------------+------------+-------------+
25+
-- | sale_date | fruit | sold_num |
26+
-- +------------+------------+-------------+
27+
-- | 2020-05-01 | apples | 10 |
28+
-- | 2020-05-01 | oranges | 8 |
29+
-- | 2020-05-02 | apples | 15 |
30+
-- | 2020-05-02 | oranges | 15 |
31+
-- | 2020-05-03 | apples | 20 |
32+
-- | 2020-05-03 | oranges | 0 |
33+
-- | 2020-05-04 | apples | 15 |
34+
-- | 2020-05-04 | oranges | 16 |
35+
-- +------------+------------+-------------+
36+
37+
-- Result table:
38+
-- +------------+--------------+
39+
-- | sale_date | diff |
40+
-- +------------+--------------+
41+
-- | 2020-05-01 | 2 |
42+
-- | 2020-05-02 | 0 |
43+
-- | 2020-05-03 | 20 |
44+
-- | 2020-05-04 | -1 |
45+
-- +------------+--------------+
46+
47+
-- Day 2020-05-01, 10 apples and 8 oranges were sold (Difference 10 - 8 = 2).
48+
-- Day 2020-05-02, 15 apples and 15 oranges were sold (Difference 15 - 15 = 0).
49+
-- Day 2020-05-03, 20 apples and 0 oranges were sold (Difference 20 - 0 = 20).
50+
-- Day 2020-05-04, 15 apples and 16 oranges were sold (Difference 15 - 16 = -1).
51+
52+
-- Solution
53+
Select sale_date, sold_num-sold as diff
54+
from
55+
((select *
56+
from sales
57+
where fruit = 'apples') a
58+
join
59+
(select sale_date as sale, fruit, sold_num as sold
60+
from sales
61+
where fruit = 'oranges') b
62+
on a.sale_date = b.sale)

Medium/Unpopular Books.sql

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
-- Question 64
2+
-- Table: Books
3+
4+
-- +----------------+---------+
5+
-- | Column Name | Type |
6+
-- +----------------+---------+
7+
-- | book_id | int |
8+
-- | name | varchar |
9+
-- | available_from | date |
10+
-- +----------------+---------+
11+
-- book_id is the primary key of this table.
12+
-- Table: Orders
13+
14+
-- +----------------+---------+
15+
-- | Column Name | Type |
16+
-- +----------------+---------+
17+
-- | order_id | int |
18+
-- | book_id | int |
19+
-- | quantity | int |
20+
-- | dispatch_date | date |
21+
-- +----------------+---------+
22+
-- order_id is the primary key of this table.
23+
-- book_id is a foreign key to the Books table.
24+
25+
26+
-- Write an SQL query that reports the books that have sold less than 10 copies in the last year, excluding books that have been available for less than 1 month from today. Assume today is 2019-06-23.
27+
28+
-- The query result format is in the following example:
29+
30+
-- Books table:
31+
-- +---------+--------------------+----------------+
32+
-- | book_id | name | available_from |
33+
-- +---------+--------------------+----------------+
34+
-- | 1 | "Kalila And Demna" | 2010-01-01 |
35+
-- | 2 | "28 Letters" | 2012-05-12 |
36+
-- | 3 | "The Hobbit" | 2019-06-10 |
37+
-- | 4 | "13 Reasons Why" | 2019-06-01 |
38+
-- | 5 | "The Hunger Games" | 2008-09-21 |
39+
-- +---------+--------------------+----------------+
40+
41+
-- Orders table:
42+
-- +----------+---------+----------+---------------+
43+
-- | order_id | book_id | quantity | dispatch_date |
44+
-- +----------+---------+----------+---------------+
45+
-- | 1 | 1 | 2 | 2018-07-26 |
46+
-- | 2 | 1 | 1 | 2018-11-05 |
47+
-- | 3 | 3 | 8 | 2019-06-11 |
48+
-- | 4 | 4 | 6 | 2019-06-05 |
49+
-- | 5 | 4 | 5 | 2019-06-20 |
50+
-- | 6 | 5 | 9 | 2009-02-02 |
51+
-- | 7 | 5 | 8 | 2010-04-13 |
52+
-- +----------+---------+----------+---------------+
53+
54+
-- Result table:
55+
-- +-----------+--------------------+
56+
-- | book_id | name |
57+
-- +-----------+--------------------+
58+
-- | 1 | "Kalila And Demna" |
59+
-- | 2 | "28 Letters" |
60+
-- | 5 | "The Hunger Games" |
61+
-- +-----------+--------------------+
62+
63+
-- Solution
64+
select b.book_id, name
65+
from
66+
(select *
67+
from books
68+
where available_from < '2019-05-23') b
69+
left join
70+
(select *
71+
from orders
72+
where dispatch_date > '2018-06-23') a
73+
on a.book_id = b.book_id
74+
group by b.book_id, name
75+
having coalesce(sum(quantity),0)<10

0 commit comments

Comments
 (0)