Here is the example table I created for demo:
Code:
table: sales
customers dept
-------------------- ------
mark IT
Jones IT
peter IT
lincon IT
dell IT
will HR
al HR
davi HR
lincon HR
susan HR
following query shoud get the first 3 records for each department, ordered by customer's name
[ocde]
select t2.customers, t2.dept
from
sales t2 inner join
(
select t1.customers, t1.dept
from
sales t0 inner join sales t1
on t0.dept = t1.dept and t0.customers < t1.customers
group by t1.customers, t1.dept
having count(*) = 3
) t3
on t2.dept = t3.dept and t2.customers < t3.customers
order by t2.dept, t2.customers
[/code]
following are the results of this query:
Code:
customers dept
-------------------- --------------------
al HR
davi HR
lincon HR
dell IT
Jones IT
lincon IT