473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Using Subqueries - 2

debasisdas
8,127 Expert 4TB
Using Co-related sub query
========================
While a subquery is evaluated only once for each table, a correlated subquery is evaluated once for each row.

Sub query can take value from outer query.

Ex#1
---------
Delete duplicate record from table
Expand|Select|Wrap|Line Numbers
  1. Delete from emp e  where rowid>(select min(rowid) from emp where e.empno=empno)
  2.  
OR
Expand|Select|Wrap|Line Numbers
  1. Delete from emp 
  2. where rowid not in(select max(rowid) from emp group  by empno)
  3.  
Ex#2
------------
Display first position of employee
Expand|Select|Wrap|Line Numbers
  1. select * from emp e where 0=(select count(distinct(sal)) from emp
  2. where sal>e.sal) order by sal desc
  3.  
Ex#3
------------
Display details of employee whose sal greater than avg sal of it own department
Expand|Select|Wrap|Line Numbers
  1. Select * from emp e
  2. Where sal>(select avg(sal) from emp where deptno=e.deptno)
  3.  
Multiple - Row Subqueries
=======================
Inner query returns more than one row
Use multiple - row comparison operators
IN ----Equal to any member in the list
ANY--Compare value to each value returned by the sub query.
ALL---Compare value to every value returned by the sub query.
<ANY means less than the maximum
>ANY means more than the minimum
=ANY is equivalent to IN
>ALL means more than the maximum
<ALL means less than minimum.

The NOT operator can be used with IN, ANY, and ALL operators

Using IN
==========
Example #1
-------------------
Find the employees who earn the same salary as the minimum salary for departments.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM emp
  2. WHERE sal IN ( SELECT MIN(sal) FROM emp GROUP BY deptno);
  3.  
  4. is  same as
  5.  
  6. SELECT * FROM emp WHERE sal IN (800, 950, 1300);
  7.  
Using ANY
=============
The ANY operator (and its synonym SOME operator) compares a value to each value returned by a sub query.

Example #2
--------------------------
Display employees whose salary is less than any clerk and who are not clerks.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM emp  WHERE sal < ANY ( SELECT sal FROM  emp WHERE job= 'CLERK')  AND job <> 'CLERK';
  2.  
Using ALL
============
The ALL operator compares a value to every value returned by a subquery.

Example #3
--------------------------
Display details of employee whose sal greater than avg sal of all department
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM emp WHERE sal > ALL(SELECT avg(sal)  FROM emp GROUP BY deptno);
  2.  
The example above displays employees whose salary is greater than the average salaries of all the department. The highest average salary of a department is 2916.66 , so the query returns those employees whose salary is greater than 2916.66

Note :--Please Try on Scott Schema

Using In line sub query(inline views)
==============================
Ex#1
----------
Display Odd number records.
Expand|Select|Wrap|Line Numbers
  1. SELECT  * FROM  ( SELECT rownum rn, empno, ename FROM emp)  temp
  2.  WHERE  MOD(temp.rn,2) = 1
  3.  
Ex#2
----------
Display manager and its salary
Expand|Select|Wrap|Line Numbers
  1. SELECT A.MGR, B.SAL FROM (SELECT DISTINCT  MGR FROM EMP
  2.  WHERE MGR IS NOT NULL) A   LEFT OUTER JOIN 
  3.  (SELECT EMPNO, SAL FROM EMP) B  ON  A.MGR=B.EMPNO
  4.  
Ex#3
----------
Top three employee who earn hightest salary.
Expand|Select|Wrap|Line Numbers
  1. select * from (select  * from emp order by sal where rownum<4
  2.  
Ex#4
----------
Query for listing Deptno, ename, sal, SUM(sal in that dept) :
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT deptno, ename, sal, (SELECT SUM(sal) FROM emp WHERE a.deptno =deptno) FROM emp a  ORDER BY deptno
  3.  
Ex#5
----------
Display manager no and name who has more employee

Expand|Select|Wrap|Line Numbers
  1. select empno,ename from emp where empno=(select mgr from 
  2. (select mgr,count(empno) from emp  where mgr is not null group by mgr
  3. order by count(empno) desc) where rownum<2)
  4.  
Sep 17 '07 #1
1 11413
Hi Debasis,

Can you please tell me how to find the employees with greater than and less than the average salaries for their concerned departments? Thanks for your time.
Feb 23 '14 #2

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 IN (SELECT HostName FROM tblhosts) Am I...
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 such as... SELECT id FROM table1 WHERE id IN...
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 someone might be able to help me understand why? ...
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 field. e.g. Comments Field :
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 which are then called by a third separate and main...
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 subqueries form as well ? What are the major...
0
debasisdas
by: debasisdas | last post by:
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...
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. Angle brackets <> are used in place of some...
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 use subqueries. Much of what I read online says...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.