views:

20

answers:

1

Hi I have these two tables "Orders" and "Employees"

Orders Table

OrderID   EmployeeID
1             1
2             1
3             2
4             7
5             2
.             .
.             .
.             .

Employee Table

EmployeeID   Firstname
1               Matin
2               Sina
3               Nima
.                .
.                .
.                .

I want to find the name of the employee which has more Orders.

also this is not my home work:) I am learning Aggregate functions also this a part of my query but I can not get that how can i find the maximum???please help me !thanks

SELECt FirstName FROM Employees E INNER JOIN Orders O ON E.EmployeeID = O.EmployeeID (SELECT Count(EmployeeID) EmployeeCount FROM Orders GROUP BY EmployeeID)
+2  A: 
Select TOP 1 WITH TIES FirstName, Count(OrderID) 
FROM Employees e 
INNER JOIN Orders o on e.EmployeeID = o.EmployeeID 
GROUP BY FirstName
ORDER BY Count(OrderID) DESC

Should doo the trick

PostMan
No I want to show just the name of the employee which has more orders !! for example consider that my above Orders table has 5 rows and my Employees table has 3 rows SO 'Sina' and 'Matin' will be shown as a result!
Take away the TOP 1. This will show each employee, and their count of 'Orders' sorted by who has the most orders
PostMan
Ohh without the count??
PostMan
Use `TOP 1 WITH TIES` then
Martin Smith
See Edit - Thanks Martin Smith
PostMan
if you write "DESC" after Order by and delete the WITH TIES!! the answer will be correct!!! thanks a lot
That's what I had :) Glad you got it sorted
PostMan