隨筆 - 3  文章 - 0  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          •  

          積分與排名

          • 積分 - 1611
          • 排名 - 4224

          最新評論

          閱讀排行榜

          評論排行榜

          What is a subquery?

          A subquery is a query within a query. In Oracle, you can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.


          WHERE clause

          Most often, the subquery will be found in the WHERE clause. These subqueries are also called nested subqueries.

          For example:

          select * from all_tables tabs
          where tabs.table_name in (select cols.table_name
           from all_tab_columns cols
           where cols.column_name = 'SUPPLIER_ID');

          Limitations: Oracle allows up to 255 levels of subqueries in the WHERE clause.


          FROM clause

          A subquery can also be found in the FROM clause. These are called inline views.

          For example:

          select suppliers.name, subquery1.total_amt
          from suppliers,
             (select supplier_id, Sum(orders.amount) as total_amt
             from orders
             group by supplier_id) subquery1,
          where subquery1.supplier_id = suppliers.supplier_id;

          In this example, we've created a subquery in the FROM clause as follows:

          (select supplier_id, Sum(orders.amount) as total_amt
           from orders
           group by supplier_id) subquery1

          This subquery has been aliased with the name subquery1. This will be the name used to reference this subquery or any of its fields.

          Limitations: Oracle allows an unlimited number of subqueries in the FROM clause.


          SELECT clause

          A subquery can also be found in the SELECT clause.

          For example:

          select tbls.owner, tbls.table_name,
            (select count(column_name) as total_columns
             from all_tab_columns cols
             where cols.owner = tbls.owner
             and cols.table_name = tbls.table_name) subquery2
          from all_tables tbls;

          In this example, we've created a subquery in the SELECT clause as follows:

          (select count(column_name) as total_columns
           from all_tab_columns cols
           where cols.owner = tbls.owner
           and cols.table_name = tbls.table_name) subquery2

          The subquery has been aliased with the name subquery2. This will be the name used to reference this subquery or any of its fields.

          The trick to placing a subquery in the select clause is that the subquery must return a single value. This is why an aggregate function such as SUM, COUNT, MIN, or MAX is commonly used in the subquery

          posted on 2007-08-14 12:19 y 閱讀(212) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 大姚县| 阿拉善盟| 玉山县| 临夏县| 修文县| 东乌珠穆沁旗| 贺州市| 乌拉特中旗| 哈密市| 枝江市| 西和县| 吉安县| 尤溪县| 金寨县| 长泰县| 明溪县| 吉林市| 岢岚县| 长宁区| 台山市| 嵊泗县| 双江| 罗江县| 团风县| 左云县| 阿坝县| 遂昌县| 马边| 铜鼓县| 聂荣县| 上虞市| 武隆县| 寿阳县| 肥城市| 富顺县| 酉阳| 岳西县| 钦州市| 会宁县| 宁波市| 华安县|