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
29 changes: 24 additions & 5 deletions task-1/queries.sql
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
---- 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 < 50;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This would only show very old books less than year 50, and that's why this query doesn't return results. Did you try with 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 a.id = b.author_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 a.id = b.author_id
WHERE a.first_name || ' ' || a.last_name = 'Stephen King'

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 one on choosing using concatenation in WHERE!

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 ('Atiqa', 'Naseer', 'Pakistani', 1999);
-- **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 ('Sunset Rose', '2026', 'Romance', 25);

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! 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)) ;)

-- **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';

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


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

---

-- ### Bonus questions _(optional)_
Expand Down
Binary file added task-2/data/flashcards.db
Binary file not shown.
16 changes: 16 additions & 0 deletions task-2/data/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- CREATE NEW TABLES

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 NOT NULL DEFAULT 0,
deck_id INTEGER NOT NULL,
FOREIGN KEY (deck_id) REFERENCES decks(id)
);
50 changes: 50 additions & 0 deletions task-2/migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { readFileSync } from 'fs';
import Database from 'better-sqlite3';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __dirname = dirname(fileURLToPath(import.meta.url));
const DATA_FILE = join(__dirname, 'data/data.json');
const DB_FILE = join(__dirname, 'data/flashcards.db');

const raw = readFileSync(DATA_FILE, 'utf-8');
const data = JSON.parse(raw);

const db = new Database(DB_FILE);
db.pragma('foreign_keys = ON');

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

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

const migrate = db.transaction(() => {
// Clear existing rows so the migration can be re-run safely.
db.prepare('DELETE FROM cards').run();
db.prepare('DELETE FROM decks').run();

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 approach on deleting before!


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

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

migrate();

console.log(
`Migration complete: inserted ${data.decks.length} decks and ${data.cards.length} cards.`
);

db.close();
Loading