Skip to content
Advertisement

Retrieve Total Chapters in each books

I have got the database which has id,chapter_name,chapter_num,verse_num,verse_text,testament as columns.

I would like to retrieve chapterName and the total number of chapters in each book.

I’m using the following query to do so,

Unfortunately, it lists a very big count of chapter names and duplicate chapter names as well. How can I fix this? This is the sample input

id,chapter_name,chapter_num,verse_num,verse_text,testament

The output should be,

Advertisement

Answer

This is a table of verses with redundant chapter (chapter_num) and book (chapter_name) information. We must first eliminate the redundancy and get a single row for each book and chapter. Then we can count them.

Redundancy indicates a poorly designed schema. There’s three pieces of information, so there should be three tables: book, chapter, verse.

Now if we want to find how many chapters are in each book…

Or how many verses are in each book…

Try it out.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement