Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 1 vote

First, you will need to create a non repeat id for each row.
Then use
DELETE FROM foo WHERE id NOT IN (SELECT min(id) FROM foo GROUP BY col1,col2,...)

- Ares.Luo.T September 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So you will modify the schema to do this?

- eugene.yarovoi September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't know if there's a super convenient way of doing it, but you could try:

SELECT DISTINCT * INTO #temp FROM YourTable;
TRUNCATE TABLE YourTable;
SELECT * INTO YourTable FROM #temp;
DROP TABLE #temp;

You might want to attempt this in a transaction.

- eugene.yarovoi September 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, didn't see that it was specific to MySQL. But I'm sure something very similar can be done in MySQL. #temp is a temp table.

- eugene.yarovoi September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, he has written only logic.
MySQL specific code, we can find anywhere in net... :)

- Anonymous September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I would also point out that you *need* to attempt this in an atomic transaction, unless it's not possible that someone else might modify the database concurrently. If someone could be modifying the database concurrently, they could insert data in between the time you do SELECT DISTINCT * INTO #temp FROM YourTable; and
TRUNCATE TABLE YourTable;, and the truncate would lose all of their changes. And then you couldn't roll them back, too.

- eugene.yarovoi September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

write the query without using in built function such as DISTINCT and etc

- Surender September 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

DISTINCT is not a function. It's a SQL keyword, so it doesn't seem that reasonable to deny me use of this. Can I use GROUP BY, or is that not OK either? If GROUP BY can't be used either, what can? What's the set of allowed operations?

- eugene.yarovoi September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's sort of arbitrary to deny DISTINCT and allow GROUP BY. You can just change the first line to SELECT * INTO #temp FROM YourTable GROUP BY *

If your dialect of SQL won't allow * for GROUP BY, you'll have to name the columns explicitly. If that has to be dynamic (columns are not known at coding time), you'll have to look at metadata to get that info and then dynamically construct a SQL string for execution.

- eugene.yarovoi September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can use GROUP BY .........

- Surender September 05, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More