Let's imagine abstract database, which contains objects with attached tags (default many-to-many relation with additional table). Now, we have a user, who wants to find objects matching the most of tags he provides, sorted by relevance. For example:
Object1: TagA, TagB, TagC, TagD
Object2: TagA, TagC, TagD
Object3: TagB, TagD, TagE, TagF
Object4: TagA, TagB, TagC, TagE
User asks for TagB, TagC, TagD Expected result should look like following:
Object1 (3 matches)
Object4 (2 matches)
Object2 (2 matches)
Object3 (1 match)
Edit: Sample structure (I created it in SQLite, but we may safely assume MSSql)
CREATE TABLE Objects(Id int not null primary key, Name text);
CREATE TABLE Tags(Id int not null primary key, Name text);
CREATE TABLE ObjectTag(Id int not null primary key, ObjectId int, TagId int);
INSERT INTO Objects(Id, Name) VALUES (1, "Object1");
INSERT INTO Objects(Id, Name) VALUES (2, "Object2");
INSERT INTO Objects(Id, Name) VALUES (3, "Object3");
INSERT INTO Objects(Id, Name) VALUES (4, "Object4");
INSERT INTO Tags(Id, Name) VALUES (1, "TagA");
INSERT INTO Tags(Id, Name) VALUES (2, "TagB");
INSERT INTO Tags(Id, Name) VALUES (3, "TagC");
INSERT INTO Tags(Id, Name) VALUES (4, "TagD");
INSERT INTO Tags(Id, Name) VALUES (5, "TagE");
INSERT INTO Tags(Id, Name) VALUES (6, "TagF");
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (1, 1, 1);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (2, 1, 2);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (3, 1, 3);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (4, 1, 4);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (5, 2, 1);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (6, 2, 3);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (7, 2, 4);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (8, 3, 2);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (9, 3, 4);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (10, 3, 5);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (11, 3, 6);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (12, 4, 1);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (13, 4, 2);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (14, 4, 3);
INSERT INTO ObjectTag(Id, ObjectId, TagId) VALUES (15, 4, 5);
Expected result (for query "TagB, TagC, TagD"):
ObjectName | MatchCount
-----------+-----------
Object1 | 3
Object4 | 2
Object2 | 2
Object3 | 1