Hi
I have 2 tables and I want to Get information from that tables by SQL Query but How Can I writ this SQL Query ? .. My target as Follow
Class Table
---------------
ClassID ClassName
1 AA
2 BB
Student Table
---------------
StudentID StudentName ClassID
1 Student 1 1
2 Student 2 1
3 Student 3 2
4 Student 4 1
5 Student 5 2
6 Student 6 1
How Can I Writ SQL Query to get result like the following ..
----------------
ClassID ClassName StudentCount
1 AA 4
2 BB 2
My SQL Query must get all Class table column plus column content the count of student in each class
And thanks with my regarding
Fraas
The simplest way to accomplish this would be a query like this:
SELECT
Class.ClassID,
Class.ClassName,
Count(Student.StudentID) AS StudentCount
FROM
Class
LEFT OUTER JOIN
Student ON Class.ClassID = Student.ClassID
GROUP BY
Class.ClassID,
Class.ClassName
ORDER BY
Class.ClassID
This will return one row for each class, with the number of students ineach class. If no students are found for the class 0 will appearin the StudentCount column.
|||In a related question, how would someone run a similar query thatwouldn't count the number of students but rather concatenate the names,preferably with separators?
|||
Thanks For Your Answer .. it's work nice
No comments:
Post a Comment