Identify matching records in single column query

Posted on 2005-04-30
Medium Priority
Last Modified: 2010-03-19
Hi All,

I have the following table:

ID | Name | Level

In this table each Name has a Level (1-6).  I need a query that will return all the rows where there is a level 4 and 5 record with an identical name.


ID | Name | Level
1     dog       5
2     cat        5
3     sheep    4
4     horse    4
5     cow      5
6     cat       4

In the above example the query should return record 6 "cat" and record 2 "cat".

Hope this makes sense.

Question by:tim_chamberlain
LVL 17

Accepted Solution

mokule earned 400 total points
ID: 13900118
SELECT a.ID, a.Name, a.Level FROM test12 a
JOIN test12 b ON a.Name=b.Name
WHERE (a.Level=4 AND b.Level=5) OR (a.Level=5 AND b.Level=4)

Author Comment

ID: 13900141
Perfect. Thanks very much.

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question