You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SQL中查询小于表中最大薪资的记录,解决ORA-00933报错

报错原因

  • ORA-00933报错的核心是Oracle语法不支持表别名使用AS关键字,AS仅可用于列别名定义,你原有SQL中instructor as T、instructor as S的写法不符合Oracle语法规范,直接去掉AS即可解决报错。

原有SQL修正版

select distinct T.salary
from instructor T, instructor S
where T.salary < S.salary

该写法通过自连接逻辑匹配所有存在更高薪资的记录,去重后即可得到所有小于最大薪资的薪资数据,逻辑符合你的需求。

更优实现方案

自连接写法在数据量较大时会产生笛卡尔积,执行效率较低,推荐使用更简洁的子查询或窗口函数实现:

方案1:聚合函数子查询(最简洁)

select distinct salary
from instructor
where salary < (select max(salary) from instructor)

直接先查整个表的最高薪资,再过滤所有小于该值的薪资即可。

方案2:窗口函数实现

select distinct salary
from (
    select salary, max(salary) over() as max_salary
    from instructor
) temp
where salary < max_salary

如果后续需要同时展示其他字段,窗口函数的拓展性更强。


内容的提问来源于stack exchange,提问作者AshenPI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 13:39:04