• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Newb sql question

jinduy

Diamond Member
say you have a list of employees with their names...

let's say there's two guys named David

how would you make a query where it only displays duplicate names, such as David's?

btw this isn't for hw or work, i'm just curious cuz my friend was asked this in an interview ^^

edit:

david's name would have to show up twice btw in the result
 
has to be done in sql, my friend didn't get the job because the other dude he was competing with got a more efficient answer... so im really curious. i'm no sql expert but i think gshock got it right woot!
 
Originally posted by: jinduy
say you have a list of employees with their names...

let's say there's two guys named David

how would you make a query where it only displays duplicate names, such as David's?

btw this isn't for hw or work, i'm just curious cuz my friend was asked this in an interview ^^

if its for a job u best know more than the count function... brush up on your sql skillz
 
oh wait that's not right i forgot to mention that the query would have to display David twice, not just one david, which that query would do i think.


correct result:

1. David
2. David

incorrect:

1. David
 
how about this

select count(lastname) as count_name, lastname from table where count_name>1

gets you

2 david
5 smith


etc etc, you can even use order by count_name to start from lowest dupes to most dupes
 
I'd walk ove the Pete, the SQL guy and have him write me a .sql file that I can call from vbs or a bat...

But that's just me...
 
Here is how Access finds duplicates, and still allows you to display other fields


SELECT tblName.FirstName, tblName.LastName
FROM tblName
WHERE (((tblName.FirstName) In (SELECT [FirstName] FROM [tblName] As Tmp GROUP BY [FirstName] HAVING Count(*)>1 )))
ORDER BY tblName.FirstName;

It uses a query in the where clause to find duplicate values.
 
Originally posted by: gshock888
select (last name field) where count(last name field) > 1


Actually this doesn't work- at least in MySQL. You need to use 'having' instead of 'where' when doing things like that with count. ex:

SELECT * FROM name HAVING count(name) > 1
 
Originally posted by: jinduy
say you have a list of employees with their names...

let's say there's two guys named David

how would you make a query where it only displays duplicate names, such as David's?

btw this isn't for hw or work, i'm just curious cuz my friend was asked this in an interview ^^

edit:

david's name would have to show up twice btw in the result

This question sounds familiar... Did your friend interview with a company here in the Los Angeles area (Glendale to be more specific)?
 
My god. This thread makes me cry.

<SQL>
SELECT * from [TableOfEmployees] where [LastNameField] IN (Select [LastNameField] HAVING Count([LastNameField]) > 1)
</SQL>

There are of course other ways to do it.
 
Back
Top