Mining and pending events
The way to do this is to validate each and every block before beginning work on the next block.  If you validate the block you will have verified each and every transaction included in the block.  This then gives you the knowledge you need to determine if particular transactions were included or not.  When they have been included in a block they are also removed from the mempool.  The process of first validating a block can be slow and is not always popular so many miners take shortcuts and mine using SPV (only the headers)  they are thus unaware of which transactions are included in each block while validation is occurring so to keep miner busy they will mine a block without including any transactions.