Skip to content

Commit 4639c53

Browse files
committed
1098 added
1 parent 6a18f2b commit 4639c53

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
----Solution to LeetCode Database question 1098: Unpopular Books
2+
3+
WITH scopped_books AS
4+
(
5+
SELECT book_id, name
6+
FROM Books
7+
WHERE available_from NOT BETWEEN DATE_ADD('2019-06-23', INTERVAL -1 MONTH) AND '2019-06-23'
8+
),
9+
orders_in_scope AS
10+
(
11+
SELECT o.book_id, o.quantity
12+
FROM Orders AS o
13+
JOIN scopped_books AS sb
14+
ON o.book_id = sb.book_id
15+
WHERE o.dispatch_date BETWEEN DATE_ADD('2019-06-23', INTERVAL -1 YEAR) AND '2019-06-23'
16+
)
17+
18+
SELECT sb.book_id, sb.name
19+
FROM scopped_books AS sb
20+
LEFT JOIN orders_in_scope AS os
21+
ON sb.book_id = os.book_id
22+
GROUP BY sb.book_id
23+
HAVING SUM(IFNULL(os.quantity, 0)) < 10

1098-Unpopular-Books/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
### 1098. Unpopular Books
2+
3+
Table: `Books`
4+
5+
```markdown
6+
+----------------+----------+
7+
| Column Name | Type |
8+
+----------------+----------+
9+
| book_id | int |
10+
| name | varchar |
11+
| available_from | int |
12+
+----------------+----------+
13+
book_id is the primary key of this table.
14+
```
15+
16+
<br/>
17+
18+
Table: `Orders`
19+
20+
```markdown
21+
+----------------+----------+
22+
| Column Name | Type |
23+
+----------------+----------+
24+
| order_id | int |
25+
| book_id | int |
26+
| quantity | int |
27+
| dispatch_date | date |
28+
+----------------+----------+
29+
order_id is the primary key of this table.
30+
book_id is a foreign key to the Books table.
31+
```
32+
33+
<br/>
34+
35+
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 one month from today. **Assume today is** `2019-06-23`.
36+
37+
Return the return table in **any order**.
38+
39+
The query result format is in the following example.
40+
41+
<br/>
42+
43+
**Example 1:**
44+
45+
```markdown
46+
Books table:
47+
+---------+---------------------+-----------------+
48+
| book_id | name | available_from |
49+
+---------+---------------------+-----------------+
50+
| 1 | "Kalila And Demna" | 2010-01-01 |
51+
| 2 | "28 Letters" | 2012-05-12 |
52+
| 3 | "The Hobbit" | 2019-06-10 |
53+
| 4 | "13 Reasons Why" | 2019-06-01 |
54+
| 5 | "The Hunger Games" | 2008-09-21 |
55+
+---------+---------------------+-----------------+
56+
57+
Books table:
58+
+---------+---------+----------+-----------------+
59+
| book_id | book_id | quantity | dispatch_date |
60+
+---------+---------+----------+-----------------+
61+
| 1 | 1 | 2 | 2018-07-26 |
62+
| 2 | 1 | 1 | 2018-11-05 |
63+
| 3 | 3 | 8 | 2019-06-11 |
64+
| 4 | 4 | 6 | 2019-06-05 |
65+
| 5 | 4 | 5 | 2019-06-20 |
66+
| 6 | 5 | 9 | 2009-02-02 |
67+
| 7 | 5 | 8 | 2010-04-13 |
68+
+---------+---------+----------+-----------------+
69+
70+
output:
71+
+---------+---------------------+
72+
| book_id | name |
73+
+---------+---------------------+
74+
| 1 | "Kalila And Demna" |
75+
| 2 | "28 Letters" |
76+
| 5 | "The Hunger Games" |
77+
+---------+---------------------+
78+
```
79+
80+
<br/>
81+
82+
Refer to **1098-Unpopular-Books.sql** for my solution.

0 commit comments

Comments
 (0)