Solved

# total number of records from another table

Posted on 2014-01-07
257 Views
hi experts, i hope i get solution
i have 2 table with one to many relation ship,

Courses                      Employees
------------                   ---------------
ID                               CourseID
Name                         EmployeeName
-------------------------------------------------

Course                       Employees
--------------------        ----------------------------------------
ID          Name          CourseID         EmployeeName
--------------------        ----------------------------------------
1           course1            1                 emp1
2           course2            1                 emp2
2                 emp3
2                 emp4
2                 emp5
-------------------------------------------------------------------

i need to write an sql statement that will return
course ID, course Name, Total number of employee in each course

result:
ID         Name          Emp_Total_No.
---------------------------------------------
1           course1           2
2           course2           3
---------------------------------------------

is there any solution to write it in a single query

looking forward for your suggestions

thank you
0
Question by:AZZA-KHAMEES
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 25

Accepted Solution

Lee Savidge earned 500 total points
ID: 39761822
Simplest way is a nested query. Haven't tested it but you get the idea.

``````select ID,
Name,
(select count(e.EmployeeName)
from Emp_Total_No e
where c.id = e.CourseID
group by e.EmployeeName) as Emp_Total_No
from courses c
``````
0

LVL 13

Expert Comment

ID: 39761843
Or you could use a join.

``````SELECT A.ID
,A.NAME
,Count(B.CourseID) AS Emp_Total_No
FROM courses A
JOIN Employees B ON A.ID = B.CourseID
GROUP BY B.courseID
,A.ID
,A.NAME
``````
0

LVL 12

Expert Comment

ID: 39761845
Subquery:
``````SELECT  ID ,
Name ,
( SELECT    COUNT(CourseID)
FROM      Employees
WHERE     Employees.CourseID = Courses.ID
) Emp_Total_No
FROM    Courses
``````
left join:
``````SELECT  C.ID ,
C.Name ,
COUNT(E.CourseID) Emp_Total_No
FROM    dbo.Courses C
LEFT JOIN dbo.Employees E ON C.ID = E.CourseID
GROUP BY C.ID ,
C.Name
``````

PS.
Lee Savidge's solution: no need to group by e.EmployeeName in subquery
jeffld's solution: no need to group BY B.courseID
0

LVL 13

Expert Comment

ID: 39761859
Thanks Pourfard.  You are correct.

``````SELECT A.ID
,A.NAME
,Count(B.CourseID) AS Emp_Total_No
FROM courses A
JOIN Employees B ON A.ID = B.CourseID
GROUP BY A.ID
,A.NAME
``````
0

LVL 6

Expert Comment

ID: 39761904
SELECT ID, NAME, Count(CourseID) as Emp_Total_No.
FROM courses,Employees where ID = CourseID GROUP BY ID, NAME
0

Author Closing Comment

ID: 39761934
thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. â€¦
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
###### Suggested Courses
Course of the Month3 days, 3 hours left to enroll

#### 695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.