You are given two tables: Books and Orders.
The Books table contains information about books with their availability dates. The Orders table contains order information including book quantities and dispatch dates.
Write a SQL query to find 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.
Return the result in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
book_id
PK
|
int | Primary key, unique book identifier |
name
|
varchar | Book name |
available_from
|
date | Date when book became available |
| Column Name | Type | Description |
|---|---|---|
order_id
PK
|
int | Primary key, unique order identifier |
book_id
|
int | Foreign key referencing Books table |
quantity
|
int | Number of copies ordered |
dispatch_date
|
date | Date when order was dispatched |
Input & Output
| book_id | name | available_from |
|---|---|---|
| 1 | Kalila And Demna | 2010-01-01 |
| 2 | 28 Letters | 2019-06-01 |
| 3 | The Hunger Games | 2008-09-01 |
| order_id | book_id | quantity | dispatch_date |
|---|---|---|---|
| 1 | 1 | 2 | 2018-07-26 |
| 2 | 1 | 1 | 2018-11-05 |
| 3 | 3 | 8 | 2019-06-11 |
| 4 | 3 | 39 | 2019-05-07 |
| book_id | name |
|---|---|
| 1 | Kalila And Demna |
Book 1: Available since 2010 (>1 month), sold 3 copies total in last year (2+1) < 10 ✓
Book 2: Available since 2019-06-01, only 22 days before today (2019-06-23) < 1 month ✗
Book 3: Available since 2008 (>1 month), sold 47 copies total in last year (8+39) ≥ 10 ✗
| book_id | name | available_from |
|---|---|---|
| 4 | Zero Sales Book | 2018-01-01 |
| 5 | New Release | 2019-06-20 |
| order_id | book_id | quantity | dispatch_date |
|---|
| book_id | name |
|---|---|
| 4 | Zero Sales Book |
Book 4: Available since 2018 (>1 month), no orders in last year (0 copies) < 10 ✓
Book 5: Available since 2019-06-20, only 3 days before today < 1 month ✗
Constraints
-
1 ≤ book_id ≤ 1000 -
1 ≤ order_id ≤ 1000 -
1 ≤ quantity ≤ 100 -
Today's date is
2019-06-23 -
Last year is defined as
2018-06-23to2019-06-23