Classifying Tree Nodes in SQL with Dynamic Logic

Day 21/100 of #100DaysOfCode 💻 Today's problem made me think in trees. 🌳 Tree Node:- Given a tree structure in a table, classify each node as Root, Inner, or Leaf. The logic: Root → has no parent (p_id IS NULL) Inner → appears as a parent of someone else Leaf → everything else I didn't get it right on the first try. 😅 My first attempt used "WHERE p_id = 1" inside the subquery, which hardcodes the root and breaks for any other tree structure. Wrong logic, wrong output. Then I stepped back and thought about it properly (Code is given in the image) 💡 The fix is instead of hardcoding, ask "does this node appear as a parent of anyone?" If yes → Inner. That's the real tree logic. Never hardcode what SQL can figure out dynamically. 🧩 Trial and error is part of the process. The wrong attempt taught me more than the right one. 😄 #SQL #100DaysOfCode #LearningInPublic #DataAnalytics #Consistency #DevJourney #LeetCode

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories