Financial Accounting > QUESTIONS & ANSWERS > Mary Doolin SQL_4 Homework COP4710 (All)

Mary Doolin SQL_4 Homework COP4710

Document Content and Description Below

Mary Doolin SQL_4 Homework COP4710 8.25/10 ********************************** Ex 4-1 ************************************ Display the COUNT of rows in each of the tables: Grade_report, Student, and... Section. How many rows would you expect in the Cartesian product of all three tables? Display the COUNT (not the resulting rows) of the Cartesian product of all three and verify your result. Use SELECT COUNT(*) ... SQL> SELECT COUNT(*) 2 FROM GR; COUNT(*) ---------- 209 SQL> SELECT COUNT(*) 2 FROM Stu; COUNT(*) ---------- 48 SQL> SELECT COUNT (*) FROM rearp.Section; COUNT(*) ---------- 32 Cartesian Product of all three tables: grade_report * student * section 209 * 48 * 32 = 321024 SQL> SELECT COUNT(*) FROM Stu, GR, rearp.Section; COUNT(*) ---------- 321024 1Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-2 ************************************ Display the COUNT of section-ids from the Section table, and then the COUNT of DISTINCT section-ids from the Grade_report table. What does this information tell you? Hint: section_id is the primary key of the Section table. SQL> SELECT COUNT(SECTION_ID) 2 FROM sec; COUNT(SECTION_ID) ----------------- 32 SQL> SELECT COUNT(SECTION_ID) 2 FROM GR; COUNT(SECTION_ID) ----------------- 209 SQL>SELECT COUNT(DISTINCT(SECTION_ID)) 2 FROM Grade_report; COUNT(DISTINCT(SECTION_ID)) ----------------- 30 The Cartesian is highest number count for each table multiplied with other table. For section row count would be 32 and Grade_report would be 209. 2Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-3a ************************************ Get the statement to work as a COUNT of a join of the three tables, Student, Grade_report, 105 and Section. Use table aliases in the join condition (remember to use /* join conditions */). Note that a join of n tables requires (n-1) join conditions, so here you have to have two join conditions—one to join the Student and Grade_report tables, and one to join the Grade_report and Section tables. Note the number of rows that you get (expect no more rows than is in the Grade_report table.) Why? SQL> SELECT COUNT(*) 2 FROM Stu, gr, sec 3 WHERE stu.stno = gr.student_number 4 --join student to grade_report 5 AND gr.section_id = sec.section_id 6 --join grade_report to section; COUNT(*) ---------- 209 ********************************** Ex 4-3b ************************************ Modify the query and put the accounting (ACCT) condition in the WHERE clause. Note the number of rows in the result—it should be a good bit less than in (a). SQL> SELECT COUNT(*) 2 FROM stu, gr, sec 3 WHERE stu.stno = gr.student_number 4 --join student to grade_report 5 AND gr.section_id = sec.section_id 6 --join grade_report to section 7 AND stu.major = 'ACCT'; --course_num, not major COUNT(*) ---------- 27 3Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-3c ************************************ Again, modify the query and add the grade constraints. The number of rows should decrease again. Note that if you have WHERE x and y or z, parentheses are optional, but then the criteria will be interpreted according to precedence rules. SQL> SELECT COUNT(*) 2 FROM stu, gr, sec 3 WHERE stu.stno = gr.student_number 4 --join student to grade_reprot 5 AND gr.section_id = sec.section_id 6 --join grade_report to section 7 AND stu.major = 'ACCT' 8 -- only show accounting majors 9 AND gr.grade = 'A' --How can grade be both A and B? See the explanation e-mail 10 AND gr.grade = 'B' 11 -- show only A and B students; COUNT(*) ---------- 0 ********************************** Ex 4-4a ************************************ How many students have names like “SMITH” or “Smith”? SQL> SELECT COUNT(*) FROM stu 2 WHERE UPPER(sname) LIKE '%SMITH%'; COUNT(*) ---------- 3 4Mary Doolin SQL_4 Homework COP4710 SQL> SELECT COUNT(*) FROM stu 2 WHERE UPPER(sname) = 'SMITH'; COUNT(*) ---------- 1 ********************************** Ex 4-4b *********************************** How many have names that contain the letter sequence “SMITH”? SQL> SELECT COUNT(*) FROM stu 2 WHERE UPPER(sname) LIKE '%SMITH%'; COUNT(*) ---------- 3 ********************************** Ex 4-4c *********************************** How many student names end in “LD”? SQL> SELECT COUNT(*) FROM stu 2 WHERE UPPER(sname) LIKE '%LD'; COUNT(*) ---------- 1 5Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-4d *********************************** Would “SELECT * FROM Student WHERE sname LIKE ‘SMITH%’” find someone whose name was: (i) “LA SMITH” (ii) “SMITH-JONES” (iii) “SMITH JR.” (iv) “SMITH, JR” SQL> SELECT * FROM stu 2 WHERE sname LIKE 'SMITH%'; no rows selected Answer: No. –This is an abstract question that has nothing to do with the student table ********************************** Ex 4-4e *********************************** Would you call UPPER or LOWER an aggregate function? Why or why not? Yes because the queries would produce the same result. –No, they are not ********************************** Ex 4-4f *********************************** Pad all the student names in the Student table with ... on the right. SQL> SELECT sname||'...' 2 FROM stu; SNAME||'...' --------------------------------------------------------------------- Lineas... Mary... Brenda... Richard... Kelly... Lujack... Reva... Elainie... 6Mary Doolin SQL_4 Homework COP4710 Harley... Donald... ********************************** Ex 4-5 *********************************** List the junior level COSC courses (like COSC3xxx) and the name of the course. Use the Course table. SQL> SELECT course_number, course_name 2 FROM cou 3 WHERE course_number LIKE 'COSC3%'; COURSE_NUMBER COURSE_NAME ------------------------ ------------------------------------------------------------ COSC3320 DATA STRUCTURES COSC3380 DATABASE COSC3701 OPERATIONS RESEARCH ********************************** Ex 4-6 *********************************** Using the COUNT feature, determine whether there are duplicate names or student numbers in the Student table. SQL> SELECT COUNT(sname) FROM stu; COUNT(SNAME) ------------ 48 SQL> SELECT COUNT(stno) FROM stu; COUNT(STNO) ----------- 48 SQL> SELECT COUNT(DISTINCT sname) FROM stu; COUNT(DISTINCTSNAME) -------------------- 7Mary Doolin SQL_4 Homework COP4710 47 SQL> SELECT COUNT(DISTINCT stno) FROM stu; COUNT(DISTINCTSTNO) ------------------- 48 ********************************** Ex 4-7 *********************************** Assume that all math courses start with MATH. How many math courses are there in the Section table? From the COUNT of courses, does it appear that there are any math courses in the Section table that are not in the Course table? Again, using COUNTs, are there any math courses in the Course table that are not in the Section table? Does it appear that there are any courses at all that are in the Grade_report, Section, or Course tables that are not in the others? (We will study how to ask these questions in SQL in a later chapter). Note that a query like the following would not work: SELECT g.section_id FROM Grade_report g, Section t WHERE g.section_id <> t.section_id; Explain why WHERE .. <> .. will not work to produce the desired output. SQL> SELECT COUNT(course_num) 2 FROM sec 3 WHERE course_num LIKE 'MATH%'; COUNT(COURSE_NUM) --??? ----------------- 6 < > is NOT an = condition 8Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-8a *********************************** Display dictionary views for the tables we have in the Student-Course database (refer to the diagram in Appendix 3 for all the table names). Use “all_tables” as the dictionary view. Do the query as follows: DESCRIBE the table with DESC all_tables; SQL> DESC ALL_TABLES; Name Null? Type ------------------------------ -------- ------------------------------------------------- OWNER NOT NULL VARCHAR2(30) TABLE_NAME NOT NULL VARCHAR2(30) TABLESPACE_NAME VARCHAR2(30) CLUSTER_NAME VARCHAR2(30) IOT_NAME VARCHAR2(30) STATUS VARCHAR2(8) PCT_FREE NUMBER PCT_USED NUMBER INI_TRANS NUMBER MAX_TRANS NUMBER ********************************** Ex 4-8b *********************************** Display the number of rows in all_tables. Use SELECT COUNT(*) FROM all_tables; Observe that when you are exploring the dictionary, it is not a good idea to simply SELECT * FROM whatever, where whatever is some dictionary view. Dictionary views are often long and wide—wide in that there are often many attributes and many of those attributes are not necessarily interesting. SQL> SELECT COUNT(*) FROM ALL_TABLES; COUNT(*) ---------- 140 9Mary Doolin SQL_4 Homework COP4710 ********************************** Ex 4-8c *********************************** Display the owner and table_name from all_tables where owner = ‘your userid’. SQL> SELECT owner, table_name 2 FROM ALL_TABLES 3 WHERE owner = 'ASTEW'; OWNER TABLE_NAME ------------------- -------------------- ASTEW CUST ********************************** Ex 4-8e *********************************** Display the first two rows (use WHERE rownum < 3) of the all_tables table. SQL> SELECT * FROM ALL_TABLES 2 WHERE rownum < 3; OWNER ------------------------------------------------------------------------------------------ TABLE_NAME ------------------------------------------------------------------------------------------ TABLESPACE_NAME ------------------------------------------------------------------------------------------ CLUSTER_NAME ------------------------------------------------------------------------------------------ IOT_NAME STATUS PCT_FREE PCT_USED INI_TRANS ------------------------------------------------------------------------------------------ ------------------------ ---------- ---------- ---------- MAX_TRANS INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS MAX_EXTENTS PCT_INCREASE FREELISTS FREELIST_GROUPS LOGGING BAC NUM_ROWS BLOCKS EMPTY_BLOCKS ---------- -------------- ----------- ----------- ----------- ------------ ---------- --------------- --------- --- ---------- ---------- ------------ 10Mary Doolin SQL_4 Homework COP4710 AVG_SPACE CHAIN_CNT AVG_ROW_LEN AVG_SPACE_FREELIST_BLOCKS NUM_FREELIST_BLOCKS DEGREE INSTANCES ---------- ---------- ----------- ------------------------- ------------------- ------------------------------ ------------------------------ CACHE TABLE_LOCK SAMPLE_SIZE LAST_ANALYZED PARTITION IOT_TYPE TEM SEC NESTED --------------- ------------------------ ----------- --------------- --------- ------------------------------------ --- --- --------- BUFFER_POOL FLASH_CACHE CELL_FLASH_CACHE ROW_MOVEMENT GLOBAL_ST USER_STAT --------------------- --------------------- --------------------- ------------------------ --------- --------- DURATION SKIP_CORRUPT MONITORIN --------------------------------------------- ------------------------ --------- CLUSTER_OWNER DEPENDENCIES COMPRESSION ------------------------------------------------------------------------------------------ ------------------------ ------------------------ COMPRESS_FOR DROPPED READ_ONLY SEGMENT_C RESULT_CACHE ------------------------------------ --------- --------- --------- --------------------- SYS ********************************** 4-9 *************************************** For Room, Course, Prereq, Department_to_major, list the attributes, number of rows, number of distinct rows, and number of rows without nulls. As you gather the information, put the information in a tabular format, as shown below (note – you may want to create this table in your word processor as you gather the information): all the tables in the Student-Course database – Student, Grade_report, Section, Table Student Section Attribute 11Mary Doolin SQL_4 Homework COP4710 stno sname major class section_id Rows without Nulls 48 48 3 etc. Rows Distinct Rows 48 48 47 8 etc. etc. Also, note that there is probably no “one” query that will give you this information. You will have to find this information using separate queries, and then put the information together in a tabular format. Hint: you can use: SELECT COUNT (*) FROM Student WHERE sname IS NULL Table Attribute rows distinct rows rows without nulls STUDENT (stu) stno 48 48 48 Sname 48 48 Major 8 45 Class 4 38 Bdate 23 48 Section section_id 32 32 32 Course_num 20 32 Semester 2 32 Year 3 32 Instructor 15 29 Bldg. 5 29 Room 4 25 12Mary Doolin SQL_4 Homework COP4710 Grade_report student_number 46 209 Section_id 30 209 Grade 5 114 ********************************** Ex 4-10a *********************************** Find the count, sum, average, minimum, and maximum capacity of rooms in the database (hint: Use the Room table for this question). SQL> SELECT COUNT(CAPACITY), SUM(CAPACITY), AVG(CAPACITY), MIN(CAPACITY), MAX(CAPACITY) 2 FROM rearp.room; COUNT(CAPACITY) SUM(CAPACITY) AVG(CAPACITY) MIN(CAPACITY) MAX(CAPACITY) --------------- ------------- ------------- ------------- ------------- 9 352 39.1111111 22 85 ********************************** Ex 4-10b *********************************** Where there is a null value for the capacity, assume the capacity to be 40, and find the average room size again. SQL> SELECT AVG(NVL(capacity, 0)) 2 FROM rearp.room; AVG(NVL(CAPACITY,0)) -------------------- 35.2 ********************************** Ex 4-12 *********************************** 13Mary Doolin SQL_4 Homework COP4710 In Chapter 2, we introduced some date functions, namely ADD_MONTHS and MONTHS_ BETWEEN. Use the MONTHS_BETWEEN and other appropriate functions to write one query that returns the number of years between the oldest and youngest student in the Student table. SQL> SELECT MONTHS_BETWEEN(MAX(bdate),MIN(bdate)) --/12 for years 2 FROM stu; MONTHS_BETWEEN(MAX(BDATE),MIN(BDATE)) ------------------------------------- 59.1290323 1 [Show More]

Last updated: 1 year ago

Preview 1 out of 14 pages

Reviews( 0 )

$9.00

Add to cart

Instant download

Can't find what you want? Try our AI powered Search

OR

GET ASSIGNMENT HELP
35
0

Document information


Connected school, study & course


About the document


Uploaded On

Jul 05, 2021

Number of pages

14

Written in

Seller


seller-icon
QuizMaster

Member since 3 years

11 Documents Sold


Additional information

This document has been written for:

Uploaded

Jul 05, 2021

Downloads

 0

Views

 35

Document Keyword Tags

Recommended For You

What is Browsegrades

In Browsegrades, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.

We are here to help

We're available through e-mail, Twitter, Facebook, and live chat.
 FAQ
 Questions? Leave a message!

Follow us on
 Twitter

Copyright © Browsegrades · High quality services·