Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 


Scalar subqueries tips

Oracle Tips by Burleson Consulting

May 31, 2015


Scalar subqueries are a powerful enhancement to Oracle9i SQL. They allow for quick formulation of extremely complex SQL statements. Oracle's introduction of scalar subquery support is another example of the company's commitment to keeping pace with the evolution of the SQL language.

Scalar subquery examples

Once you become acquainted with the syntax, you'll find scalar subqueries to be very powerful. Scalar subqueries are especially useful for combining multiple queries into a single query. In Listing C, we use scalar subqueries to compute several different types of aggregations (max and avg) all in the same SQL statement. Note that this query uses both scalar subqueries and in-line views.

select
   (select max(salary) from emp)       highest_salary,
   emp_name                            employee_name,
   (select avg(bonus) from commission) avg_comission,
   dept_name
from
   emp,
   (select dept_name from dept where dept = 'finance');
 
Scalar subqueries are also handy for inserting into tables, based on values from other tables. In Listing D, we use a scalar subquery to compute the maximum credit for BILL and insert this value into a max_credit table.
insert into
   max_credit
(
   name,
   max_credit
)
values
(
   ?Bill?,
   select max(credit) from credit_table where name = ?BILL?
);

The scalar subquery in Listing D is quite useful for Oracle data warehouse applications. In an Oracle data warehouse, it's common for the DBA to pre-aggregate values to speed up query execution, and scalar subqueries are a powerful helper in aggregation. In Listing E, we populate an emp_salary_summary table with many types of aggregate values from the base tables.

insert into
   emp_salary_summary
(
   sum_salaries
   max_salary,
   min_salary,
   avg_salary,
values
(
   (select sum(salary) from emp),
   (select max(salary) from emp),
   (select min(salary) from emp),
   (select avg(salary) from emp)
;


Restrictions on scalar subqueries

Scalar subqueries are restricted to returning a single value because they select a finite value. Scalar subqueries could be used in previous versions of Oracle in some parts of a SQL statement, but Oracle9i extends their use to almost any place where an expression can be used, including:
 

  • CASE expressions
  • SELECT statements
  • VALUES clauses of INSERT statements
  • WHERE clauses
  • ORDER BY clauses
  • Parameters of a function
There are also important restrictions on scalar subqueries. Scalar subqueries can?t be used for:
 
  • Default values for columns
  • RETURNING clauses
  • Hash expressions for clusters
  • Functional index expressions
  • CHECK constraints on columns
  • WHEN condition of triggers
  • GROUP BY and HAVING clauses
  • START WITH and CONNECT BY clauses
 
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.

 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational

 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster