handling comparing data using SQL