1

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
1
  • 1
    At a guess, is this mysql? If so (and even if not) please edit your question and add a suitable tag for your RDBMS product. Commented Aug 14, 2014 at 7:16

2 Answers 2

1
Select dist_name,Count(*) total_sch
From School
Group By dist_name
Order By Count(*)
Sign up to request clarification or add additional context in comments.

3 Comments

or SELECT dist_name, COUNT(dist_name) as total_sch FROM school GROUP BY dist_name, COUNT(dist_name) ORDER BY COUNT(dist_name)
thanks buddy <Jithin Shaji> how can i contact u, for more lessons?
This is a great place to learn,technet.microsoft.com/en-US/sqlserver/ff898410
1

You need a GROUP BY:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.