Having the company name and product name in the sales and purchases tables is wrong. You need to normalize your database.
Database normalization description - Office | Microsoft Docs[
^]
For example:
CREATE TABLE Companies
(
Id int NOT NULL IDENTITY(1, 1),
Name nvarchar(100) NOT NULL,
CONSTRAINT PK_Companies PRIMARY KEY (Id)
);
CREATE TABLE Products
(
Id int NOT NULL IDENTITY(1, 1),
Name nvarchar(100) NOT NULL,
CONSTRAINT PK_Products PRIMARY KEY (Id)
);
CREATE TABLE Purchases
(
Id int NOT NULL IDENTITY(1, 1),
CompanyId int NOT NULL,
ProductId int NOT NULL,
Quantity decimal(18, 6) NOT NULL,
CONSTRAINT PK_Sales PRIMARY KEY (Id),
CONSTRAINT FK_Sales_Company FOREIGN KEY (CompanyId) REFERENCES Companies (Id),
CONSTRAINT FK_Sales_Product FOREIGN KEY (ProductId) REFERENCES Products (Id)
);
CREATE TABLE Sales
(
Id int NOT NULL IDENTITY(1, 1),
CompanyId int NOT NULL,
ProductId int NOT NULL,
Quantity decimal(18, 6) NOT NULL,
CONSTRAINT PK_Purchases PRIMARY KEY (Id),
CONSTRAINT FK_Purchases_Company FOREIGN KEY (CompanyId) REFERENCES Companies (Id),
CONSTRAINT FK_Purchases_Product FOREIGN KEY (ProductId) REFERENCES Products (Id)
);
You also need to consider what happens if you have sales with no purchases. Currently, that product/company combination would not be included in your report.
SELECT
C.Name As CompanyName,
P.Name As ProductName,
B.Quantity
FROM
(
SELECT
IsNull(P.CompanyId, S.CompanyId) As CompanyId,
IsNull(P.ProductId, S.ProductId) As ProductId,
IsNull(P.Quantity, 0) - IsNull(S.Quantity, 0) As Quantity
FROM
(SELECT CompanyId, ProductId, Quantity FROM Purchases) As P
FULL OUTER JOIN (SELECT CompanyId, ProductId, Quantity FROM Sales) As S
ON S.CompanyId = P.CompanyId And S.ProductId = P.ProductId
) As B
INNER JOIN Companies As C ON C.Id = B.CompanyId
INNER JOIN Products As P ON P.Id = B.ProductId
;