# Matching Employees’ Skills with Jobs

Question:
[Joe Celko’s Employment Agency Puzzle]

You are running an employment agency that has requests for jobs and applications from candidates. Both of these forms include a list of the skills required or offered, and whether this is a primary or secondary skill. The tables look like this:

`    CREATE TABLE CandidateSkills        (candidateid INTEGER NOT NULL PRIMARY KEY,         name CHAR(20) NOT NULL,         skillid INTEGER NOT NULL,         skilllevel INTEGER NOT NULL CHECK (skilllevel IN (1, 2)),         PRIMARY KEY (candidateid, skillid, skilllevel)        );    CREATE TABLE JobSkills        (jobid INTEGER NOT NULL,         skillid INTEGER NOT NULL,         skilllevel INTEGER NOT NULL CHECK (skilllevel IN (1, 2)),         PRIMARY KEY (jobid, skillid, skilllevel)        );`
We need to write some queries that will match up candidates with jobs. But our service offers three kinds of matchings for a job:

1. A list of candidate names who each exhibit ALL the primary skills and ANY of the secondary skills.

2. A list of candidate names who each exhibit ALL the primary skills and ALL of the secondary skills.

3. A list of candidate names who each exhibit ALL the primary skills and (n) number of the secondary skills.

To find people with all the primary skills:

`    SELECT C1.name, C1.candidateid, J1.jobid        FROM JobSkills AS J1    LEFT OUTER JOIN        Candidates AS C1        ON J1.skilllevel = C1.skilllevel            AND J1.skillid = C1.skillid;    GROUP BY C1.name, C1.candidateid, J1.jobid    HAVING ((SELECT COUNT(skillid)         FROM Candidates AS C2        WHERE C1.candidateid = C2.candidateid            AND C2.skilllevel = 1)            >= (SELECT COUNT(skillid)                 FROM JobSkills AS J2                WHERE J1.jobid = J2.jobid                    AND J2.skilllevel = 1))                    AND [add one of the following];`
a) All the secondary skills:
`                    ((SELECT COUNT(skillid)                         FROM Candidates AS C2                        WHERE C1.candidateid = C2.candidateid                            AND C2.skilllevel = 2)                            >= (SELECT COUNT(skillid)                                 FROM JobSkills AS J2                                WHERE J1.jobid = J2.jobid                                    AND J2.skilllevel = 2))`
b) Any of the secondary skills:
`                    ((SELECT COUNT(skillid)                         FROM Candidates AS C2                        WHERE C1.candidateid = C2.candidateid                            AND C2.skilllevel = 2) > 0)`
c) exactly (n) of the secondary skills:
`                    ((SELECT COUNT(skillid)                         FROM Candidates AS C2                        WHERE C1.candidateid = C2.candidateid                            AND C2.skilllevel = 2) = :n)`

Puzzle provided courtesy of:
Joe Celko
[email protected]

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.