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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified task-1/books_library.db
Binary file not shown.
45 changes: 43 additions & 2 deletions task-1/queries.sql

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 effort overall! Most queries are correct but there are a few issues to fix.
Question 1 - Almost correct, but the question asks for oldest first, which means ascending order. You used DESC (newest first).
Question 3 - The logic is correct and good use of table aliases (b and a). However two small issues:
The space between first and last name is missing. You wrote '' (empty string) instead of ' ' (space). So names would appear as "StephenKing" instead of "Stephen King".
The alias is spelled auther_name, should be author_name. Small thing, but attention to detail matters!
Question 8 - The DELETE works, but you hardcoded id IN (101). This assumes you know the exact ID. A safer approach would be:DELETE FROM books WHERE title = 'The Light of life';
Also id IN (101) can just be id = 101 since it's a single value.
Bonus A - Almost correct! The GROUP BY and COUNT are right, but the question asks for most to fewest, which means descending order. Add DESC.

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 DESC;

-- **Question 2** — Show every book published before 1950. Display the title and year only.
SELECT title , published_year
FROM books
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 auther_name
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;

-- **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('Muna', 'Nasher', 'Yemeni', 1999);

SELECT * FROM authors
WHERE last_name= 'Nasher' ;

-- **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)
VALUES ('The Light of life', 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' ;

-- **Question 8** — Delete the book you added in Question 6. Make sure your query targets only that specific row.

SELECT * 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 IN (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_Category
FROM books
GROUP BY genre
ORDER BY book_Category ;
-- **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 a.id=b.author_id
WHERE b.id IS NULL;
Empty file added task-2/data/flashcards.db
Empty file.
Binary file added task-2/flashcards.db
Binary file not shown.
34 changes: 34 additions & 0 deletions task-2/migrate.js
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!');
Loading