-
Notifications
You must be signed in to change notification settings - Fork 18
Jawad A. #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Jawad A. #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,27 +1,86 @@ | ||||||||||
| ---- Queries | ||||||||||
| -- Question 1: | ||||||||||
| -- List the title and published year of every book | ||||||||||
| -- in the 'Science Fiction' genre, ordered by published year (oldest first). | ||||||||||
|
|
||||||||||
| -- **Question 1** — List the title and published year of every book in the `'Science Fiction'` genre, ordered by published year (oldest first). | ||||||||||
| SELECT * FROM books WHERE genre = 'Science Fiction' | ||||||||||
| ORDER BY published_year DESC; | ||||||||||
|
|
||||||||||
| -- **Question 2** — Show every book published before 1950. Display the title and year only. | ||||||||||
|
|
||||||||||
| -- **Question 3** — Show every book in the database along with its author's full name. Combine `first_name` and `last_name` into a single column called `author`. _(Hint: you will need a JOIN.)_ | ||||||||||
| -- Question 2: | ||||||||||
| -- Show every book published before 1950. | ||||||||||
| -- Display the title and year only. | ||||||||||
|
|
||||||||||
| -- **Question 4** — List all books written by Stephen King. Show the title and published year, ordered by year. _(Hint: JOIN the two tables and filter on the author's name.)_ | ||||||||||
| SELECT title, published_year FROM books WHERE published_year < 1950; | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||||||||||
|
|
||||||||||
| -- **Question 5** — Add yourself as a new author. Use your real name, or make one up. Pick any nationality and birth year. | ||||||||||
| -- Question 3: | ||||||||||
| -- Show every book in the database along with its author's full name. | ||||||||||
| -- Combine first_name and last_name into a single column called author. | ||||||||||
|
|
||||||||||
| -- **Question 6** — Add one book for the author you just inserted. It can be a real book or a made-up one. | ||||||||||
| SELECT title, a.first_name || ' ' || a.last_name as author | ||||||||||
| FROM books b | ||||||||||
| INNER JOIN authors a ON b.author_id = a.id; | ||||||||||
|
|
||||||||||
| -- **Question 7** — The genre for "The Dark Tower: The Gunslinger" was entered incorrectly as `'Fantasy'`. It should be `'Horror'`. Write an UPDATE to fix it, then verify the change with a SELECT. | ||||||||||
| -- Question 4: | ||||||||||
| -- List all books written by Stephen King. | ||||||||||
| -- Show the title and published year, ordered by year. | ||||||||||
|
|
||||||||||
| -- **Question 8** — Delete the book you added in Question 6. Make sure your query targets only that specific row. | ||||||||||
| SELECT title, published_year | ||||||||||
| FROM books b | ||||||||||
| INNER JOIN authors a ON b.author_id = a.id | ||||||||||
| WHERE a.last_name = 'King' | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the database contains other authors with the last name of "King"? |
||||||||||
| ORDER BY published_year DESC; | ||||||||||
|
|
||||||||||
| --- | ||||||||||
| -- Question 5: | ||||||||||
| -- Add yourself as a new author. Use your real name, or make one up. | ||||||||||
| -- Pick any nationality and birth year. | ||||||||||
|
|
||||||||||
| -- ### Bonus questions _(optional)_ | ||||||||||
| INSERT INTO authors | ||||||||||
| (id, first_name, last_name, nationality, birth_year) | ||||||||||
| VALUES (25, 'Jawad', 'Al Bdiwi', 'Syrian', '1994'); | ||||||||||
|
Comment on lines
+38
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The authors table has an AUTOINCREMENT clause. It will generate an ID for you. You should not create it yourself, as this may conflict with auto-generated IDs.
Suggested change
|
||||||||||
|
|
||||||||||
| -- These cover topics slightly beyond the core material. Have a go if you finish early. | ||||||||||
| -- ALTERNATIVE SYNTAX | ||||||||||
| -- INSERT INTO authors | ||||||||||
| -- SELECT | ||||||||||
| -- 25 AS id | ||||||||||
| -- , 'Jawad' AS first_name | ||||||||||
| -- , 'Al Bdiwi' AS last_name | ||||||||||
| -- , 'Syrian' AS nationality | ||||||||||
| -- , 1994 AS birth_year; | ||||||||||
|
|
||||||||||
| -- **Bonus A** — How many books are there per genre? Show the genre name and the count, ordered from most to fewest books. | ||||||||||
| -- Question 6: | ||||||||||
| -- Add one book for the author you just inserted. | ||||||||||
| -- It can be a real book or a made-up one. | ||||||||||
|
|
||||||||||
| -- **Bonus B** — Find any authors in the database who have no books at all. _(Hint: you will need a LEFT JOIN and check for NULL.) | ||||||||||
| INSERT INTO books | ||||||||||
| (id, title, published_year genre, author_id) | ||||||||||
| VALUES (101, 'SQL for Beginners', 2026, 'Non-fiction', 25); | ||||||||||
|
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| -- ALTERNATIVE SYNTAX | ||||||||||
| -- INSERT INTO books | ||||||||||
| -- SELECT | ||||||||||
| -- 101 AS id | ||||||||||
| -- , 'SQL for Beginners' AS title | ||||||||||
| -- , 2026 AS published_year | ||||||||||
| -- 'Non-fiction' AS genre | ||||||||||
| -- 25 AS author_id | ||||||||||
|
|
||||||||||
| -- QUESTION 7: | ||||||||||
| -- The genre for "The Dark Tower: The Gunslinger" was entered incorrectly as 'Fantasy'. | ||||||||||
| -- It should be 'Horror'. Write an UPDATE to fix it, | ||||||||||
| -- then verify the change with a SELECT. | ||||||||||
|
|
||||||||||
| UPDATE books | ||||||||||
| SET genre = 'Horror' | ||||||||||
| WHERE title = 'The Dark Tower: The Gunslinger'; | ||||||||||
|
|
||||||||||
| SELECT title, genre | ||||||||||
| FROM books | ||||||||||
| WHERE title = 'The Dark Tower: The Gunslinger'; | ||||||||||
|
|
||||||||||
|
|
||||||||||
| -- Question 8: | ||||||||||
| -- Delete the book you added in Question 6. | ||||||||||
| -- Make sure your query targets only that specific row. | ||||||||||
|
|
||||||||||
| DELETE * FROM books | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no
Suggested change
|
||||||||||
| WHERE id = 101; | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import fs from 'fs'; | ||
| import Database from 'better-sqlite3'; | ||
| import { fileURLToPath } from 'url'; | ||
| import { dirname, join } from 'path'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| const db = new Database(join(__dirname, 'flashcards.db')); | ||
|
|
||
| const data = JSON.parse( | ||
| fs.readFileSync(join(__dirname, 'data', 'data.json'), 'utf-8') | ||
| ); | ||
|
|
||
|
|
||
| for (const deck of data.decks) { | ||
| db.prepare( | ||
| 'INSERT INTO decks (id, name, description) VALUES (?, ?, ?)' | ||
| ).run(deck.id, deck.name, deck.description); | ||
| } | ||
|
|
||
|
|
||
| for (const card of data.cards) { | ||
| db.prepare(` | ||
| INSERT INTO cards (id, question, answer, learned, deck_id) | ||
| VALUES (?, ?, ?, ?, ?) | ||
| `).run( | ||
| card.id, | ||
| card.question, | ||
| card.answer, | ||
| card.learned ? 1 : 0, | ||
| card.deckId | ||
| ); | ||
| } | ||
|
|
||
| console.log('✅ Migration done!'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks perfect. Maybe too perfect to be completely your own work? Note that AI tools love inserting emojis. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| -- SQLite | ||
|
|
||
| CREATE TABLE decks ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| name TEXT NOT NULL, | ||
| description TEXT | ||
| ); | ||
|
|
||
| CREATE TABLE cards ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| question TEXT NOT NULL, | ||
| answer TEXT NOT NULL, | ||
| learned INTEGER DEFAULT 0, | ||
| deck_id INTEGER, | ||
| FOREIGN KEY (deck_id) REFERENCES decks(id) | ||
| ); | ||
|
|
||
| SELECT * FROM decks; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,67 +1,78 @@ | ||
| // database.js | ||
| // Your task: implement each function below using better-sqlite3. | ||
| // The function signatures are identical to storage.js so you can | ||
| // compare the two files side by side. | ||
| // | ||
| // When every function works correctly, `node app.js` should | ||
| // print exactly the same output as it did with storage.js. | ||
|
|
||
| import Database from 'better-sqlite3'; | ||
| import { fileURLToPath } from 'url'; | ||
| import { dirname, join } from 'path'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| const DB_FILE = join(__dirname, '../data/flashcards.db'); | ||
|
|
||
| const db = new Database(DB_FILE); | ||
| const db = new Database(join(__dirname, '../flashcards.db')); | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // Decks | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| export function getAllDecks() { | ||
| // TODO: return all rows from the decks table | ||
| throw new Error('Not implemented'); | ||
| return db.prepare('SELECT * FROM decks').all(); | ||
| } | ||
|
|
||
| export function getDeckById(id) { | ||
| // TODO: return the deck row with the given id, or null if not found | ||
| throw new Error('Not implemented'); | ||
| return db.prepare('SELECT * FROM decks WHERE id = ?').get(id) ?? null; | ||
| } | ||
|
|
||
| export function addDeck(name, description) { | ||
| // TODO: insert a new deck and return the new row (including its id) | ||
| throw new Error('Not implemented'); | ||
| const info = db | ||
| .prepare('INSERT INTO decks (name, description) VALUES (?, ?)') | ||
| .run(name, description); | ||
|
|
||
| return { | ||
| id: info.lastInsertRowid, | ||
| name, | ||
| description, | ||
| }; | ||
| } | ||
|
|
||
| export function deleteDeck(deckId) { | ||
| // TODO: delete the deck with the given id | ||
| // return true if a row was deleted, false otherwise | ||
| throw new Error('Not implemented'); | ||
| const info = db.prepare('DELETE FROM decks WHERE id = ?').run(deckId); | ||
| return info.changes > 0; | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // Cards | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| export function getAllCardsForDeck(deckId) { | ||
| // TODO: return all card rows whose deckId matches | ||
| throw new Error('Not implemented'); | ||
| return db | ||
| .prepare( | ||
| 'SELECT id, question, answer, learned, deck_id AS deckId FROM cards WHERE deck_id = ?' | ||
| ) | ||
| .all(deckId); | ||
| } | ||
|
|
||
| export function addCard(question, answer, deckId) { | ||
| // TODO: insert a new card and return the new row (including its id) | ||
| throw new Error('Not implemented'); | ||
| const info = db | ||
| .prepare( | ||
| 'INSERT INTO cards (question, answer, learned, deck_id) VALUES (?, ?, 0, ?)' | ||
| ) | ||
| .run(question, answer, deckId); | ||
|
|
||
| return { | ||
| id: info.lastInsertRowid, | ||
| question, | ||
| answer, | ||
| learned: 0, | ||
| deckId, | ||
| }; | ||
| } | ||
|
|
||
| export function markCardLearned(cardId) { | ||
| // TODO: set learned = 1 for the card with the given id | ||
| // return the updated row, or null if not found | ||
| throw new Error('Not implemented'); | ||
| db.prepare('UPDATE cards SET learned = 1 WHERE id = ?').run(cardId); | ||
| return db | ||
| .prepare( | ||
| 'SELECT id, question, answer, learned, deck_id AS deckId FROM cards WHERE id = ?' | ||
| ) | ||
| .get(cardId); | ||
| } | ||
|
|
||
| export function deleteCard(cardId) { | ||
| // TODO: delete the card with the given id | ||
| // return true if a row was deleted, false otherwise | ||
| throw new Error('Not implemented'); | ||
| } | ||
| const info = db.prepare('DELETE FROM cards WHERE id = ?').run(cardId); | ||
| return info.changes > 0; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better readability, place the WHERE clause on a separate line.