Where is the category of a post stored?

wp_terms, wp_term_taxonomy, wp_term_relationships and wp_term_meta these 4 tables powers the WordPress taxonomy world. You’ll find the relation between post and term within these 3 tables wp_terms, wp_term_taxonomy, wp_term_relationships. You have to start journey from wp_term_relationships. If you wanna know more about DB schema https://codex.wordpress.org/Database_Description https://code.tutsplus.com/tutorials/understanding-and-working-with-data-in-wordpress–cms-20567

And query_posts() is harmful and bad practice, it overrides the WordPress main query. Better you should use a custom query.

$query = new WP_Query( array(
        'cat' => 2, // category id goes here
    ) );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        // do the whatever you wanna do inside the loop
    endwhile;
endif;