From 6cefb10e27f545955fa4bd985c1425eb21454e2f Mon Sep 17 00:00:00 2001 From: yinkus89 Date: Wed, 20 Nov 2024 14:06:18 +0100 Subject: [PATCH] done --- queries.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/queries.md b/queries.md index b06f900..05d6883 100644 --- a/queries.md +++ b/queries.md @@ -1,13 +1,36 @@ ![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) # Answers +CREATE DATABASE library; +CREATE TABLE authors ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + nationality VARCHAR(50), + birth_year DATE +); +CREATE TABLE publishers ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + location VARCHAR(100), + founded_year INT +); +CREATE TABLE books ( + id SERIAL PRIMARY KEY, + title VARCHAR(255) NOT NULL, + author_id INT REFERENCES authors(id), + publisher_id INT REFERENCES publishers(id), + publish_date DATE +); ## Iteration 2 - Joins 1. Using an **INNER JOIN**, list all books (left table) that have an assigned author (right table). The result should include only books with assigned authors. ```sql --- Your Query Goes Here +SELECT books.title, authors.name AS author_name +FROM books +INNER JOIN authors ON books.author_id = authors.id; + ```
@@ -15,7 +38,10 @@ 2. Using a **LEFT JOIN**, list all authors (left table) and their corresponding books on the (right table). The result should include all authors, including those who don't have any books assigned. ```sql --- Your Query Goes Here +SELECT authors.name AS author_name, books.title +FROM authors +LEFT JOIN books ON authors.id = books.author_id; + ```
@@ -23,7 +49,10 @@ 3. Using a **RIGHT JOIN**, list all books (right table) and their corresponding authors on the (left table). The result should include books without assigned authors. ```sql --- Your Query Goes Here +SELECT books.title, authors.name AS author_name +FROM books +RIGHT JOIN authors ON books.author_id = authors.id; + ```
@@ -31,7 +60,10 @@ 4. Using a **FULL JOIN**, list all records from the `books` and `authors` tables. The result should include all details from both tables, even if there are no match. ```sql --- Your Query Goes Here +SELECT books.title, authors.name AS author_name +FROM books +FULL JOIN authors ON books.author_id = authors.id; + ```
@@ -41,7 +73,10 @@ 1. Using an **INNER JOIN**, list all books (left table) and their corresponding publishers on the (right table). The result should include the book's title, publisher's name, and location. ```sql --- Your Query Goes Here +SELECT books.title, publishers.name AS publisher_name, publishers.location +FROM books +INNER JOIN publishers ON books.publisher_id = publishers.id; + ```
@@ -49,7 +84,10 @@ 2. Using a **LEFT JOIN**, list all publishers (left table) and any books they have published on the (right table). The result should include all publishers, including those who haven't published any books. ```sql --- Your Query Goes Here +SELECT publishers.name AS publisher_name, publishers.location, books.title +FROM publishers +LEFT JOIN books ON publishers.id = books.publisher_id; + ```
@@ -57,7 +95,10 @@ 3. Using a **RIGHT JOIN**, list all books (right table) and their corresponding publishers on the (left table). The result should include all books, even those without a linked publisher. ```sql --- Your Query Goes Here +SELECT books.title, publishers.name AS publisher_name, publishers.location +FROM books +RIGHT JOIN publishers ON books.publisher_id = publishers.id; + ```
@@ -65,7 +106,11 @@ 4. Using a **FULL JOIN**, list all records from the `authors`, `books`, and `publishers` tables. The result should include all records from the three tables, even if there are no matches between them. ```sql --- Your Query Goes Here +SELECT books.title, authors.name AS author_name, publishers.name AS publisher_name +FROM books +FULL JOIN authors ON books.author_id = authors.id +FULL JOIN publishers ON books.publisher_id = publishers.id; + ```