-
Notifications
You must be signed in to change notification settings - Fork 18
Diana C. #1
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?
Diana C. #1
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,63 @@ | ||
| ---- Queries | ||
|
|
||
| -- **Question 1** — List the title and published year of every book in the `'Science Fiction'` genre, ordered by published year (oldest first). | ||
|
|
||
| -- **Question 2** — Show every book published before 1950. Display the title and year only. | ||
| --Question 1** — List the title and published year of every book in the `'Science Fiction'` genre, ordered by published year (oldest first). | ||
| SELECT books.title, books.published_year | ||
| FROM books | ||
| WHERE genre = 'Science Fiction' | ||
| ORDER BY books.published_year ASC; | ||
|
|
||
| -- **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. | ||
| SELECT books.title, books.published_year FROM books | ||
| WHERE published_year < 1950; | ||
|
|
||
| -- **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.)_ | ||
|
|
||
| -- **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`. *(Hint: you will need a JOIN.)* | ||
| SELECT b.title, a.first_name || ' ' || a.last_name AS author | ||
| FROM books b | ||
| JOIN authors a ON a.id = b.author_id | ||
|
|
||
| -- **Question 6** — Add one book for the author you just inserted. It can be a real book or a made-up one. | ||
|
|
||
| -- **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. *(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 a.id = b.author_id | ||
| WHERE a.first_name = 'Stephen' AND a.last_name = 'King' | ||
| ORDER BY b.published_year; | ||
|
|
||
| -- **Question 8** — Delete the book you added in Question 6. Make sure your query targets only that specific row. | ||
| --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 ('Diana', 'Chukhrai', 'Ukrainian', 2001); | ||
|
|
||
|
|
||
| --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 ('Spaces',2005, 'Fantasy', 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 title = 'The Dark Tower: The Gunslinger'; | ||
|
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. Good one here! Adding exactly the title for UPDATE or DELETE and avoiding using LIKE makes you changing one what is needed, without the risk of updating or deleting not wanted rows |
||
|
|
||
|
|
||
|
|
||
| --Question 8** — Delete the book you added in Question 6. Make sure your query targets only that specific row. | ||
| DELETE FROM books | ||
| WHERE books.id = 101; | ||
|
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. How would write this if you don't know the id to be deleted? Just a thing to consider if you have a database with many rows ;) |
||
| --- | ||
|
|
||
| -- ### 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. | ||
|
|
||
| -- **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.) | ||
| --**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 book_count | ||
|
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. Well done in using an ALIAS! Makes more clear and logic to use in code later! |
||
| FROM books | ||
| GROUP BY genre | ||
| ORDER BY book_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 b ON b.author_id = a.id | ||
| WHERE b.id IS NULL; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import fs from "node:fs"; | ||
| import path from "path"; | ||
| import Database from "better-sqlite3"; | ||
|
|
||
| const dataPath = path.join("data", "data.json"); | ||
| const data = fs.readFileSync(dataPath, "utf-8"); | ||
| const res = JSON.parse(data); | ||
|
|
||
| // Open the database file. | ||
| // If the file doesn't exist, better-sqlite3 will create it. | ||
| const db = new Database("data/flashcards.db"); | ||
|
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. Very solid and direct implementation of the migration! 👏 |
||
|
|
||
| const insertDecks = db.prepare(` | ||
| INSERT INTO decks (name, description) | ||
| VALUES (@name, @description) | ||
| `); | ||
|
|
||
| for (const deck of res.decks) { | ||
| insertDecks.run({ | ||
| name: deck.name, | ||
| description: deck.description, | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| const insertCards = db.prepare(` | ||
| INSERT INTO cards (question, answer, learned, deck_id) | ||
| VALUES (@question, @answer, @learned, @deck_id) | ||
| `); | ||
|
|
||
| for (const card of res.cards) { | ||
| insertCards.run({ | ||
| question: card.question, | ||
| answer: card.answer, | ||
| learned: card.learned ? 1 : 0, | ||
| deck_id: card.deckId | ||
|
|
||
| }); | ||
| } | ||
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.
Good! A challenge: there is a way of doing a SELECT to find the author_id. How would you do that? Tip: I can add doing something like ...VALUES('Rafael', 'Pereira', (SELECT id FROM customers)) ;)