Fahaad Munir
Fahaad Munir

Reputation: 11

How to run two SQL queries together?

I have two sql queries I want to run at once so I don't have to run twice to get the result

SELECT COUNT(*) FROM attendance WHERE month =10 and grade =  4

SELECT COUNT(*) from attendance WHERE month = 10 and grade  = 4 AND userid = 24 and attendance = 'present'

I want two counts of total class and total number of classes in which student is present.

Upvotes: 0

Views: 57

Answers (1)

Stu
Stu

Reputation: 32609

You can combine the second criteria using conditional aggregation, this way the table is only read once.

select 
  Count(*) as TotalCount, 
  Count(case when userid = 24 and attendance = 'present' then 1 end) as StudentCount
from attendance 
where month = 10 and grade = 4;

Upvotes: 5

Related Questions