How to model a course > section > lesson structure where lessons can belong to either sections or courses? I have three entities:

Course
Section
Lesson

Each &quot;course&quot; is made up of several &quot;lessons&quot;.
The &quot;lessons&quot; inside a course, can either be categorized into &quot;sections&quot;, or not.
So, the contents of a course could look either like this:
Foo Course:
    Lesson 1
    Lesson 2
    Lesson 3
    Lesson 4

Or like this:
Bar Course:
    Section 1:
        Lesson 1
        Lesson 2
    Section 2:
        Lesson 3
        Lesson 4

So, in other words, a course can either directly have &quot;lessons&quot;, or it can have &quot;sections&quot; that in turn have &quot;lessons&quot;.
From the other perspective, a &quot;lessons&quot; can either directly belong to a &quot;course&quot;, or belong to a &quot;section&quot; that in turn belongs to a &quot;course&quot;.
I'm struggling with how to implement this structure in a a relational database.
If every &quot;lesson&quot; had to necessarily belong to a &quot;section&quot;, it would be easy, I could just simply have a &quot;Course&quot; table, a &quot;Section&quot; table with a &quot;CourseId&quot; column, and a &quot;Lesson&quot; table with a &quot;SectionId&quot; column.
But my scenario is not as straightforward as that. A &quot;section&quot; can potentially exist as a middleman between a &quot;course&quot; and several &quot;lessons&quot;, but it can also be absent, in which case a &quot;course&quot; directly has the lessons and no there are no &quot;sections&quot;.
I'd appreciate any suggestions regarding how such a structure can ideally be implemented in the context of relational databases.
Thanks.
Generally when I need to handle this sort of situation in textbooks, I “hide” the full structure of a book from the author. So, for your situation, I would have the following structure:
Course
⇢ Section
  ⇢ Lesson

This way, all courses would have a minimum of one section. This offers a course author a bit of flexibility in the future if they choose to switch to (or from) a Course⇢Lesson to Course⇢Section⇢Lesson model. It also drastically simplifies the SQL queries that will be used, as everything is consistent. Other developers will appreciate this when being introduced to your system.

Disclaimer: I write textbook and online learning software for a living, so my design patterns may be biased as a result of experience working with public and private institutions.