3

Lets say I have two tables, Members and Orders with a 1:N relation:

Members         | Orders
UserID  Name    | OrderID   UserID  Name
111     Peter   | 777       111     Peter
222     Bart    | 888       333     Joe
333     Joe     | 999       111     Peter
444     Andrew  | 101       444     Andrew
                | 102       111     Peter
                | 103       333     Joe

I am trying to get the Members ID from the Members table, in case that Member has more than 1 order in the Orders table.

So the result should be...

Members
UserID  Name
111     Peter
333     Joe

...because Peter and Joe both have at least 2 orders in the Orders table.


I have tried to get a result with:

SELECT
    s.UserID,
FROM Members s
    INNER JOIN Orders o
        ON s.UserID = o.UserID
WHERE
    s.UserID IN
    (
        SELECT UserID
        FROM Orders
        GROUP BY UserID
        HAVING COUNT(*) > 5
    )

But that gives me Peter back 3 times, and Joe 2 times; so I get duplicates instead of each UserID once. How can I get the result without duplicates?

2
  • Did you try SELECT DISTINCT s.UserID ?
    – fen1x
    Nov 15, 2017 at 13:52
  • "Member has more then 1 order in the Orders table"? Why are you using HAVING COUNT(*) > 5 then? And you don't even have to JOIN you also can get your answer out off the Orders.UserID column Nov 15, 2017 at 13:57

5 Answers 5

7

I'm assuming (and hoping) that you're not storing the user's name twice, since that leads to data quality issues when the user changes their name.

Assuming the tables are structured like below:

CREATE TABLE
  Members
(
    UserID INT,
    Name VARCHAR(15)
);

INSERT INTO
  Members
VALUES
(111, 'Peter'),
(222, 'Bart'),
(333, 'Joe'),
(444, 'Andrew');

CREATE TABLE
  Orders
(
   OrderID INT,
   UserID INT
);

INSERT INTO
  Orders
VALUES
(777, 111),
(888, 333),
(999, 111),
(101, 444),
(102, 111),
(103, 333);

You can use a GROUP BY and HAVING clause which would give you the UserID of all users with more than 1 (or whichever number you choose) orders. Then, you join that to the Members table to get the name.

SELECT
  Orders.UserID,
  Members.Name
FROM
  Orders
INNER JOIN
  Members
  ON Orders.UserID = Members.UserID
GROUP BY
  UserID,
  Members.Name
HAVING
  COUNT(OrderID) > 1;

SQLFiddle: http://sqlfiddle.com/#!9/1dadc4/2

However, if you already have the names stored (and that's not changing), then you could skip the JOIN like below:

SELECT
  UserID,
  Name
FROM
  Orders
GROUP BY
  UserID,
  Name
HAVING
  COUNT(OrderID) > 1
3
  • 4
    The last query is the best answer to this question. No join is necessary as the question is phrased. Nov 15, 2017 at 14:07
  • 1
    I agree based on the question that was posed, but would still argue that the current design may lead to long term issues.
    – kchason
    Nov 15, 2017 at 14:11
  • Thank you very much; awesome response time! I was able to get the correct result using your feedback. And thank you for providing the issues and examples. I do store the customer's name twice, but only using the member's table for communication, thereby this always contains the latest members info Nov 15, 2017 at 14:31
3

Is it this what your looking for?

SELECT
    s.UserID,
    s.Name
FROM 
    Members s
INNER JOIN 
    Orders o ON s.UserID = o.UserID
GROUP BY
    s.UserID
HAVING
    COUNT(*) > 5
1
  • Judging by expected result - there should be s.Name after SELECT s.UserID,. Either that, or comma after s.UserID is a typo.
    – fen1x
    Nov 15, 2017 at 13:59
1

You just need a subquery having count(OrderID) > 1 as below.

SELECT *
FROM members
WHERE UserID IN (
        SELECT UserId
        FROM orders
        GROUP BY userid
        HAVING count(OrderID) > 1
        );

Result:

UserID  Name
--------------
111     Peter 
333     Joe  

DEMO

0

To remove duplicates, use DISTINCT.

SELECT
DISTINCT
    s.UserID
FROM Members s
    INNER JOIN Orders o
        ON s.UserID = o.UserID
WHERE
    s.UserID IN
    (
        SELECT UserID
        FROM Orders
        GROUP BY UserID
        HAVING COUNT(*) > 5
    )
0

Here the answer, try to avoid DISTINCT

SELECT 
 UserID
 ,counts.count
FROM
 Members mem

JOIN (SELECT UserID,COUNT(*) as 'count' FROM Orders WHERE COUNT(*) > 1 GROUP BY UserID) counts
ON mem.UserID = counts.UserID
1
  • How does this checks if member has more then 1 order in the Orders table? Nov 15, 2017 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.