Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified task-1/books_library.db
Binary file not shown.
468 changes: 468 additions & 0 deletions task-1/package-lock.json

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions task-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "task-1",
"version": "1.0.0",
"description": "For this task use the books_library database. The sqlite db file is there, and if you are having problems with it loading, then use the sql script to create the database.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"better-sqlite3": "^12.8.0"
}
}
45 changes: 43 additions & 2 deletions task-1/queries.sql
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
---- Queries

-- **Question 1** — List the title and published year of every book in the `'Science Fiction'` genre, ordered by published year (oldest first).
SELECT title, published_year
FROM books
WHERE genre = 'Science Fiction'
ORDER BY published_year ASC;

-- **Question 2** — Show every book published before 1950. Display the title and year only.
SELECT title, published_year
FROM books
WHERE published_year < 1950;
ORDER BY published_year ASC;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You finished the query before with a ; and then you have the ORDER BY not linked with the previous line, that would cause an error. And the question doesn't ask for ordering ;)


-- **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
books.title,
authors.first_name || ' ' || authors.last_name AS author
FROM books
JOIN authors ON books.author_id = authors.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
books.title,
authors.first_name || ' ' || authors.last_name AS author

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good written query, but the question asks for title and published year ;)

FROM books
JOIN authors
ON books.author_id = authors.id
WHERE authors.first_name = 'Stephen'
AND authors.last_name = 'King'
ORDER BY books.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 ('Hamed', 'Razizadeh', 'Iranian', 1987);

-- **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, author_id, genre, published_year)
VALUES ('Freedom For IRAN',
(SELECT id FROM authors WHERE first_name = 'Hamed' AND last_name = 'Razizadeh'), 'Documentary', 2026);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 👏 Very good option on choosing SELECT to get the id. Imagine that you a have a big database with a lot of lines. Than this saves time to search the specific 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.
SELECT id FROM books WHERE title = 'The Dark Tower: The Gunslinger';
UPDATE books
SET genre = 'Horror'
WHERE title = 'The Dark Tower: The Gunslinger';
SELECT id, 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
WHERE id = 101;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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?

SELECT * FROM books WHERE id = 101;

-- ### 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 book_count

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 Good choise on using alias here!

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.id, a.first_name, a.last_name
FROM authors a
LEFT JOIN books b ON a.id = b.author_id
WHERE b.id IS NULL;
91 changes: 0 additions & 91 deletions task-2/data/data.json

This file was deleted.

Binary file added task-2/data/flashcards.db
Binary file not shown.
33 changes: 33 additions & 0 deletions task-2/migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Database from "better-sqlite3";
import { readFileSync } from "fs";

const db = new Database("./data/flashcards.db");

const raw = readFileSync("./data/data.json", "utf-8");
const data = JSON.parse(raw);

// insert decks
const insertDeck = db.prepare(
"INSERT INTO decks (id, name, description) VALUES (?, ?, ?)",
);

for (const deck of data.decks) {
insertDeck.run(deck.id, deck.name, deck.description);
}

// insert cards
const insertCard = db.prepare(
"INSERT INTO cards (id, question, answer, learned, deck_id) VALUES (?, ?, ?, ?, ?)",
);

for (const card of data.cards) {
insertCard.run(
card.id,
card.question,
card.answer,
card.learned ? 1 : 0,
card.deckId,
);
}

console.log("Migration complete!");
Loading