0

Hello guys i need to use mutiple where clause in one sql query as follows but it can't work please help me.

select (select count(total) as 'studentMarks1' from School  where total <60 ),
       (select count(total) as 'studentMarks2' from School  where total >80 ) 
from School
where Id = '8'
1
  • Which database are you using? Commented Feb 27, 2017 at 9:21

2 Answers 2

2

You rather need to use CASE statement like

select SUM(case when total < 60 then 1 else 0 end) as 'studentMarks1',
       sum(case when total > 80 then 1 else 0 end) as 'studentMarks2' 
from School
where Id = '8'
Sign up to request clarification or add additional context in comments.

4 Comments

Thnks @Rahul it works , what i don't know is why they use SUM , so if i need use Sum as operator like select sum(total) ' there will not be a problem in query
@user3518835, that's conditional sum and only when condition meets the sum operation will happen adding 1 to the sum which is same as performing a count operation. if it has to be sum then you can do like select sum(case when condition then total else 0 end)
ooh! it woks . i am sorry to as muck ,i was not familiar with case but it's great , so whe i want to select all like select * where total >50 and where total = 40 how does this works ?
Case expression, not case statement.
1

You cau usually do this with an appropriate CASE statement:

SELECT COUNT(CASE WHEN total < 60 then 1 else NULL END)
    , COUNT(CASE WHEN total > 80 then 1 else NULL END)
FROM School
WHERE ID = '8'

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.