Commit 0bf4dfa2 authored by michael lundquist's avatar michael lundquist
Browse files

Adding homeworks from weeks 3 and 4.

parent baa103fa
# Problem 1
Identify two related tables in the JustLee Books database.
Identify the common field between the two tables. Decide which columns
you would like to display in the output. Write a SQL code to join
identified tables using the WHERE statement. Make sure to include
qualifiers for columns that appear in both tables. Since only two
tables are joined, make sure to include one join condition in the WHERE
statement. Explain what the query is intended to do.
```sql
SELECT NAME, TITLE, ISBN FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID ORDER BY P.PUBID;
```
![ANSWER PIC](./PICS/PROBLEM1.PNG)
# Problem 2
Repeat problem 1 but remove the WHERE statement. What happened? Why?
```sql
SELECT NAME, TITLE, ISBN FROM BOOKS B, PUBLISHER P;
```
Without a where clause preventing it, the select statement turned into a cartesian join. This selects all combinations of all tables involved.
![ANSWER PIC](./PICS/PROBLEM2A.PNG)
![ANSWER PIC](./PICS/PROBLEM2B.PNG)
![ANSWER PIC](./PICS/PROBLEM2C.PNG)
# Problem 3
Repeat problem 1 using the JOIN … USING keywords.
```sql
SELECT NAME, TITLE, ISBN FROM BOOKS join PUBLISHER USING (PUBID);
```
![ANSWER PIC](./PICS/PROBLEM3.PNG)
# Problem 4
Repeat problem 1 using the JOIN … ON keywords.
```sql
SELECT NAME, TITLE, ISBN FROM BOOKS B join PUBLISHER P ON P.PUBID = B.PUBID;
```
![ANSWER PIC](./PICS/PROBLEM4.PNG)
# Problem 5
Repeat problem 1 but add a condition in the WHERE statement. Use logical operators to combine multiple conditions. Explain what the query is intended to do.
```sql
SELECT NAME, TITLE, ISBN, COST FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID and B.COST BETWEEN 20 AND 30 ORDER BY P.PUBID;
```
This query finds books that cost between 20 and 30$ and their publisher.
![ANSWER PIC](./PICS/PROBLEM5.PNG)
# Problem 6
Repeat problem 1 but add two more conditions in the WHERE statement. Use
logical operators to combine multiple conditions. Explain what the query
is intended to do.
```sql
SELECT NAME, TITLE, ISBN, COST, B.PUBDATE FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID and B.COST BETWEEN 10 AND 30 AND B.PUBDATE > '05-jan-05' ORDER BY P.PUBID;
```
[//]: # (note, these must be single qoutes)
This query finds books that cost between 10 and 30$ that were published after jan 5, 2010 along with their publisher
![ANSWER PIC](./PICS/problem6.PNG)
# Problem 7
Identify three related tables in the JustLee Books database. Identify
common fields between the tables. Decide which columns you would like to display in the output. Write a SQL code to join identified tables using the WHERE statement. Make sure to include qualifiers for columns that appear in multiple tables. Since three tables are joined, make sure to include two join conditions in the WHERE statement. Explain what the query is intended to do.
```sql
select A.LNAME, A.FNAME, B.TITLE FROM AUTHOR A, BOOKS B, BOOKAUTHOR BA WHERE BA.ISBN = B.ISBN AND A.AUTHORID = BA.AUTHORID;
```
This query finds the author and title for each book
![ANSWER PIC](./PICS/PROBLEM7.PNG)
# Problem 8
Repeat problem 7 using the JOIN … USING keywords and add two conditions.
Explain what the query is intended to do.
```sql
SELECT A.LNAME, A.FNAME, B.TITLE FROM BOOKAUTHOR BA JOIN BOOKS B USING (ISBN) JOIN AUTHOR A USING (AUTHORID) order by A.FNAME, A.LNAME;
```
This query finds the author and title of each book using join ... using.
![ANSWER PIC](./PICS/PROBLEM8.PNG)
# Problem 9
Repeat problem 7 using the JOIN … ON keywords and add two conditions.
Explain what the query is intended to do.
```sql
SELECT A.LNAME, A.FNAME, B.TITLE, B.COST FROM BOOKAUTHOR BA JOIN BOOKS B USING (ISBN) JOIN AUTHOR A USING (AUTHORID) where A.FNAME = 'JACK' AND A.LNAME = 'BAKER';
```
Finds the books Jack Baker wrote and their cost
![ANSWER PIC](./PICS/PROBLEM9.PNG)
# Problem 10
Use set operators UNION, UNION ALL, INTERSECT, and MINUS to combine the
results of two queries. Make sure the column list in both queries is
consistent. Explain the result.
```sql
SELECT TITLE, COST FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID AND P.PUBID = 2
INTERSECT
SELECT TITLE, COST FROM BOOKS WHERE ROWNUM <= 5 ORDER BY COST;
```
Finds books that are in the top 5 most expensive books sold and are published by publish our way.
![ANSWER PIC](./PICS/PROBLEM10.PNG)
# Problem 11
Identify a table in the JustLee Books database where a self-join can be
used and write a corresponding SQL query. Explain what the query is
intended to do.
```sql
select r.firstname, r.lastname, c.lastname "Referred" from customers c, customers r WHERE c.referred = r.customer#;
```
Tells which customers were referred and who referred them.
![ANSWER PIC](./PICS/PROBLEM11.PNG)
\ No newline at end of file
/**
# Problem 1
Identify two related tables in the JustLee Books database.
Identify the common field between the two tables. Decide which columns
you would like to display in the output. Write a SQL code to join
identified tables using the WHERE statement. Make sure to include
qualifiers for columns that appear in both tables. Since only two
tables are joined, make sure to include one join condition in the WHERE
statement. Explain what the query is intended to do.
*/
SELECT NAME, TITLE, ISBN FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID ORDER BY P.PUBID;
/**
# Problem 2
Repeat problem 1 but remove the WHERE statement. What happened? Why?
*/
SELECT NAME, TITLE, ISBN FROM BOOKS B, PUBLISHER P;
--cartesian join
/**
# Problem 3
Repeat problem 1 using the JOIN … USING keywords.
*/
SELECT NAME, TITLE, ISBN FROM BOOKS join PUBLISHER USING (PUBID);
/**
# Problem 4
Repeat problem 1 using the JOIN … ON keywords.
*/
SELECT NAME, TITLE, ISBN FROM BOOKS B join PUBLISHER P ON P.PUBID = B.PUBID;
/**
# Problem 5
Repeat problem 1 but add a condition in the WHERE statement. Use logical operators to combine multiple conditions. Explain what the query is intended to do.
*/
SELECT NAME, TITLE, ISBN, COST FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID and B.COST BETWEEN 10 AND 30 ORDER BY P.PUBID;
-- FIND BOOKS AND THEIR PUBLISHER THAT COST BETWEEN 10$ AND 30$
/**
# Problem 6
Repeat problem 1 but add two more conditions in the WHERE statement. Use
logical operators to combine multiple conditions. Explain what the query
is intended to do.
*/
SELECT NAME, TITLE, ISBN, COST, B.PUBDATE FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID and B.COST BETWEEN 10 AND 30 AND B.PUBDATE > '05-jan-05' ORDER BY P.PUBID;
--note, these must be single qoutes
-- FIND BOOKS AND THEIR PUBLISHER THAT COST BETWEEN 10$ AND 30$ AND WERE PUBLISHED AFTER JAN 5, 2010
/**
# Problem 7
Identify three related tables in the JustLee Books database. Identify
common fields between the tables. Decide which columns you would like to display in the output. Write a SQL code to join identified tables using the WHERE statement. Make sure to include qualifiers for columns that appear in multiple tables. Since three tables are joined, make sure to include two join conditions in the WHERE statement. Explain what the query is intended to do.
*/
select A.LNAME, A.FNAME, B.TITLE FROM AUTHOR A, BOOKS B, BOOKAUTHOR BA WHERE BA.ISBN = B.ISBN AND A.AUTHORID = BA.AUTHORID;
--FINDS THE AUTHOR AND TITLE OF A BOOK
/**
# Problem 8
Repeat problem 7 using the JOIN … USING keywords and add two conditions.
Explain what the query is intended to do.
*/
SELECT A.LNAME, A.FNAME, B.TITLE FROM BOOKAUTHOR BA JOIN BOOKS B USING (ISBN) JOIN AUTHOR A USING (AUTHORID) order by A.FNAME, A.LNAME;
--FINDS THE AUTHOR AND TITLE OF A BOOK
/**
# Problem 9
Repeat problem 7 using the JOIN … ON keywords and add two conditions.
Explain what the query is intended to do.
*/
SELECT A.LNAME, A.FNAME, B.TITLE, B.COST FROM BOOKAUTHOR BA JOIN BOOKS B USING (ISBN) JOIN AUTHOR A USING (AUTHORID) where A.FNAME = 'JACK' AND A.LNAME = 'BAKER';
--FINDS THE BOOKE JACK BAKER WROTE AND THEIR COST
/**
# Problem 10
Use set operators UNION, INION ALL, INTERSECT, and MINUS to combine the
results of two queries. Make sure the column list in both queries is
consistent. Explain the result.
*/
SELECT TITLE, COST FROM BOOKS B, PUBLISHER P WHERE P.PUBID = B.PUBID AND P.PUBID = 2
INTERSECT
SELECT TITLE, COST FROM BOOKS WHERE ROWNUM <= 5 ORDER BY COST;
-- FINDS THE BOOKS THAT ARE IN THE TOP 5 MOST EXPENSIVE AND PUBLISHED BY PUBLISHER 2
/**
# Problem 11
Identify a table in the JustLee Books database where a self-join can be
used and write a corresponding SQL query. Explain what the query is
intended to do.
*/
select r.firstname, r.lastname, c.lastname "Referred" from customers c, customers r WHERE c.referred = r.customer#;
-- tells which customers were referred and who referred them.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment