How to do a MySql query in WordPress?

You can write your own MySQL queries by getting the global $wpdb variable and preparing a SQL statement and then getting the results.

global $wpdb;
$prepared_query = $wpdb->prepare( 'SELECT * FROM [your_table] WHERE LAST_INSERT_ID();' );
$results = $wpdb->get_results( $prepared_query );

Please, be very careful when using this, if you do it incorrectly, it could be very malicious. With what you’re requesting here, you technically don’t need to prepare it, but I’ve gotten in the habit of always preparing my statements just in case they get changed later to be more powerful.

EDIT

I think I wrote my query wrong, try this:

global $wpdb;
$prepared_query = $wpdb->prepare( 'SELECT LAST_INSERT_ID() FROM %s', $table_name );
$results = $wpdb->get_results( $prepared_query );

If that doesn’t work, I found another resource on SO dealing with this, since the ID you mentioned was auto-incrementing, and if you are not worried about simultaneous insertions, then you maybe just need MAX(id) instead.

global $wpdb;
$prepared_query = $wpdb->prepare( 'SELECT MAX(id) FROM %s', $table_name );
$results = $wpdb->get_results( $prepared_query );