472,333 Members | 1,092 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,333 developers and data experts.

Using Subqueries - 1

debasisdas
8,127 Expert 4TB
Using Subqueries
==================

The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement.

The sub query executes once before the main query.
The result of the sub query is used by the main query (outer Query).

You can place the sub query in a number of SQL clauses.
WHERE clause
HAVING clause
FROM clause (Inline sub query/inline view)

1.THE NESTED QUERY MUST RETURN A SINGLE COLUMN.
2.THE BETWEEN OPERATOR CAN'T BE USED IN SUBQUERIES.
3.THE RESULT CAN ONLY CONTAIN COLUMNS FROM THE TABLES REFERENCED IN THE OUTER MOST QUERIES.

Types of sub query
---------------------------
Single -row sub queries: -
Queries that return only one row from the inner SELECT statement.

Multiple -row sub queries: -
Return more than one row from the inner SELECT statement.

Note:
---------
Comparison operators fall into two classes. :
Single - row operators (>, >=, =, <>, <, <=)
Multiple - row operators (IN, ANY, SOME, ALL)

Syntax
---------
SELECT select_list FROM table
WHERE expr operator (SELECT select_list FROM table);

**Do not add an ORDER BY clause to a sub query.
*Use single-row operators with single-row sub queries.
*Use multiple-row operators with multiple-row sub queries.

Sample example of Single row sub query: -
=================================

Example #1
Who earn more than empno 7566
Expand|Select|Wrap|Line Numbers
  1. SELECT *  FROM emp WHERE sal >(SELECT sal FROM emp WHERE empno=7566);
  2.  
Example #2
Display details of employee whose job is equal to 7369

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM emp
  2. WHERE job= (SELECT job FROM emp WHERE empno=7369);
  3.  
Example #3

Displays employees whose job title is the same as that of employee 7369 and whose salary is greater than that of employee 7876.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM emp WHERE job= (SELECT job FROM emp
  2. WHERE empno=7369)  AND Sal > ( SELECT Sal FROM emp WHERE  empno=7876);
  3.  
USING GROUP FUNCTION in sub-queries
=================================
Example #1
--------------------
Display details of all employees whose salary is equal to the minimum salary.

Expand|Select|Wrap|Line Numbers
  1.  SELECT * FROM emp
  2. WHERE sal = (SELECT MIN(sal) FROM emp);
  3.  
Exp:-The MIN group function returns a single value(800) to the outer query.
display data from a main query by using a group function in a subquery to return a single row.

Using HAVING clause with subqueries***************
The Oracle Server returns results into the main query's HAVING clause.

Example #2
------------------------
Display all the departments that have a minimum salary greater than that of department 20's MIN(Sal);
Expand|Select|Wrap|Line Numbers
  1. SELECT deptno, MIN(sal) FROM emp GROUP BY deptno HAVING MIN(sal) >
  2. ( SELECT MIN(sal) FROM emp WHERE deptno=20);
  3.  
deptno=20's MIN sal is 800.
i.e. subquery is 800
main query compares MIN(sal) > 800;

Example #3
----------------------
Find the job with the lowest avg salary.
Expand|Select|Wrap|Line Numbers
  1. SELECT job, AVG(sal) FROM emp GROUP BY job
  2. HAVING AVG(sal) = (SELECT  MIN(AVG(sal)) FROM emp GROUP BY job);
  3.  
Also Check Using Subqueries - 2
Sep 17 '07 #1
0 5527

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: pete | last post by:
Been banging my head against the wall with subqueries. Even simple stuff like this fails: SELECT CompanyName FROM tblcompanies WHERE CompanyName...
5
by: Nick | last post by:
Im moving a development app (MySQL 5.0) to a different server which runs MySQL 4.0.20-standard. I am getting errors on queries that have subqueries...
2
by: Kevin | last post by:
While converting SQL statements for a database change, I discovered a big performance hit in MYSQL with subqueries vices Sybase. I'm hoping that...
11
by: SQL_developer | last post by:
Hello, I am trying to update a column in a table with an 8 digit string. The 8 digit number is obtained from another column which is a comments...
2
by: psuaudi | last post by:
I have a main query that I would like to call two different subqueries. In MS Access, I usually just save the two subqueries as separate queries...
4
by: muzu1232004 | last post by:
Can anyone explain me when we use correlated subqueries rather than nested subqueries. Do all the correlated subqueries can be written in nested...
1
debasisdas
by: debasisdas | last post by:
Using Co-related sub query ======================== While a subquery is evaluated only once for each table, a correlated subquery is evaluated...
0
MMcCarthy
by: MMcCarthy | last post by:
Rather than using the Access design view change the view to SQL. I am going to attempt to outline the general syntax used for SQL queries in Access....
1
by: lizandra | last post by:
Greetings, I am a newbie, I have been working to extract data from a basic sales db and trying to decide when I should use joins and when I should...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.