Commit 61d98539 authored by michael lundquist's avatar michael lundquist
Browse files

Done with week 5 practice problems. Need to the hands on assignments and week 6 stuff.

parent 25b58239
/**
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.
*/
SELECT 'MICHAEL_LUNDQUIST' FROM DUAL;
/**
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.
*/
SELECT INITCAP('MICHAEL_LUNDQUIST') FROM DUAL;
/**
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'.
*/
SELECT INSTR('MICHAEL LUNDQUIST', ' ') FROM DUAL;
/**
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.
*/
SELECT SUBSTR('MICHAEL LUNDQUIST', 9, 19) AS LASTNAME FROM DUAL;
/**
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.
*/
SELECT SUBSTR('MICHAEL LUNDQUIST', 0, INSTR('MICHAEL LUNDQUIST', ' ')) AS LASTNAME FROM DUAL;
/**
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.
*/
SELECT SUBSTR('MICHAEL LUNDQUIST', INSTR('MICHAEL LUNDQUIST', ' '), LENGTH('MICHAEL LUNDQUIST')) AS LASTNAME FROM DUAL;
/**
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.
*/
SELECT CONCAT('MICHAEL', CONCAT(' ', 'LUNDQUIST')) FROM DUAL;
/**
problem 8
Write an SQL query to retrieve the current date using the SYSDATE function and the DUAL table.
*/
SELECT SYSDATE FROM DUAL;
/**
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.
*/
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.
/**
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').
*/
SELECT TO_DATE('JANUARY 21, 2013', 'MONTH DD, YYYY') FROM DUAL;
/**
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.
*/
SELECT TO_CHAR('21-JAN-13') FROM DUAL;
\ 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.
*/
SELECT MAX(COST), MIN(COST), AVG(COST) FROM BOOKS;
/**
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.
*/
SELECT COUNT(*) FROM BOOKS;
SELECT COUNT(*) FROM BOOKS WHERE NOT DISCOUNT IS NULL;
/**
problem 3
Write an SQL query that uses the GROUP BY, WHERE and HAVING statements. Explain what the query is supposed to do
*/
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
/**
problem 4
Write an SQL query that uses the GROUP BY ROLL statement. Explain what the query is supposed to do.
*/
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.
/**
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
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