Skip to content
Advertisement

How to join tables without using join SQL

I’m trying to write a SQL query to grab information from multiple tables and output an end result: a list of all clients that have not placed a stock request yet. Displaying client number will be sufficient.

I am not allowed to use inner join or any type of join to achieve this.

So far this is what I have come up with.

SELECT c.clientNum
FROM client AS C, Stock_Request AS SR
WHERE C.clientNum NOT IN SR.ClientNum 

This current attempt doesn’t return the desired result.

Here is the information from the tables:

(client) 
    INSERT INTO Client (clientName)
        VALUES ('Mike');
    INSERT INTO Client (clientName)
        VALUES ('John');
    INSERT INTO Client (clientName)
        VALUES ('Sally');
    INSERT INTO Client (clientName)
        VALUES ('Margret');
    INSERT INTO Client (clientName)
        VALUES ('Max');

(stock request)
INSERT INTO Stock_Request (requestDate, clientNum)
    VALUES ('2020-12-10',1);
INSERT INTO Stock_Request (requestDate, clientNum)
    VALUES ('2020-05-04',2);
INSERT INTO Stock_Request (requestDate, clientNum)
    VALUES ('2021-07-06',3);
INSERT INTO Stock_Request (requestDate, clientNum)
    VALUES ('2021-07-08',4);
INSERT INTO Stock_Request (requestDate, clientNum)
    VALUES ('2022-02-07',5);

Any help would be appreciated.

Advertisement

Answer

You can achieve it through this code:

SELECT clientNum FROM `client`
WHERE clientNum 
NOT IN (SELECT clientNum FROM stock_request GROUP BY clientNum);

You don’t need to indicate the stock_request table on your main SELECT, you just need to use the stock_request table to fetch all the clientNum in a subquery by using IN, I think your using IN the wrong way.

Check more about MySQL IN Operator.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement