Related posts using the loop

Hi, I found a way to create related posts for the single post without using a plugin , actually, it doesn’t work when I try it. But I was wondering if there’s a way to accomplish this using a custom query for the loop, maybe including posts that have the same tags or the same category? is more a curiosity than a real support question but it will be nice to accomplish that with the loop.