I have given following tasks in technical round.
- Write the SQL DDL for creating a new table called Job that relates a person to a company in a Many-to-Many relationship.
- Provided an SQL parameter called
@searchValue
, create an SQL query that will return all people who worked for a company where the name or description contains the search term.
I have studied https://social.technet.microsoft.com/wiki/contents/articles/19854.tutorial-many-to-many-dimension-example-for-sql-server-analysis-services.aspx and checked similar question like How to make SQL many-to-many same-type relationship table but still can not figure it out.
I tried solving questions as below:
1) I have created Job table but wondering that should it contains job_id
as primary key or should I make primary key after combining person_id
and company_id
?
create table Job
(
person_id int,
company_id int,
CONSTRAINT person_cat_pk PRIMARY KEY (person_id, company_id),
CONSTRAINT FK_ person
FOREIGN KEY (person_id) REFERENCES person (person_id),
CONSTRAINT FK_company
FOREIGN KEY (company_id) REFERENCES category (company_id)
);
2) I have used two INNER JOIN
, is it correct in all circumstances? Can we solve it in a simple way? It helps me to understand for future queries.
SELECT a.firstName, a.lastName, a.dateOfBirth
FROM Person a INNER JOIN Job b ON a.id = b.person_id
INNER JOIN Company c ON b.company_id = c.id
WHERE ((c.name LIKE '%' + @searchValue + '%')
OR (c.description LIKE '%' + @searchValue + '%'))