OCP-1Z0-051 第142题 嵌套子查询的应用

该博客探讨了Oracle OCP-1Z0-051考试中的一道关于嵌套子查询的问题。查询涉及在PRODUCTS表中找出价格第二高的产品名称。博客详细解析了子查询的步骤,首先找到最高价格,然后找出次高价,最终获取相应产品的名称。正确答案是显示价格第二高的产品名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、原题
View the Exhibit and examine the structure of the PRODUCTS table.

Evaluate the following query:
SQL> SELECT prod_name
       FROM products
      WHERE prod_id IN
            (SELECT prod_id
               FROM products
              WHERE prod_list_price =
                    (SELECT MAX(prod_list_price)
                       FROM products
                      WHERE prod_list_price <
                            (SELECT MAX(prod_list_price) FROM products)));
What would be the outcome of executing the above SQL statement?
A. It produces an error.
B. It shows the names of all products in the table.
C. It shows the names of products whose list price is the second highest in the table.
D. It shows the names of all products whose list price is less than the maximum list price.

答案:C

二、题目翻译
看下面PRODUCTS表的结构:
评估下面的语句
上面的SQL语句执行后会出现什么结果?
A.报错。
B.显示所有产品的名称。
C.显示表中价格第二高的产品名称。
D.显示所有小于最高价格的产品名称。

三、题目解析
先求出最里层的子查询SELECT MAX(prod_list_price) FROM products,得出的是最高的产品价格;
紧接着外面的子查询,
SELECT MAX(prod_list_price)
   FROM products
 WHERE prod_list_price < (SELECT MAX(prod_list_price) FROM products)
求出的是小于最大价格的最大价格,也就是第二高的价格;
然后再求出这个产品的ID,进一步再求出产品的名称。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值