db query(s) to find unused/invalid quest ids

Is it possible to determine if a quest id is valid/active in the game from a database query?

I tried checking:
world.quest_template.QuestType == 1
But most usused/invalid world.quest_template rows have this set to “2”

world.quest_template.Flags == QUEST_FLAGS_UNAVAILABLE
But most usused/invalid world.quest_template do not have this flag

I searched every quest related table, and could not find anything helpful.

I am writing a python script to generate a report of uncompleted quests for a player. It works well except I cannot reliably filter out the inactive quest ids from world.quest_template

PS. also, is quest faction determined by world.quest_template.AllowableRaces ?