数据库相同成绩排名例子
数据库相同成绩排名例子
create table score1(
s_id number,
s_score decimal(5,2) --表示最大5位的数字,其中小数点后面两位小数
)
insert into score1 (s_id, s_score) values ('1', '3.5');
insert into score1 (s_id, s_score) values ('2', '3.65');
insert into score1 (s_id, s_score) values ('3', '4.0');
insert into score1 (s_id, s_score) values ('4', '3.85');
insert into score1 (s_id, s_score) values ('5', '4.0');
insert into score1 (s_id, s_score) values ('6', '3.65');
select *from score1
select s_score,(select count(distinct s_score)from score1 where s_score>=s.s_score)排名 from score1 s order by s_score desc
select count(distinct s_score)from score1
查询表中排名前三的数据
Create table Employee (Id int, Name varchar(255), Salary int, DepartmentId int);
Create table Department (Id int, Name varchar(255));
select *from employee
insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '70000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2');
insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2');
insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('5', 'Janet', '69000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('6', 'Randy', '85000', '1');
insert into Department (Id, Name) values ('1', 'IT');
insert into Department (Id, Name) values ('2', 'Sales');
select *from Department
select d.Name as Department,e1.Name as Employee,e1.Salary
from Employee e1 join
Department d
on d.Id=e1.DepartmentId
where 3>
(select count(distinct e2.Salary)from Employee e2 where e2.Salary>e1.Salary
and e1.departmentid=e2.departmentid)