Amazon Interview Question
Software Engineer / Developersselect CustID from TABLE where Date=1 or Date=10
I have no idea about the most optimal one, any suggestion?
Select DISTINCT(CustID) from Table Where Date=1 AND Date=2
The question is "Retrieve the customer who has ordered on two days"
Use the Explian in mysql or
top the bottom statement - "Use the Explian in mysql or"
Explain will give you info on the query itself in mysql
this would not ensure that the record is from the same customer.
SELECT CustId FROM table
WHERE Date =1
AND CustId in
(
SELECT CustID FROM table
WHERE DATE = 2
);
@davidf
Date's value can't be 1 and 2 at the same time. Your query will return 0 results.
The above query is the optimum one as far as I know. Here is another query:
(select custid from table where date=1)intersect (select custid from table where date=2)
but is the date above actually specified?? no right. so how can we give the date like this?
we may have to use GroupBy to group using the customer id and to count number of occurences to check if the entries are equal to 2 or more
right? am not so good with sql queries..but is this logic correct?
It actually depends on the indexes.
- Bala February 28, 2010if you have B+tree index on date, then AND is correct answer.
If you have B+tree index on custid, then using intersect is better.