diff --git a/README.md b/README.md index 05e2cf7..573bbff 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,6 @@ Here is an example of how you should paste your SQL queries in the `queries.md` Find all technologies in the database: -```sql -# This should be pasted in the queries.md file -SELECT * FROM technologies; -``` -
## Iteration 1 - Database Setup @@ -172,36 +167,81 @@ After you run a query in your `psql` client and get the correct result, remember
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 +select * from books +inner join authors on books.author_id = authors.id +``` +
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 +select * from authors +left join books on authors.id = books.author_id +``` +
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 +select * from authors +right join books on authors.id = books.author_id +``` +
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 +select * from authors +full join books on authors.id = books.author_id +``` +
## BONUS: Iteration 3 - Joins (continued) 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 +select books.title, publishers.name, publishers.location from books +inner join publishers on books.publisher_id = publishers.id +``` +
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 +select * from publishers +left join books on publishers.id = books.publisher_id +``` +
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 +select books.title, publishers.name, publishers.location +from publishers +right join books on publishers.id = books.publisher_id; +``` +
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 +select a.*, b.*, p.* +from authors a +full join books b on a.id = b.author_id +full join publishers p on b.publisher_id = p.id; + +``` +
Happy Coding! 💙 diff --git a/queries.md b/queries.md index b06f900..a011535 100644 --- a/queries.md +++ b/queries.md @@ -4,10 +4,13 @@ ## 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 * from books +inner join authors on books.author_id = authors.id ```
@@ -15,7 +18,8 @@ 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 * from authors +left join books on authors.id = books.author_id ```
@@ -23,7 +27,8 @@ 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 * from authors +right join books on authors.id = books.author_id ```
@@ -31,7 +36,8 @@ 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 * from authors +full join books on authors.id = books.author_id ```
@@ -41,7 +47,8 @@ 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, publishers.location from books +inner join publishers on books.publisher_id = publishers.id ```
@@ -49,7 +56,8 @@ 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 * from publishers +left join books on publishers.id = books.publisher_id ```
@@ -57,7 +65,9 @@ 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, publishers.location +from publishers +right join books on publishers.id = books.publisher_id; ```
@@ -65,7 +75,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 a.*, b.*, p.* +from authors a +full join books b on a.id = b.author_id +full join publishers p on b.publisher_id = p.id; + ```