Amazon Interview Question for Software Engineer / Developers






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

It actually depends on the indexes.

if 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.

- Bala February 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select CustID from TABLE where Date=1 or Date=10

I have no idea about the most optimal one, any suggestion?

- jimmyzzxhlh February 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- davidf February 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

top the bottom statement - "Use the Explian in mysql or"

Explain will give you info on the query itself in mysql

- davidf February 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a better query

Select * FROM Table WHERE Date=1 AND Date=2
   AND OrderID <> OrderID

- davidf February 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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
);

- Anonymous February 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

@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)

- Pandu February 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I thought self join will be more efficient. Please let me know your comments.

select a.custid
from
table a,
table b,
where a.custid = b.custid
and (a.date = date1 and b.date = date2);

- Anonymous May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't davidf answer correct??

- cirus February 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no. he didnt filter for "Same Customer"

shd be unique order AND on two different date AND two different order.

Wow what a one liner turns out to be a good analytical Q. BEWARE :D

- pk July 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I feel the one with the IN clause in the most optimal one , because the one with the subquery and the intersect clauses will take more time is a costlier SQL statement to execute.

- Anonymous March 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select * from TABLE where
TO_DATE(insertion_date,'DD-MON-YY') IN
(TO_DATE('02-MAR-10','DD-MON-YY'),TO_DATE('04-MAR-10','DD-MON-YY'))
>/

- DEEPAK GURUNG March 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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?

- jango April 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Select Cust_ID,Date
   from (select distinct cust_id, date 
From table)

- Anonymous May 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Select A.Cust_ID,A.Date
   from (select distinct cust_id, date 
   From table) A
   Group By (A.Cust_id,A.date)
   having count('*')=2

- Amit May 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select CustId, count(distinct Date) as num from TABLENAME group by CustID having num=2

- Anonymous September 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Select * from Test where Cust_id IN
(
Select Cust_Id
from test
group by Cust_id
Having COUNT(date)=2
)

- HiddenCoder December 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wouldn't this work??

SELECT CUSTOMERID FROM TABLE GROUP BY CUSTOMERID HAVING COUNT(SALEDATE) =2

- bobby June 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select custid
from orders ord_out
exists( select 1
from orders ord_in
where ord_in.custid=ord_out.custid
and ord_out.ord_Date <> ord_in.ord_date);

- James July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SELECT Cust_ID FROM Orders WHERE Order_Date = 1
INTERSECT
SELECT Cust_ID FROM Orders WHERE Order_Date = 2;

- tianchu1987 December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select cust_id
from order_t t
where order_dttm=date1
and exists(select 1 from
order_t t1 where t1.cust_id=t.cust_id
t1.order_dttm=date2)

- Ren March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select 
custid 
from (
select date, custid from table
group by date,custid)
group by custid having count(date)>1;

- murali July 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Select * FROM Table WHERE Date IN[date1, date2];

- clrs March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

select count(distinct Date) as num from TABLENAME group by CustID where num=2

- yangyang4j@gmail.com March 03, 2010 | 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