create table tbl(uczen varchar(20), ocena integer);
insert into tbl values('JANEK',5);
insert into tbl values('JANEK',5);
insert into tbl values('JANEK',4);
insert into tbl values('JANEK',4);
insert into tbl values('ANIA',5);
insert into tbl values('ANIA',4);
SELECT
uczen,
SUM(ocena = 1) AS "1-ki",
SUM(ocena = 2) AS "2-ki",
SUM(ocena = 3) AS "3-ki",
SUM(ocena = 4) AS "4-ki",
SUM(ocena = 5) AS "5-ki"
FROM
tbl
GROUP BY
uczen
ORDER BY
"5-ki" desc,"4-ki" desc,"3-ki" desc,"2-ki" desc,"1-ki" desc;
Y3JlYXRlIHRhYmxlIHRibCh1Y3plbiB2YXJjaGFyKDIwKSwgb2NlbmEgaW50ZWdlcik7Cmluc2VydCBpbnRvIHRibCB2YWx1ZXMoJ0pBTkVLJyw1KTsKaW5zZXJ0IGludG8gdGJsIHZhbHVlcygnSkFORUsnLDUpOwppbnNlcnQgaW50byB0YmwgdmFsdWVzKCdKQU5FSycsNCk7Cmluc2VydCBpbnRvIHRibCB2YWx1ZXMoJ0pBTkVLJyw0KTsKaW5zZXJ0IGludG8gdGJsIHZhbHVlcygnQU5JQScsNSk7Cmluc2VydCBpbnRvIHRibCB2YWx1ZXMoJ0FOSUEnLDQpOwoKU0VMRUNUCiAgICB1Y3plbiwgIAogICAgU1VNKG9jZW5hID0gMSkgQVMgIjEta2kiLAogICAgU1VNKG9jZW5hID0gMikgQVMgIjIta2kiLAogICAgU1VNKG9jZW5hID0gMykgQVMgIjMta2kiLAogICAgU1VNKG9jZW5hID0gNCkgQVMgIjQta2kiLAogICAgU1VNKG9jZW5hID0gNSkgQVMgIjUta2kiCkZST00gCiAgICB0YmwKR1JPVVAgQlkgCiAgICB1Y3plbgpPUkRFUiBCWQogICAgIjUta2kiIGRlc2MsIjQta2kiIGRlc2MsIjMta2kiIGRlc2MsIjIta2kiIGRlc2MsIjEta2kiIGRlc2M7