exam #2 review with answers. the entire exam will be on the computer you can use search engines,...

13
Oracle SQL Exam #2 Review with Answers

Upload: barnard-dalton

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Oracle SQLExam #2 Review with Answers

Page 2: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Exam #2 Details

The entire exam will be on the computer You can use search engines, Oracle SQL help,

notes, books, and PPT’s You can NOT use other people, email, phone a friend,

text a friend, etc. When you complete all of the questions, zip up

the SQL and submit through Angel Drop Box for Exam #2

When you are finished, please verify that I have received your file BEFORE you leave class

You will have the entire 1 hour and 15 minutes to complete the exam

Page 3: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Need to Know

Select statements Select clause From clause Where clause Group by clause Having clause Order by clause

Page 4: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Need to Know

Additional topics Equijoins Subqueries Row functions Aggregate functions And, or , not logic Dates Number and character functions

Page 5: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Practice

Using the Student table How many students are in this table? List the first and last names of the

students using this format: lastname, firstname▪ Notice the space between the comma and

firstname List the area code of the phone number List all unique registration dates List the number of students that

registered on each registration date

Page 6: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Practice

Using the appropriate table(s) How many cities per state are there currently listed in our

database?▪ List in alphabetical order

List the states that have at least 60 unique cities currently listed in our database▪ List in alphabetical order

(1) List all sections that have a start date time of May 4, 2007 (2) List all sections that have a modified date of Jan 2, 2007 Explain the following difference between query (1) and (2) by

writing the appropriate query▪ To find the start date time of May 4, 2007 you needed to use the trunc

function▪ Why did the start date time search require the trunc function?

▪ To find the modified date of Jan 2, 2007 you did not need to use trunc▪ Why did the modified date search not require the trunc function?

Page 7: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Practice

Using the appropriate table(s) List the first name, last name and

section id for all students actually enrolled in a section

List how many classes each student is currently taking

Page 8: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Answers starting with Slide 5

-- How many students are in this table?SELECT count(*)FROM student.student;

-- List the first and last names of the students using this format: lastname, firstnameSELECT last_name || ', ' || first_namefrom student.student;

-- List the area code of the phone numberSELECT phone, substr(phone, 1, 3) as AreaCodefrom student.student;

-- List all unique registration datesSELECT DISTINCT(registration_date)FROM student.student;

Page 9: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Answers continued-- List the number of students that registered on each registration dateSELECT registration_date, count(*)FROM student.studentgroup by registration_date;

-- How many unique cities per state are there currently listed in our database? -- List in alphabetical order.SELECT state, count(distinct city)FROM student.zipcodeGROUP BY stateORDER BY state;

-- List the states that have at least 60 unique cities currently listed in our database. -- List in alphabetical order.SELECT state, count(distinct city) FROM student.zipcodeGROUP BY stateHaving count(distinct city) > 59ORDER BY state;

Page 10: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Answers continued

-- List all sections that have a start date of May 4, 2007-- Using format shows that this field contains a 09:30:00 value for timeSELECT section_id, to_char(start_date_time, 'DD-MON-YYYY HH24:MI:SS')FROM student.sectionWHERE trunc(start_date_time) = '04-May-07';

-- List all sections that have a modified date of Jan 2, 2007-- Using format shows that this field contains a 00:00:00 value for timeSELECT to_char(modified_date, 'DD-MON-YYYY HH24:MI:SS')FROM student.sectionWHERE modified_date = '02-Jan-07';

Page 11: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Answers continued

-- Explain the following difference between query (1) and (2) by writing the appropriate query-- To find the start date time of May 4, 2007 you needed to use the trunc function-- Why did the start date time search require the trunc function?-- To find the modified date of Jan 2, 2007 you did not need to use trunc-- Why did the modified date search not require the trunc function?-- ANSWER: The start_date_time column was loaded with a time other than 00:00:00-- The modified_date was loaded without a time, in other words, 00:00:00-- So when you search on modified date without the trunc function it looked for a 00:00:00 -- time and found it. The start_date_time value did not have a 00:00:00 value so you -- needed to use the trunc function to eliminate the time value from the where clause-- search.

-- List the first name, last name and section id for all students actually enrolled in a sectionSELECT s.first_name, s.last_name, e.section_idFROM student.student s, student.enrollment ewhere s.student_id = e.student_id;

Page 12: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

Answers continued

-- List how many classes each student is currently takingSELECT e.student_id, s.first_name, s.last_name, count(e.section_id)FROM student.student s, student.enrollment ewhere s.student_id = e.student_idGROUP BY e.student_id, s.first_name, s.last_nameorder by e.student_id;

Page 13: Exam #2 Review with Answers.  The entire exam will be on the computer  You can use search engines, Oracle SQL help, notes, books, and PPT’s  You can

For October 30th

Exam #2 If you do not attend class, You must contact me immediately▪ This test is entirely computer-based▪ Thus it will NOT be placed in the Test Center

▪ This is your responsibility! ▪ You will have until 12:00 p.m. (noon) on

Friday, November 1st to complete the exam

▪ If you do not complete the exam, you will earn 0 points for this exam score