A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
Please try the following.
If you really need the % sign in the output, you can try to uncomment the commented line.
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE
(
ID INT IDENTITY PRIMARY KEY
, exam VARCHAR(20)
, result VARCHAR(10)
);
INSERT INTO @tbl (exam, result) VALUES
('Math', 'Pass'),
('Math', 'Pass'),
('Math', 'Fail'),
('English', 'Pass'),
('English', 'Pass'),
('English', 'Pass'),
('Science', 'Fail'),
('Science', 'Fail'),
('Science', 'Pass');
-- DDL and sample data population, end
SELECT exam
, FORMAT(SUM(IIF(result = 'Pass', 1, 0)) * 100.00 / COUNT(*), '###.##') AS PassRate
--, FORMAT(SUM(IIF(result = 'Pass', 1, 0)) * 1.00 / COUNT(*), '###.##%') AS PassRate
FROM @tbl
GROUP BY exam;
Output
+---------+----------+
| exam | PassRate |
+---------+----------+
| English | 100 |
| Math | 66.67 |
| Science | 33.33 |
+---------+----------+