mountain
mountain

Reputation: 106

SQL COUNT function query as per same column value

Am kind of new to sql and I have just come with a situation as shown below. the query that i have so far outputs wrong results i.e

$query = "SELECT sch_name, dist_name COUNT(sch_name) AS total_sch FROM school ORDER BY dist_name";

school

  **sch_name**   **dist_name**
    kaoma        lusaka
    kaloma       lusaka
    momba        mansa
    kebwi        mansa
    matero       ndola

EXPECTED OUTPUT

   **dist_name**     **total_sch**
     lusaka            2
     mansa             2
     ndola             1

Upvotes: 1

Views: 105

Answers (2)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

You need a GROUP BY:

SELECT dist_name, COUNT(sch_name) total_sch
FROM school
GROUP BY dist_name
ORDER BY dist_name

Upvotes: 1

Jithin Shaji
Jithin Shaji

Reputation: 6073

Select dist_name,Count(*) total_sch
From School
Group By dist_name
Order By Count(*)

Upvotes: 1

Related Questions