-
Notifications
You must be signed in to change notification settings - Fork 18
Dagim H. #4
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
Closed
Closed
Dagim H. #4
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "c55-core-week-12", | ||
| "version": "1.0.0", | ||
| "description": "The week 12 assignment for the HackYourFuture Core program can be found at the following link: https://hub.hackyourfuture.nl/core-program-week-12-assignment", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/Unlock7/c55-core-week-12.git" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "module", | ||
| "bugs": { | ||
| "url": "https://github.com/Unlock7/c55-core-week-12/issues" | ||
| }, | ||
| "homepage": "https://github.com/Unlock7/c55-core-week-12#readme" | ||
| } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,67 @@ | ||
| ---- Queries | ||
|
|
||
| -- **Question 1** — List the title and published year of every book in the `'Science Fiction'` genre, ordered by published year (oldest first). | ||
| SELECT b.title, b.published_year | ||
| FROM books b | ||
| WHERE genre = 'Science Fiction' | ||
| ORDER BY published_year ASC; | ||
|
|
||
| -- **Question 2** — Show every book published before 1950. Display the title and year only. | ||
|
|
||
| SELECT b.title, b.published_year | ||
| FROM books b | ||
| WHERE published_year < 1950; | ||
| -- **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.)_ | ||
|
|
||
| SELECT | ||
| b.title, | ||
| a.first_name || ' ' || a.last_name AS author | ||
| FROM books b | ||
| JOIN authors a | ||
| ON b.author_id = a.id; | ||
| -- **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 b.title, b.published_year | ||
| FROM books b | ||
| JOIN authors a | ||
| ON b.author_id = a.id | ||
| WHERE a.first_name = 'Stephen' | ||
| AND a.last_name = 'King' | ||
| ORDER BY b.published_year ASC; | ||
| -- **Question 5** — Add yourself as a new author. Use your real name, or make one up. Pick any nationality and birth year. | ||
|
|
||
| INSERT INTO authors( | ||
| first_name, last_name, nationality, birth_year) | ||
| VALUES ('Haddis', 'Alemayehu', 'Ethiopian', 1910); | ||
| -- **Question 6** — Add one book for the author you just inserted. It can be a real book or a made-up one. | ||
| INSERT INTO books( | ||
| title, published_year, genre, author_id) | ||
| VALUES('Fikir Eske Mekabir', 1968, 'Tragedy/Romance', 25); | ||
|
|
||
| -- **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 books.title = 'The Dark Tower: The Gunslinger'; | ||
| --verifying the change! | ||
| SELECT b.genre | ||
| FROM books b | ||
| WHERE b.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 | ||
| WHERE books.title = 'Fikir Eske Mekabir' | ||
| AND books.author_id = 25 | ||
| AND books.published_year = 1968; | ||
| --- | ||
|
|
||
| -- ### Bonus questions _(optional)_ | ||
|
|
||
| -- These cover topics slightly beyond the core material. Have a go if you finish early. | ||
|
|
||
| -- **Bonus A** — How many books are there per genre? Show the genre name and the count, ordered from most to fewest books. | ||
|
|
||
| SELECT genre, COUNT(*) AS books_count | ||
| FROM books | ||
| GROUP BY genre | ||
| ORDER BY count(*) DESC; | ||
| -- **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.) | ||
| SELECT a.first_name, a.last_name | ||
| FROM authors a | ||
| LEFT JOIN books AS b | ||
| ON a.id = b.author_id | ||
| WHERE b.id IS NULL; | ||
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import fs from 'fs'; | ||
| import Database from 'better-sqlite3'; | ||
|
|
||
| // Read data/data.json | ||
| const raw = fs.readFileSync('./data/data.json', 'utf8'); | ||
| const data = JSON.parse(raw); | ||
|
|
||
| // Open data/flashcards.db | ||
| const db = new Database('./data/flashcards.db'); | ||
|
|
||
| // Prepare insert statements | ||
| const insertDeck = db.prepare(` | ||
| INSERT INTO decks (name, description) | ||
| VALUES (?, ?) | ||
| `); | ||
|
|
||
| const insertCard = db.prepare(` | ||
| INSERT INTO cards (question, answer, learned, deck_id) | ||
|
Unlock7 marked this conversation as resolved.
|
||
| VALUES (?, ?, ?, ?) | ||
| `); | ||
|
|
||
| //Insert decks | ||
| const deckIdMap = {}; | ||
|
|
||
| for (const deck of data.decks) { | ||
| const result = insertDeck.run(deck.name, deck.description); | ||
| deckIdMap[deck.id] = result.lastInsertRowid; | ||
| } | ||
|
|
||
| //Insert cards | ||
| for (const card of data.cards) { | ||
| insertCard.run(card.question, card.answer, card.learned ? 1 : 0, deckIdMap[card.deckId]); | ||
| } | ||
|
|
||
| console.log('Migration complete!'); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Question 6 hard-codes
author_id = 25, which will only work if the newly inserted author happens to get id 25. To reliably link the new book to the author inserted in Question 5, derive the id via a subquery (e.g., lookup by the inserted author’s name) instead of relying on a fixed id.