generates SQL query to get children