Rewriting sql query in two lines