Approach
Relational aggregation
For Find Candidates for Data Scientist Position II, the query transforms and combines relational rows, then filters or aggregates them into the requested result.
- Identify the source rows and join keys.
- Apply filters before aggregation when possible.
- Group, rank, or project the final columns required by the result.
Code notes
- 42 lines of SQL from the credited upstream file 3278.sql.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Review join cardinality, grouping keys, and available indexes when estimating query cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1WITH2 ProjectSkills AS (3 SELECT project_id, COUNT(skill) AS required_skills4 FROM Projects5 GROUP BY 16 ),7 CandidateScores AS (8 SELECT9 Projects.project_id,10 Candidates.candidate_id,11 100 + SUM(12 CASE13 WHEN Candidates.proficiency > Projects.importance THEN 1014 WHEN Candidates.proficiency < Projects.importance THEN -515 ELSE 016 END17 ) AS score,18 COUNT(Projects.skill) AS matched_skills19 FROM Projects20 INNER JOIN Candidates21 USING (skill)22 GROUP BY 1, 223 ),24 RankedCandidates AS (25 SELECT26 CandidateScores.project_id,27 CandidateScores.candidate_id,28 CandidateScores.score,29 RANK() OVER(30 PARTITION BY CandidateScores.project_id31 ORDER BY CandidateScores.score DESC, CandidateScores.candidate_id32 ) AS `rank`33 FROM CandidateScores34 INNER JOIN ProjectSkills35 USING (project_id)36 WHERE CandidateScores.matched_skills = ProjectSkills.required_skills37 )38SELECT project_id, candidate_id, score39FROM RankedCandidates40WHERE `rank` = 141ORDER BY 1;42