#453 Books and Their Authors

Companies:
Tochka Bank
Themes:
SQLJoinsPractice

Display the titles of all books and the surnames of their authors (title, surname):

-- Create the authors table
CREATE TABLE authors (
    id INT PRIMARY KEY AUTO_INCREMENT,
    surname VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    patronymic VARCHAR(255) NOT NULL,
    birth_date DATE,
    country VARCHAR(100),
    biography TEXT
);

-- Create the books table
CREATE TABLE books (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    author_id INT NOT NULL,
    publish_date DATE,
    isbn VARCHAR(20) UNIQUE,
    pages INT,
    FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE
);

Additional exercise: remove NOT NULL from author_id and remove FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE. How can all books and authors now be displayed, including books without an author?

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in