|
| 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