For each question below, find the approriate SQL query to obtain the information requested. Put all your answers in the solutions.txt
file.
To get started we'll need to import the booktown.sql file.
-
Fork and clone this repository
-
cd
into the repository -
use the command
psql -f booktown.sql
-
type
psql
to open your psql console -
type
\list
to ensure the booktown database was successfully completed -
type
\c booktown
to connect to the booktown database -
type
\d
to see a list of all the tables in the booktown database List of relations -
type
\d [TABLE_NAME]
to see information about columns and their types for a specific table. You should see output like below:
booktown=# \d books
Table "public.books"
Column | Type | Modifiers
------------+---------+-----------
id | integer | not null
title | text | not null
author_id | integer |
subject_id | integer |
Indexes:
"books_id_pkey" PRIMARY KEY, btree (id)
"books_title_idx" btree (title)
Complete the following exercises to practice using SQL.
- Find all subjects sorted by subject SELECT subject FROM subjects ORDER BY subject;
- Find all subjects sorted by location SELECT subject FROM subjects ORDER BY location;
- Find the book "Little Women" SELECT title FROM books WHERE title = 'Little Women';
- Find all books containing the word "Python" SELECT title FROM books WHERE title LIKE '%Python%';
- Find all subjects with the location "Main St" sort them by subject SELECT subject FROM subjects WHERE location = 'Main St' ORDER BY subject;
-
Find all books about Computers and list ONLY the book titles SELECT books.title FROM books JOIN subjects ON books.subject_id = subjects.id WHERE subject = 'Computers';
-
Find all books and display a result table with ONLY the following columns
- Book title
- Author's first name
- Author's last name
- Book subject SELECT books.title, authors.first_name, authors.first_name, subjects.subject FROM books JOIN subjects ON books.subject_id = subjects.id JOIN authors ON books.author_id = authors.id;
-
Find all books that are listed in the stock table
- Sort them by retail price (most expensive first)
- Display ONLY: title and price SELECT books.title, stock.cost FROM editions JOIN books ON editions.book_id = books.id JOIN alternate_stock ON editions.isbn = stock.isbn ORDER BY stock.retail DESC;
-
Find the book "Dune" and display ONLY the following columns
- Book title
- ISBN number
- Publisher name
- Retail price SELECT books.title, editions.isbn, publishers.name, stock.retail FROM editions JOIN books ON editions.book_id = books.id JOIN publishers ON editions.publisher_id = publishers.id JOIN stock ON editions.isbn =stock.isbn WHERE books.title = 'Dune';
-
Find all shipments sorted by ship date display a result table with ONLY the following columns:
- Customer first name
- Customer last name
- ship date
- book title SELECT customers.first_name, customers.last_name, shipments.ship_date, books.title FROM books JOIN editions ON editions.book_id = books.id JOIN shipments ON editions.isbn = shipments.isbn JOIN customers ON shipments.customer_id = customers.id ORDER BY shipments.ship_date;
-
Get the COUNT of all books SELECT COUNT(*) FROM books;
-
Get the COUNT of all Locations SELECT COUNT(location) FROM subjects;
-
Get the COUNT of each unique location in the subjects table. Display the count and the location name. (hint: requires GROUP BY). SELECT location, COUNT(location)AS Frequency FROM subjects GROUP BY location;
-
List all books. Display the book_id, title, and a count of how many editions each book has. (hint: requires GROUP BY and JOIN) SELECT books.id, title, COUNT(editions.book_id)AS Frequency FROM books JOIN editions ON editions.book_id = books.id GROUP BY books.id;
- All content is licensed under a CC-BY-NC-SA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].