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
32 changes: 32 additions & 0 deletions task-1/queries.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
---- 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;


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The required terminating semicolon is missing.

Suggested change
JOIN books b ON a.id = b.author_id
JOIN books b 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 a.first_name || ' ' || a.last_name AS author, b.title, b.published_year FROM authors a
JOIN books b ON a.id = b.author_id
WHERE author = 'Stephen King' ORDER BY published_year DESC;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For better readability, place the ORDER BY clause on a new line.

Suggested change
WHERE author = 'Stephen King' ORDER BY published_year DESC;
WHERE author = 'Stephen King'
ORDER BY published_year DESC;

Note that Question 4 does not mention that the author's name should be shown. So the following SQL may be more appropriate:

SELECT b.title, b.published_year FROM authors a
JOIN books b ON a.id = b.author_id 
WHERE a.first_name = "Stephen" AND a.last_name = "King" 
ORDER BY published_year DESC;


-- **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
('Shadi', 'Abedinpour', 'Iranian', 1995);

-- **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
('The Story of My Cats', 2023, 'Biography',
(SELECT id FROM authors WHERE first_name = 'Shadi' AND last_name = 'Abedinpour'));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Excellent use of a SQL subquery.


-- **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';
SELECT * FROM books WHERE title = 'The Dark Tower: The Gunslinger';
Comment on lines +38 to +39

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use consistent indentation:

Suggested change
WHERE title = 'The Dark Tower: The Gunslinger';
SELECT * FROM books WHERE title = 'The Dark Tower: The Gunslinger';
WHERE title = 'The Dark Tower: The Gunslinger';
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 = 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.

  1. Rather than hard-coding the id, you could use a subquery here too.
  2. The required terminating semicolon is missing.
Suggested change
WHERE id = 101
WHERE id = 101;


---

Expand All @@ -23,5 +49,11 @@
-- 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 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 AS author FROM authors a
LEFT JOIN books b ON a.id = b.author_id
WHERE b.id IS NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The full name is not required. So this alternative would also be fine:

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;

Binary file added task-2/data/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 sqlite3 from "sqlite3";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unfortunately, this code doesn't work at all. The sqlite3 npm package is deprecated, see: https://www.npmjs.com/package/sqlite3. The db.run() function in this deprecated package is asynchronous and consequently produces this error:

[Error: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed
Emitted 'error' event on Statement instance at:
] {
  errno: 19,
  code: 'SQLITE_CONSTRAINT'
}

This makes me wonder how you managed to create a populated flashcards.db file, with migrate.js being defective.


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

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

db.run("PRAGMA foreign_keys = ON");

data.decks.forEach((deck) => {
db.run(
`INSERT INTO decks (id, name, description) VALUES (?, ?, ?)`,
[deck.id, deck.name, deck.description]
);
});


data.cards.forEach((card) => {
db.run(
`INSERT INTO cards (id, question, answer, learned, deck_id)
VALUES (?, ?, ?, ?, ?)`,
[
card.id,
card.question,
card.answer,
card.learned ? 1 : 0,
card.deckId
]
);
});

db.close();
Loading