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.

Answer:
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]

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a