27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Solutions</strong> to Chapter 15 | Databases<br />

15.5 Imagine a simple database storing information for students’ grades. Design what this<br />

database might look like, <strong>and</strong> provide a SQL query to return a list of <strong>the</strong> honor roll<br />

students (top 10%), sorted by <strong>the</strong>ir grade point average.<br />

SOLUTION<br />

pg 80<br />

In a simplistic database, we’ll have at least <strong>the</strong>se three objects: Students, Courses, <strong>and</strong> CourseEnrollment.<br />

Students will have at least <strong>the</strong> student name <strong>and</strong> ID, <strong>and</strong> will likely have o<strong>the</strong>r<br />

personal information. Courses will contain <strong>the</strong> course name <strong>and</strong> ID, <strong>and</strong> will likely contain<br />

<strong>the</strong> course description, professor, etc. CourseEnrollment will pair Students <strong>and</strong> Courses, <strong>and</strong><br />

will also contain a field for CourseGrade. We will assume that CourseGrade is an integer.<br />

Our SQL query to get <strong>the</strong> list of honor roll students might look like this:<br />

1 SELECT StudentName, GPA<br />

2 FROM (<br />

3 SELECT top 10 percent Avg(CourseEnrollment.Grade) AS GPA,<br />

4 CourseEnrollment.StudentID<br />

5 FROM CourseEnrollment<br />

6 GROUP BY CourseEnrollment.StudentID<br />

7 ORDER BY Avg(CourseEnrollment.Grade)) Honors<br />

8 INNER JOIN Students ON Honors.StudentID = Students.StudentID<br />

This database could get arbitrarily more complicated if we wanted to add in professor information,<br />

billing, etc.<br />

CareerCup.com<br />

2 3 6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!