HW02: Intro SQL SELECT due Tue 20 Jan 23:59

\begin{purpose}
You will gain experience in installing a database and in interacting with a
database using SQL.
\end{purpose}

Allowed and Disallowed Resources

In completing this assignment you MAY use/access the following resources:

You may NOT use/access anything in the universe that is not listed above, including but not limited to these prohibitions:

If it is clear to the instructor that you have utilized prohibited resources you will be assigned a grade of 0 on the assignment and given the opportunity to rework the assignment. The due date remains unchanged and late work penalties will be enforced.

Before You Start

Quick reminder that before you start each assignment you should make sure your local workspace is properly synced with your homework repo and the upstream repo:

Overview

In this assignment you will install a database that will be used for several assignment. Then you interact with that database using SQL commands.

Part 1: Install Database (4 pts)

Make sure that docker is running on your system and that the database environment is ready to work with. Then follow these steps:
  1. Visit: localhost:5050 and log into the pgAdmin portal.
  2. From the pgAdmin dashboard, click the “Add New Server” and specify the following values:
    • Name: Homework
    • Host name/address: db
    • Username: student
    • Password: devpass
  3. Click Save to add the Homework server.
  4. In the left panel, expand Servers -> Homework -> Databases and you should see mydb as one of the database that was created when you launched the docker container.
  5. Follow these steps to install the booksdb.sql file:
    • In the terminal, navigate to the examples/sql directory in your workspace. In that directory you should have a file named booksdb.sql that has a list of SQL commands that can be issued to generate the database in a schema (i.e., namespace) called booksdb.
    • From the command-line in the examples/sql directory, issue this command (NOTE: Use runsql.sh booksdb.sql if you are on Mac/Linux/Git Bash in Windows):
      runsql.ps1 booksdb.sql
      
    • The command should finish after several seconds and you should be able to find the new schema in the left panel under mydb -> Schemas. You may need to refresh the view by right clicking on the Schemas folder and clicking Refresh.
    • NOTE: There are also two scripts (runsql.ps1 for Windows and runsql.sh for Mac/Linux) that can be used for homework assignments moving forward.
  6. Right-click on the booksdb schema in the left panel and choose Query Tool. This will provide a window in which you can type SQL commands.
  7. Begin by setting the search path to the new schema:
    SET SEARCH_PATH TO booksdb;
    
    Then click the run/play icon to execute the command.
  8. To verify everything is ready for the homework, add this command following the SET SEARCH_PATH command:
    SELECT * FROM publisher;
    
    You should see a list of publishers.
  9. Next install the albumdb.sql database using the commands above. You do not need to create a new server. Instead just repeat the docker exec command with this file.

Part 2: Write SQL SELECT Queries (20 pts)

Using the newly installed booksdb| database, write SQL queries to perform the following tasks. The queries should be saved in the hw02 directory of your repository and should stored in a file named select.sql.

Above each query (as a SQL comment) include the problem number along with the number of rows displayed by the query. So, your file will look something like this:

-- 1. 23 rows
SELECT ...

-- 2. 1720 rows
SELECT ...

...

-- 8. 1 row
SELECT ...

Each query is worth 2 points plus 1/2 a point for giving the correct number of rows displayed in the comment section. Unless stated otherwise, each query should display all fields of the table(s) in question. Write a query that will:

  1. Produce a list of all info about all books (including author name, publisher name, etc.) using an INNER JOIN with the USING connector.
  2. Repeat the query from problem 1 using an INNER JOIN with the NATURAL connector.
  3. Repeat the query from problem 1 using an INNER JOIN with the ON connector.
  4. Produce a list of books (title and year only) that were published before 1980.
  5. Produce a list of books (title, publisher name) whose publication year is not known.
  6. Produce a list of books (title, author last, author first) that are between 100 and 200 pages (inclusive).
  7. Produce a list of authors whose first name is either unknown or contains only one letter.
  8. Produce a list of authors whose first name or last name contains this sequence of characters: 'evil'. (Do not include in your match the single quotes or the period!)

Turn in your work by making sure all your work appears in the homework directory in your workspace for this assignment. Then add/commit/push your work to your github homework repo.