Commit bc8aa0a6 authored by michael lundquist's avatar michael lundquist
Browse files

Adding the final submission documents for week5 chs 11 and10.

parent eddaa2d7
# problem 1
Write an SQL query to display a text string 'your_first_name your_last_name' that represents your full name using the DUAL table.
```sql
SELECT 'MICHAEL_LUNDQUIST' FROM DUAL;
```
![PROBLEM1 ANSWER](./PICS/PROBLEM1.PNG)
## problem 2
Modify the query you created in problem 1. This time, use the INITCAP function to convert characters in the text string 'your_first_name your_last_name' to mixed case.
```sql
SELECT INITCAP('MICHAEL_LUNDQUIST') FROM DUAL;
```
![PROBLEM2 ANSWER](./PICS/PROBLEM2.PNG)
## problem3
Modify the query you created in problem 1. This time, use the INSTR function to determine the position of the single blank space in the text string 'your_first_name your_last_name'.
```sql
SELECT INSTR('MICHAEL LUNDQUIST', ' ') FROM DUAL;
```
![PROBLEM3 ANSWER](./PICS/PROBLEM3.PNG)
## problem 4
Modify the query you created in problem 1. This time, use the SUBSTR function to extract a substring that represents your last name from the text string 'your_first_name your_last_name'. Assume you know the number of characters you need to extract.
```sql
SELECT SUBSTR('MICHAEL LUNDQUIST', 9, 19) AS LASTNAME FROM DUAL;
```
![PROBLEM4 ANSWER](./PICS/PROBLEM4.PNG)
## problem 5
Modify the query you created in problem 1. This time, use the INSTR and SUBSTR functions to extract a substring that represents your first name from the text string 'your_first_name your_last_name'. Assume you do not know the number of characters to extract.
```sql
SELECT SUBSTR('MICHAEL LUNDQUIST', 0, INSTR('MICHAEL LUNDQUIST', ' ')) AS LASTNAME FROM DUAL;
```
![PROBLEM5 ANSWER](./PICS/PROBLEM5.PNG)
## problem 6
Modify the query you created in problem 1. This time, use the INSTR, SUBSTR, and LENGTH functions to extract a substring that represents your last name from the text string 'your_first_name your_last_name'. Assume you do not know the number of characters you need to extract.
```sql
SELECT SUBSTR('MICHAEL LUNDQUIST', INSTR('MICHAEL LUNDQUIST', ' '), LENGTH('MICHAEL LUNDQUIST')) AS LASTNAME FROM DUAL;
```
![PROBLEM6 ANSWER](./PICS/PROBLEM6.PNG)
## problem 7
Write an SQL query to join three character strings: your first name, blank space, and your last name. Use a nested CONCAT function and the DUAL table.
```sql
SELECT CONCAT('MICHAEL', CONCAT(' ', 'LUNDQUIST')) FROM DUAL;
```
![PROBLEM7 ANSWER](./PICS/PROBLEM7.PNG)
## problem 8
Write an SQL query to retrieve the current date using the SYSDATE function and the DUAL table.
```sql
SELECT SYSDATE FROM DUAL;
```
![PROBLEM8 ANSWER](./PICS/PROBLEM8.PNG)
## problem 9
Write an SQL query to calculate the number of months until Christmas. Use the MONTHS_BETWEEN function and the DUAL table. Then, round off the answer to an integer. Decide which function - ROUND or TRANC - to be used for rounding off. Explain your reasoning.
```sql
SELECT TRUNC(MONTHS_BETWEEN('25-DEC-2019', SYSDATE)) FROM DUAL;
```
I used trunc because even if there are 9 months and a few days, there are only 9 full months.
![PROBLEM9 ANSWER](./PICS/PROBLEM9.PNG)
## problem 10
Write an SQL query to display a text string 'JANUARY 21, 2013' as a date using the TO_DATE function and the DUAL table. (Hint: correct format model to use for this string is 'MONTH DD, YYYY').
```sql
SELECT TO_DATE('JANUARY 21, 2013', 'MONTH DD, YYYY') FROM DUAL;
```
![PROBLEM10 ANSWER](./PICS/PROBLEM10.PNG)
## problem 11
Write an SQL query to display a date '21-JAN-13' as a text string using the TO_CHAR function and the DUAL table. You may use any format model that is different from the default Oracle date format.
```sql
SELECT TO_CHAR('21-JAN-13') FROM DUAL;
```
![PROBLEM11 ANSWER](./PICS/PROBLEM11.PNG)
\ No newline at end of file
# problem 1
Calculate the average value, the maximum value, and the minimum value for one of the columns in the JustLee Books Database using the AVG, MAX and MIN group functions.
```sql
SELECT MAX(COST), MIN(COST), AVG(COST) FROM BOOKS;
```
![problem1 pic](./PICS/PROBLEM1.PNG)
## problem 2
Use the COUNT function to count non-NULL values first and then to count the total number of records in one of the tables in JustLee Books Database.
```sql
SELECT COUNT(*) FROM BOOKS;
SELECT COUNT(*) FROM BOOKS WHERE NOT DISCOUNT IS NULL;
```
![problem1 pic](./PICS/PROBLEM2.PNG)
## problem 3
Write an SQL query that uses the GROUP BY, WHERE and HAVING statements. Explain what the query is supposed to do
```sql
SELECT COUNT(*), CATEGORY FROM BOOKS WHERE COST > 10 GROUP BY CATEGORY HAVING CATEGORY <> 'SELF HELP' AND COUNT(*) >= 2;
```
Finds categories of books that have 2 or more books that cost more than $10
![problem1 pic](./PICS/PROBLEM3.PNG)
## problem 4
Write an SQL query that uses the GROUP BY ROLL statement. Explain what the query is supposed to do.
```sql
SELECT TITLE, CATEGORY, SUM(PAIDEACH*QUANTITY) REVENUE, SUM(QUANTITY) SALES, AVG(RETAIL) AS COST_PER_BOOK FROM ORDERS JOIN ORDERITEMS USING(ORDER#) JOIN BOOKS USING (ISBN) GROUP BY ROLLUP(CATEGORY, TITLE) ORDER BY CATEGORY, TITLE;
```
shows the total revenue made from each book, the number of books sold, and the cost per book. Then calculates the revenue made for each category, the number of books sold in a category and the total revenue made.
![problem1 pic](./PICS/PROBLEM4.PNG)
## problem 5
Write an SQL query that uses the GROUP BY CUBE statement. Explain what the query is supposed to do.
```sql
SELECT CUSTOMER#, TITLE, SUM(QUANTITY) FROM CUSTOMERS JOIN ORDERS USING (CUSTOMER#) JOIN ORDERITEMS USING (ORDER#) JOIN BOOKS USING (ISBN) GROUP BY CUBE(CUSTOMER#, TITLE);
```
Tells which items each customer ordered and how many orders they placed for the item. Then it sums the items a customer ordered and sums the total orders for each item
![problem1 pic](./PICS/problem5a.PNG)
![problem1 pic](./PICS/problem5b.PNG)
\ No newline at end of file
......@@ -42,7 +42,6 @@ problem 5
Write an SQL query that uses the GROUP BY CUBE statement. Explain what the query is supposed to do.
*/
SELECT CUSTOMER#, TITLE, SUM(QUANTITY) FROM CUSTOMERS JOIN ORDERS USING (CUSTOMER#) JOIN ORDERITEMS USING (ORDER#) JOIN BOOKS USING (ISBN) GROUP BY CUBE(CUSTOMER#, TITLE);
-- items this customer ordered and customers who ordered each item
\ No newline at end of file
-- Tells which items each customer ordered and how many orders they placed for the item. Then it sums the items a customer ordered and sums the total orders for each item
\ No newline at end of file
/**
# problem1
To complete problem 1, write an SQL query that uses a single-row subquery in a WHERE clause.
*/
/**
## problem 2
To complete problem 2, write an SQL query that uses a multiple-column subquery in a FROM clause. The subquery will use the GROUP BY statement.
*/
/**
## problem 3
To complete problem 3, write an SQL query that uses a single-row subquery in a WHERE clause.
*/
/**
## problem 4
To complete problem 4, write an SQL query that joins two tables and uses a multiple-row subquery in a HAVING clause. The subquery will join two tables.
*/
/**
## problem 5
To complete problem 5, write an SQL query that uses a multiple-row subquery in a WHERE clause. The subquery will include the GROUP BY statement and another multiple-row subquery in a HAVING clause.
*/
/**
## problem 6
To complete problem 6, write an SQL query that uses two multiple-row subqueries in a WHERE clause joined with the AND operator. Each subquery will join multiple tables.
*/
/**
## problem 7
To complete problem 7, write an SQL query that uses a single-row subquery in a WHERE clause.
*/
/**
## problem 8
To complete problem 8, write an SQL query that joins four tables and uses a single-row subquery in a WHERE clause.
*/
/**
## problem 9
To complete problem 9, write an SQL query that joins two tables and uses a multiple-row subquery in a WHERE clause. The subquery will join three tables.
*/
/**
## Problem 10
To complete problem 10, write an SQL query that uses a single-row subquery in a WHERE clause.
*/
\ 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