0
receipt_id           order_id
1                       A
2                       B
3                       B
4                       C
5                       C

What is the sql code to show all the records in this table where order_id's count is more than 1?

2 Answers 2

3

You just need an aggregate function with a GROUP BY

select order_id
from yourtable
group by order_id
having count(order_id) > 1

See SQL Fiddle with Demo

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

 Select * from table where order_id in (select  distinct order_id
 from table
 group by order_id
 having count(order_id) > 1)

DEMO

2 Comments

Thanks a lot. Although it takes a lot of time to execute in a large table.
just create an index on order_id

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.