Because what the code does is simple enough, connects to a database, runs some queries and returns the results. I know it worked because I tested it and for that kind of code there aren't really edge cases. Even if I can't validate or catch syntax errors because I'm not yet experienced enough I can see the overall structure of the code and what it does and if it runs then there's no syntax errors.
It's like with riddles, someone asks you a riddle you think and you think and you draw a blank but if you are given the answer you can instantly validate it even if you didn't know the answer before hand, same with this.
It's like with riddles, someone asks you a riddle you think and you think and you draw a blank but if you are given the answer you can instantly validate it even if you didn't know the answer before hand, same with this.