【问题标题】:Using ANY/ALL in SQL to show highest entry in a table在 SQL 中使用 ANY/ALL 显示表中的最高条目
【发布时间】:2017-01-17 22:47:04
【问题描述】:

我正在为考试做练习并坚持这个问题:

编写一条 SQL 语句来查找哪个客户名称具有 所有余额中的最高余额。 (您必须使用 ALL 或 ANY)。

Deposit中的一个示例(也是最高余额)行

INSERT INTO Deposit(customerName, branchName, accountNumber, balance)      
VALUES ('Jones', 'HFE', '42', '4100.00');

这是我尝试过的:

SELECT customerName
FROM Deposit
WHERE balance > ALL
(SELECT balance
FROM Deposit
WHERE balance >= balance);

如何解决此问题以显示余额最高的客户名称?

谢谢

【问题讨论】:

    标签: sql oracle correlated-subquery


    【解决方案1】:

    假设你有这样的数据:

    create table deposit ( customerName, balance) as (
        select 'cust1', 100 from dual UNION ALL
        select 'cust2', 500 from dual UNION ALL
        select 'cust3', 500 from dual UNION ALL
        select 'cust4', 400 from dual UNION ALL
        select 'cust5', 400 from dual
    )
    

    你需要:

    select customerName
    from deposit
    where balance >= ALL ( select balance from deposit)
    

    给出:

    cust2
    cust3
    

    你的代码出了什么问题:

    • 你有一个无用的 where 条件:这个条件不仅简单地检查一个列本身,而且它是无用的,因为你只是希望得到的 customerName 的余额大于每个人的余额,没有条件李>
    • 你有一个严格的> 而不是>=,因此不匹配任何值

    【讨论】:

    • 太棒了,谢谢我看到了我的错误以及如何纠正它们!
    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多