A table contains list of customers and his city with other
details. Each customer has a unique number and the table
consists millions of data. Query is: I want to retrieve 10
customers from each city, no script, only from single query?

Answers were Sorted based on User's Feedback



A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / grace

SELECT A.*
FROM dbo.T1 A
WHERE CustomerID IN
(SELECT TOP 10 CustomerID FROM dbo.T1 WHERE City=A.City)

Is This Answer Correct ?    24 Yes 6 No

A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / asim

select id,addressid,city
from
(
SELECT ROW_NUMBER() OVER (partition by city Order by
addressid) ID,AddressID,City
FROM person.Address A
WHERE City IN
(select city from person.address group by City

) )a
where a.ID <11
order by City,ID

Is This Answer Correct ?    6 Yes 0 No

A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / jyoti

CREATE TABLE #CustDetails
(
CustID INT IDENTITY(1,1),
CustName VARCHAR(10),
City VARCHAR(10)
)

INSERT INTO #CustDetails
SELECT 'A1','Pune' UNION ALL
SELECT 'A2','Pune' UNION ALL
SELECT 'A3','Pune' UNION ALL
SELECT 'A4','Pune' UNION ALL
SELECT 'A5','Pune' UNION ALL
SELECT 'A6','Pune' UNION ALL
SELECT 'B1','Delhi' UNION ALL
SELECT 'B2','Delhi' UNION ALL
SELECT 'B3','Delhi' UNION ALL
SELECT 'B4','Delhi' UNION ALL
SELECT 'B5','Delhi' UNION ALL
SELECT 'B6','Delhi' UNION ALL
SELECT 'B7','Delhi' UNION ALL
SELECT 'B8','Delhi' UNION ALL
SELECT 'C1','Mumbai' UNION ALL
SELECT 'C2','Mumbai' UNION ALL
SELECT 'C3','Mumbai' UNION ALL
SELECT 'C4','Mumbai' UNION ALL
SELECT 'C5','Mumbai'

SELECT
ID,CustName,City
FROM
(
SELECT
ROW_NUMBER() OVER (PARTITION BY CITY ORDER
BY CustID DESC) ID,CustName,City
FROM
#CustDetails AS CD1
) A
WHERE A.ID < 3

Is This Answer Correct ?    4 Yes 0 No

A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / rakesh ameta

create table newq(ids numeric(5),sname nvarchar(10),city nvarchar(10))

select * from newq where newq.ids in(select top 10 ids from newq x where newq.city=x.city)

Is This Answer Correct ?    0 Yes 2 No

A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / ravi kumar ravuri

select * from Customer where Customer_ID in (select top 10
from Customer Where City_name in ( select City_Name from
Customer Group by City_Name))

Is This Answer Correct ?    0 Yes 4 No

A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / abhijith

select top 10
from tablename
where city IN
(select distinct(city) from tablename)

Is This Answer Correct ?    2 Yes 6 No

A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / abhijith

select top 10 *
from tablename
where city IN
(select distinct(city) from tablename)

Is This Answer Correct ?    1 Yes 5 No

A table contains list of customers and his city with other details. Each customer has a unique num..

Answer / sonal parekh

select Top 10 CustomerName, city from customer

Is This Answer Correct ?    5 Yes 31 No

Post New Answer

More SQL Server Interview Questions

Explain how dts is used to extract, transform and consolidate data?

0 Answers  


What objects does the fn_my_permissions function reports on? : sql server security

0 Answers  


You are creating an application where users are asked their gender in the gender combo box you have three options: ‘male’ , ‘female’ and ‘i choose not to disclose’ these options are stored in the table as 1, 0 or null which datatype should you use?

0 Answers  


How do clustered indexes store data?

0 Answers  


how can a database be repaired? : Sql server administration

0 Answers  






What are trace flags?

0 Answers  


What's the purpose of Stored Procedure?

6 Answers   Wipro,


What is mssql?

0 Answers  


diffrence between Cluster Index and non Cluster Index

3 Answers   Wipro,


What is model database? : SQL Server Architecture

0 Answers  


Is oracle faster than sql server?

0 Answers  


What is sql profiler. What are the default templates with it? : sql server database administration

0 Answers  


Categories