SQL code for Analysis 3