Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4618340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:15:11+00:00 2026-05-22T02:15:11+00:00

i have the following table Students: id | status | school | name —————————-

  • 0

i have the following table Students:

id | status | school | name
----------------------------
0  | fail   | skool1 | dan
1  | fail   | skool1 | steve
2  | pass   | skool2 | joe
3  | fail   | skool2 | aaron

i want a result that gives me

school | fail | pass  
---------------------
skool1 | 2    | 0   
skool2 | 1    | 1    

I have this but it’s slow,

SELECT s.school, (

SELECT COUNT( * ) 
FROM school
WHERE name = s.name
AND status = 'fail'
) AS fail, (

SELECT COUNT( * ) 
FROM school
WHERE name = s.name
AND status = 'pass'
) AS pass,

FROM Students s
GROUP BY s.school

suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:15:12+00:00Added an answer on May 22, 2026 at 2:15 am

    Something like this should work:

    SELECT 
        school,
        SUM(CASE WHEN status = 'fail' THEN 1 ELSE 0 END) as [fail],
        SUM(CASE WHEN status = 'pass' THEN 1 ELSE 0 END) as [pass]
    FROM Students
    GROUP BY school
    ORDER BY school
    

    EDIT
    Almost forgot, but you could also write the query this way:

    SELECT 
        school,
        COUNT(CASE WHEN status = 'fail' THEN 1 END) as [fail],
        COUNT(CASE WHEN status = 'pass' THEN 1 END) as [pass]
    FROM Students
    GROUP BY school
    ORDER BY school
    

    I’m not sure if there’s any performance benefit with second query. My guess would be if there is it’s probably very small. I tend to use the first query because I think it’s more clear but both should work. Also, I don’t have a MySql instance handy to test with, but according to @Johan the ORDER BY clauses are unnecessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After doing some joins and stuff I have table in the following format name
I have the following scenario in zend framework: Data Table of students Table of
I have reduced a complex schema to the following samples Students StudentID int, Name
Suppose I have the following student table: id | name | datecreated ------------------------ 1
I have a table Students with the following schema: CREATE TABLE STUDENTS ( SID
I have the following table Name Subject Month Year ----- ----------- ----- ---- Raju
I have the following table and I want to retrieve students ( STUD_ID )
I have three table in SQL Server 2008: Students StudentId Name 1 Ghanshyam 2
I have a table (session_comments) with the following fields structure: student_id (foreign key to
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.