106k views
0 votes
Write a SELECT statement to create a lesson schedule for Feb 1, 2020 with the lesson date/time, student's first and last names, and the horse's registered name. Order the results in ascending order by lesson date/time, then by the horse's registered name. Make sure unassigned lesson times (student ID is NULL) appear in the results.

User Xth
by
8.6k points

2 Answers

10 votes

Final answer:

To create a lesson schedule for February 1, 2020, with the lesson date/time, student's first and last names, and the horse's registered name, use a SELECT statement with JOIN and ORDER BY clauses.

Step-by-step explanation:

To create a lesson schedule for February 1, 2020, with the lesson date/time, student's first and last names, and the horse's registered name, you can use the following SELECT statement:



SELECT lesson_date_time, students.first_name, students.last_name, horses.registered_name
FROM schedule
LEFT JOIN students ON schedule.student_id = students.id
LEFT JOIN horses ON schedule.horse_id = horses.id
WHERE lesson_date_time >= '2020-02-01 00:00:00' AND lesson_date_time < '2020-02-02 00:00:00'
ORDER BY lesson_date_time ASC, horses.registered_name ASC;
  • SELECT: Specifies the columns to be selected in the result set.
  • FROM: Specifies the tables to be queried.
  • LEFT JOIN: Associates the schedule table with the students and horses tables based on the student_id and horse_id columns, respectively.
  • WHERE: Filters the rows to include only those with lesson dates on February 1, 2020.
  • ORDER BY: Sorts the results in ascending order by lesson date/time, then by the horse's registered name.

User Calingasan
by
7.3k points
11 votes

Final answer:

The SQL query provided retrieves the lesson schedule for Feb 1, 2020, including date/time, student names, and horse names, with unassigned times shown and ordered by lesson date/time and horse name.

Step-by-step explanation:

To create a lesson schedule for Feb 1, 2020, with the lesson date/time, student's first and last names, and the horse's registered name while ensuring that unassigned lesson times are included, you should use a SELECT statement. The statement should also order the results in ascending order by the lesson date/time, followed by the horse's registered name. The SQL query might look something like this:

SELECT lesson.date_time, student.first_name, student.last_name, horse.registered_name
FROM lesson
LEFT JOIN student ON lesson.student_id = student.id
LEFT JOIN horse ON lesson.horse_id = horse.id
WHERE lesson.date_time BETWEEN '2020-02-01 00:00:00' AND '2020-02-01 23:59:59'
ORDER BY lesson.date_time ASC, horse.registered_name ASC;

This query selects the required fields, joins the student and horse tables to the lesson table, filters for lessons on the specified date, and orders the results as requested. It also includes lessons that have not been assigned to a student by using a LEFT JOIN and displaying results even if the student ID is NULL.

User Mweisz
by
8.2k points