title: Produce a list of appointments for each doctor; including doctors name, patient name, date, time and duration
body: <p>I have three tables; </p>
  
  <ul>
  <li>Doctor</li>
  <li>Person</li>
  <li>Appointment</li>
  </ul>
  
  <p>The table structure is:</p>
  
  <p>doctor table:</p>
  
  <pre><code>+-----------+----------+---------+----------------+----------------+
  | doctor_id | phone_no | room_no | date_qualified | date_appointed |
  +-----------+----------+---------+----------------+----------------+
  | 50        | 1234     | 1       | 1963-09-01     | 1991-05-10     |
  | 51        | 1235     | 2       | 1973-09-12     | 1991-05-10     |
  | 52        | 1236     | 3       | 1990-10-02     | 1993-04-01     |
  | 53        | 1237     | 4       | 1965-06-30     | 1994-03-01     |
  +-----------+----------+---------+----------------+----------------+
  </code></pre>
  
  <p>person table</p>
  
  <pre><code>+-----------+----------+-----------+---------------+------+
  | person_id | initials | last_name | date_of_birth | sex  |
  +-----------+----------+-----------+---------------+------+
  | 100       | T        | Williams  | 1972-01-12    | m    |
  | 101       | J        | Garcia    | 1981-03-18    | f    |
  | 102       | W        | Fisher    | 1950-10-22    | m    |
  | 103       | K        | Waldon    | 1942-06-01    | m    |
  | 104       | P        | Timms     | 1928-06-03    | m    |
  | 105       | A        | Dryden    | 1944-06-23    | m    |
  | 106       | F        | Fogg      | 1955-10-16    | f    |
  | 150       | T        | Saj       | 1994-06-17    | m    |
  | 50        | A        | Cameron   | 1937-04-04    | m    |
  | 51        | B        | Finlay    | 1948-12-01    | m    |
  | 52        | C        | King      | 1965-06-06    | f    |
  | 53        | D        | Waldon    | 1938-07-08    | f    |
  +-----------+----------+-----------+---------------+------+
  </code></pre>
  
  <p>appointment table</p>
  
  <pre><code>+-----------+------------+------------+-----------+---------------+
  | doctor_id | patient_id | appt_date  | appt_time | appt_duration |
  +-----------+------------+------------+-----------+---------------+
  | 50        | 100        | 1994-08-10 | 10:00:00  |            10 |
  | 50        | 100        | 1994-08-16 | 10:50:00  |            10 |
  | 50        | 102        | 1994-08-21 | 11:20:00  |            20 |
  | 50        | 103        | 1994-08-10 | 10:10:00  |            10 |
  | 50        | 104        | 1994-08-10 | 10:20:00  |            20 |
  | 52        | 102        | 1994-08-10 | 10:00:00  |            10 |
  | 52        | 105        | 1994-08-10 | 10:10:00  |            10 |
  | 52        | 150        | 2014-03-10 | 12:00:00  |            15 |
  | 53        | 106        | 1994-08-10 | 11:30:00  |            10 |
  +-----------+------------+------------+-----------+---------------+
  </code></pre>
  
  <p>I need to create a query to Produce a list of appointments for each doctor including the doctor's name, the patient's name and the date time and duration of the appointment. The result should be ordered by doctor's name and date/time of appointment.</p>
  
  <p>I tried the following code: </p>
  
  <pre><code>select person.last_name, person.initials, person.last_name, appointment.appt_date, appointment.appt_time, appointment.appt_duration -&gt; from doctor inner join appointment on doctor.doctor_id = appointment.doctor_id -&gt; inner join person on person.person_id = appointment.patient_id -&gt; order by person.initials, person.last_name, appointment.appt_date, appointment.appt_time;
  </code></pre>
  
  <p>And I got the following result which doesnt have the doctors name and also theres 2 columns for the last name when there should only be one. </p>
  
  <pre><code>+-----------+----------+-----------+------------+-----------+---------------+ 
  | last_name | initials | last_name | appt_date  | appt_time | appt_duration | 
  +-----------+----------+-----------+------------+-----------+---------------+ 
  | Dryden    | A        | Dryden    | 1994-08-10 | 10:10:00  | 10            | 
  | Fogg      | F        | Fogg      | 1994-08-10 | 11:30:00  | 10            | 
  | Waldon    | K        | Waldon    | 1994-08-10 | 10:10:00  | 10            | 
  | Timms     | P        | Timms     | 1994-08-10 | 10:20:00  | 20            | 
  | Saj       | T        | Saj       | 2014-03-10 | 12:00:00  | 15            | 
  | Williams  | T        | Williams  | 1994-08-10 | 10:00:00  | 10            | 
  | Williams  | T        | Williams  | 1994-08-16 | 10:50:00  | 10            | 
  | Fisher    | W        | Fisher    | 1994-08-10 | 10:00:00  | 10            | 
  | Fisher    | W        | Fisher    | 1994-08-21 | 11:20:00  | 20            | 
  +-----------+----------+-----------+------------+-----------+---------------+
  </code></pre>