File size: 11,964 Bytes
e7cf806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
[
  {
    "id": "q1",
    "title": "List all science fiction books",
    "difficulty": "Beginner",
    "description": "Retrieve the titles and authors of all books in the Science Fiction genre.",
    "hint": "Use a WHERE clause to filter by genre.",
    "expected_sql": "SELECT title, author FROM books WHERE genre = 'Science Fiction';"
  },
  {
    "id": "q2",
    "title": "Find books with low availability",
    "difficulty": "Beginner",
    "description": "Show books with fewer than 2 available copies.",
    "hint": "Use a WHERE clause to filter by available_copies.",
    "expected_sql": "SELECT title, available_copies FROM books WHERE available_copies < 2;"
  },
  {
    "id": "q3",
    "title": "List loans for user ID 101",
    "difficulty": "Beginner",
    "description": "Retrieve all loan details for the user with ID 101.",
    "hint": "Use a WHERE clause to filter by user_id.",
    "expected_sql": "SELECT loan_id, book_id, issue_date, due_date FROM loans WHERE user_id = 101;"
  },
  {
    "id": "q4",
    "title": "Find books published after 2000",
    "difficulty": "Beginner",
    "description": "Show titles and publication years of books published after 2000.",
    "hint": "Use a WHERE clause with year comparison.",
    "expected_sql": "SELECT title, year FROM books WHERE year > 2000;"
  },
  {
    "id": "q5",
    "title": "List unique genres",
    "difficulty": "Beginner",
    "description": "Retrieve all unique genres from the books table.",
    "hint": "Use DISTINCT to avoid duplicate genres.",
    "expected_sql": "SELECT DISTINCT genre FROM books;"
  },
  {
    "id": "q6",
    "title": "Find overdue loans",
    "difficulty": "Beginner",
    "description": "Show loans that are overdue as of '2023-03-01' (due_date before '2023-03-01' and not returned).",
    "hint": "Use a WHERE clause to check due_date and NULL return_date.",
    "expected_sql": "SELECT loan_id, book_id, due_date FROM loans WHERE due_date < '2023-03-01' AND return_date IS NULL;"
  },
  {
    "id": "q7",
    "title": "List users joined after 2020",
    "difficulty": "Beginner",
    "description": "Show names and emails of users who joined after December 31, 2020.",
    "hint": "Use a WHERE clause with membership_date.",
    "expected_sql": "SELECT name, email FROM users WHERE membership_date > '2020-12-31';"
  },
  {
    "id": "q8",
    "title": "Books by George Orwell",
    "difficulty": "Beginner",
    "description": "Retrieve all books written by George Orwell.",
    "hint": "Use a WHERE clause to filter by author.",
    "expected_sql": "SELECT title, genre FROM books WHERE author = 'George Orwell';"
  },
  {
    "id": "q9",
    "title": "List active loans",
    "difficulty": "Beginner",
    "description": "Show all loans that have not been returned.",
    "hint": "Use a WHERE clause to check for NULL return_date.",
    "expected_sql": "SELECT loan_id, book_id, user_id, issue_date FROM loans WHERE return_date IS NULL;"
  },
  {
    "id": "q10",
    "title": "List books by publication year",
    "difficulty": "Beginner",
    "description": "Show all book titles and their publication years, ordered by year ascending.",
    "hint": "Use ORDER BY clause on year.",
    "expected_sql": "SELECT title, year FROM books ORDER BY year ASC;"
  },
  {
    "id": "q11",
    "title": "Count loans per book",
    "difficulty": "Intermediate",
    "description": "Show the number of loans for each book, including books with zero loans.",
    "hint": "Use a LEFT JOIN and GROUP BY book title.",
    "expected_sql": "SELECT b.title, COUNT(l.loan_id) AS loan_count FROM books b LEFT JOIN loans l ON b.book_id = l.book_id GROUP BY b.title;"
  },
  {
    "id": "q12",
    "title": "Total loans per user",
    "difficulty": "Intermediate",
    "description": "Calculate the total number of loans for each user, including users with zero loans.",
    "hint": "Use a LEFT JOIN and GROUP BY user name.",
    "expected_sql": "SELECT u.name, COUNT(l.loan_id) AS total_loans FROM users u LEFT JOIN loans l ON u.user_id = l.user_id GROUP BY u.name;"
  },
  {
    "id": "q13",
    "title": "Books by genre count",
    "difficulty": "Intermediate",
    "description": "Count the number of books in each genre.",
    "hint": "Use GROUP BY on genre and COUNT.",
    "expected_sql": "SELECT genre, COUNT(book_id) AS book_count FROM books GROUP BY genre;"
  },
  {
    "id": "q14",
    "title": "Active loans by genre",
    "difficulty": "Intermediate",
    "description": "Show the number of active loans (not returned) for each book genre.",
    "hint": "Join books and loans, filter by NULL return_date, and GROUP BY genre.",
    "expected_sql": "SELECT b.genre, COUNT(l.loan_id) AS active_loans FROM books b JOIN loans l ON b.book_id = l.book_id WHERE l.return_date IS NULL GROUP BY b.genre;"
  },
  {
    "id": "q15",
    "title": "Users with multiple loans",
    "difficulty": "Intermediate",
    "description": "Find users who have taken out more than one loan.",
    "hint": "Use GROUP BY on user name and HAVING clause.",
    "expected_sql": "SELECT u.name, COUNT(l.loan_id) AS loan_count FROM users u JOIN loans l ON u.user_id = l.user_id GROUP BY u.name HAVING COUNT(l.loan_id) > 1;"
  },
  {
    "id": "q16",
    "title": "Books never loaned",
    "difficulty": "Intermediate",
    "description": "List books that have never been loaned.",
    "hint": "Use a LEFT JOIN and check for NULL in the loans table.",
    "expected_sql": "SELECT b.title FROM books b LEFT JOIN loans l ON b.book_id = l.book_id WHERE l.loan_id IS NULL;"
  },
  {
    "id": "q17",
    "title": "Loan details with book and user",
    "difficulty": "Intermediate",
    "description": "Show loan details including book title, user name, and issue date for all loans.",
    "hint": "Join books, users, and loans tables.",
    "expected_sql": "SELECT l.loan_id, b.title, u.name, l.issue_date FROM loans l JOIN books b ON l.book_id = b.book_id JOIN users u ON l.user_id = u.user_id;"
  },
  {
    "id": "q18",
    "title": "Books loaned in 2022",
    "difficulty": "Intermediate",
    "description": "List all books that were loaned in 2022.",
    "hint": "Use a WHERE clause with LIKE on issue_date and JOIN with books.",
    "expected_sql": "SELECT DISTINCT b.title FROM books b JOIN loans l ON b.book_id = l.book_id WHERE l.issue_date LIKE '2022%';"
  },
  {
    "id": "q19",
    "title": "Average loan duration for returned books",
    "difficulty": "Intermediate",
    "description": "Calculate the average number of days for loans that have been returned.",
    "hint": "Use JULIANDAY to calculate date difference and AVG.",
    "expected_sql": "SELECT AVG(JULIANDAY(return_date) - JULIANDAY(issue_date)) AS avg_loan_days FROM loans WHERE return_date IS NOT NULL;"
  },
  {
    "id": "q20",
    "title": "Most popular author by loans",
    "difficulty": "Intermediate",
    "description": "Find the author with the most loans.",
    "hint": "Join books and loans, GROUP BY author, and use LIMIT.",
    "expected_sql": "SELECT b.author, COUNT(l.loan_id) AS loan_count FROM books b JOIN loans l ON b.book_id = l.book_id GROUP BY b.author ORDER BY loan_count DESC LIMIT 1;"
  },
  {
    "id": "q21",
    "title": "Overdue loans with user details",
    "difficulty": "Advanced",
    "description": "Show user names, book titles, and days overdue for loans not returned by '2023-03-01'.",
    "hint": "Join tables, filter for overdue loans, and calculate days overdue.",
    "expected_sql": "SELECT u.name, b.title, ROUND(JULIANDAY('2023-03-01') - JULIANDAY(l.due_date), 1) AS days_overdue FROM loans l JOIN books b ON l.book_id = b.book_id JOIN users u ON l.user_id = u.user_id WHERE l.due_date < '2023-03-01' AND l.return_date IS NULL;"
  },
  {
    "id": "q22",
    "title": "Most active user by loan duration",
    "difficulty": "Advanced",
    "description": "Find the user with the highest total loan duration for returned books.",
    "hint": "Join users and loans, calculate duration, GROUP BY user, and use LIMIT.",
    "expected_sql": "SELECT u.name, SUM(JULIANDAY(l.return_date) - JULIANDAY(l.issue_date)) AS total_loan_days FROM users u JOIN loans l ON u.user_id = l.user_id WHERE l.return_date IS NOT NULL GROUP BY u.name ORDER BY total_loan_days DESC LIMIT 1;"
  },
  {
    "id": "q23",
    "title": "Books with high demand",
    "difficulty": "Advanced",
    "description": "Identify books with more loans than their available copies.",
    "hint": "Join books and loans, GROUP BY book, and use HAVING clause.",
    "expected_sql": "SELECT b.title, COUNT(l.loan_id) AS loan_count, b.available_copies FROM books b JOIN loans l ON b.book_id = l.book_id GROUP BY b.title, b.available_copies HAVING COUNT(l.loan_id) > b.available_copies;"
  },
  {
    "id": "q24",
    "title": "Longest overdue loan",
    "difficulty": "Advanced",
    "description": "Find the loan with the longest overdue period as of '2023-03-01'.",
    "hint": "Calculate days overdue, filter for overdue loans, and use LIMIT.",
    "expected_sql": "SELECT l.loan_id, b.title, ROUND(JULIANDAY('2023-03-01') - JULIANDAY(l.due_date), 1) AS days_overdue FROM loans l JOIN books b ON l.book_id = b.book_id WHERE l.return_date IS NULL AND l.due_date < '2023-03-01' ORDER BY days_overdue DESC LIMIT 1;"
  },
  {
    "id": "q25",
    "title": "Loan activity by month",
    "difficulty": "Advanced",
    "description": "Show the number of loans issued per month in 2022 and 2023.",
    "hint": "Use STRFTIME to extract the month and GROUP BY.",
    "expected_sql": "SELECT STRFTIME('%Y-%m', issue_date) AS month, COUNT(loan_id) AS loan_count FROM loans GROUP BY month;"
  },
  {
    "id": "q26",
    "title": "Users with no returns",
    "difficulty": "Advanced",
    "description": "List users who have loans but no returned books.",
    "hint": "Use JOIN and check for NULL return_date with HAVING clause.",
    "expected_sql": "SELECT u.name FROM users u JOIN loans l ON u.user_id = l.user_id GROUP BY u.name HAVING COUNT(l.return_date) = 0;"
  },
  {
    "id": "q27",
    "title": "Books loaned by multiple users",
    "difficulty": "Advanced",
    "description": "Find books that have been loaned by more than one distinct user.",
    "hint": "Use COUNT and DISTINCT on user_id, GROUP BY book title.",
    "expected_sql": "SELECT b.title, COUNT(DISTINCT l.user_id) AS user_count FROM books b JOIN loans l ON b.book_id = l.book_id GROUP BY b.title HAVING COUNT(DISTINCT l.user_id) > 1;"
  },
  {
    "id": "q28",
    "title": "Average loan duration by genre",
    "difficulty": "Advanced",
    "description": "Calculate the average loan duration for returned books by genre.",
    "hint": "Join books and loans, calculate duration, and GROUP BY genre.",
    "expected_sql": "SELECT b.genre, AVG(JULIANDAY(l.return_date) - JULIANDAY(l.issue_date)) AS avg_loan_days FROM books b JOIN loans l ON b.book_id = l.book_id WHERE l.return_date IS NOT NULL GROUP BY b.genre;"
  },
  {
    "id": "q29",
    "title": "Users borrowing all science fiction books",
    "difficulty": "Advanced",
    "description": "List users who have borrowed every book in the Science Fiction genre.",
    "hint": "Count distinct Science Fiction books per user and compare with total Science Fiction books.",
    "expected_sql": "SELECT u.name FROM users u JOIN loans l ON u.user_id = l.user_id JOIN books b ON l.book_id = b.book_id WHERE b.genre = 'Science Fiction' GROUP BY u.name HAVING COUNT(DISTINCT b.book_id) = (SELECT COUNT(*) FROM books WHERE genre = 'Science Fiction');"
  },
  {
    "id": "q30",
    "title": "Earliest and latest loan per book",
    "difficulty": "Advanced",
    "description": "Show the earliest and latest issue dates for each book that has been loaned.",
    "hint": "Use MIN and MAX on issue_date, GROUP BY book title.",
    "expected_sql": "SELECT b.title, MIN(l.issue_date) AS earliest_loan, MAX(l.issue_date) AS latest_loan FROM books b JOIN loans l ON b.book_id = l.book_id GROUP BY b.title;"
  }
]