SUB QUERIES
-VARSHA KUMARI
Sub Queries
 In nested queries, a query is written inside a query.
 The result of inner query(sub query) is used in
execution of outer query(main query).
 Subqueries can be used with the SELECT,
INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN,
BETWEEN, etc.
syntax
 SELECT column1,column2,..
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column FROM table1 [, table2
] [WHERE])
[ ] bracket means it is optional
Example – employee_table
E_id E_name Dept Salary
1 Ram HR 10000
2 Amit MRKT 20000
3 Ravi HR 30000
4 Nitin MRKT 40000
5 Hardik IT 50000
query
 Display the maximum salary from employee_ table
 Select max(salary) from employee_ table
 Display the name of employee having maximum
salary from employee_ table
 Select Ename from employee_ table where salary =
(Select max(salary) from employee_ table)


= 50000 false
= 50000 false
= 50000 false
= 50000 false
= 50000 True
Example :
Table creation
 Q. Find the name of the employees whose salary is
equal to salary of Arjun & Harthik?
SQL :
 select Emp_name from emp2 where salary
 IN
 (select salary from emp2 where Emp_name in
('Arjun', 'Harthik'));
 Q. Find the name of the employees whose belongs
to new york?

SQL
 select emp_name from emp2 where deptno
 IN
 (select deptno from dept where loc = 'NEW
YORK');
 Q. Find the name of the employees whose does not
belongs to ‘london’?
 select emp_name from emp2 where deptno
 NOT IN
 (select deptno from dept where loc
= 'LONDON');
 Q. Find the name of the employee who gets salary
greater than the average salary?
 select emp_name,salary from emp2 where salary
 >
 (select round(avg(salary),2) from emp2);
 Q. Find the department name and location of the
employee whose name is Aman?
 select dname,loc from dept where deptno =
 (select deptno from emp2 where emp_name =
'Aman');
 Q. Display all employee names and salary whose
salary is greater than minimum salary of the
company and job title starts with ‘D‘.
 Select Emp_name, Salary from emp2 where
Salary>(select min(Salary) from emp2 where job
like 'D%');
 Q write a query to find all the employees who
work in the same job as Arjun.
 Select Emp_name from emp2 where Job = (select
Job from emp2 where Emp_name='Aman');
 Issue a query to display information about
employees who earn more than any employee in
dept 1
 Select * from emp where Salary>(select
max(Salary) from emp where Emp_no=1);
Question
 Q. Write a query to display the name of employee
having 2nd maximum salary.
 Q. Write a query to display the name of employee
having 2nd minimum salary.
 Q. Write a query to display the name of employee
having salary less than the average salary.

Sub queries

  • 1.
  • 2.
    Sub Queries  Innested queries, a query is written inside a query.  The result of inner query(sub query) is used in execution of outer query(main query).  Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
  • 3.
    syntax  SELECT column1,column2,.. FROMtable1 [, table2 ] WHERE column_name OPERATOR (SELECT column FROM table1 [, table2 ] [WHERE]) [ ] bracket means it is optional
  • 4.
    Example – employee_table E_idE_name Dept Salary 1 Ram HR 10000 2 Amit MRKT 20000 3 Ravi HR 30000 4 Nitin MRKT 40000 5 Hardik IT 50000
  • 5.
    query  Display themaximum salary from employee_ table  Select max(salary) from employee_ table  Display the name of employee having maximum salary from employee_ table  Select Ename from employee_ table where salary = (Select max(salary) from employee_ table)
  • 6.
      = 50000 false =50000 false = 50000 false = 50000 false = 50000 True
  • 7.
  • 8.
  • 9.
     Q. Findthe name of the employees whose salary is equal to salary of Arjun & Harthik?
  • 10.
    SQL :  selectEmp_name from emp2 where salary  IN  (select salary from emp2 where Emp_name in ('Arjun', 'Harthik'));
  • 11.
     Q. Findthe name of the employees whose belongs to new york? 
  • 12.
    SQL  select emp_namefrom emp2 where deptno  IN  (select deptno from dept where loc = 'NEW YORK');
  • 13.
     Q. Findthe name of the employees whose does not belongs to ‘london’?
  • 14.
     select emp_namefrom emp2 where deptno  NOT IN  (select deptno from dept where loc = 'LONDON');
  • 15.
     Q. Findthe name of the employee who gets salary greater than the average salary?
  • 16.
     select emp_name,salaryfrom emp2 where salary  >  (select round(avg(salary),2) from emp2);
  • 17.
     Q. Findthe department name and location of the employee whose name is Aman?
  • 18.
     select dname,locfrom dept where deptno =  (select deptno from emp2 where emp_name = 'Aman');
  • 19.
     Q. Displayall employee names and salary whose salary is greater than minimum salary of the company and job title starts with ‘D‘.
  • 20.
     Select Emp_name,Salary from emp2 where Salary>(select min(Salary) from emp2 where job like 'D%');
  • 21.
     Q writea query to find all the employees who work in the same job as Arjun.
  • 22.
     Select Emp_namefrom emp2 where Job = (select Job from emp2 where Emp_name='Aman');
  • 23.
     Issue aquery to display information about employees who earn more than any employee in dept 1
  • 24.
     Select *from emp where Salary>(select max(Salary) from emp where Emp_no=1);
  • 25.
    Question  Q. Writea query to display the name of employee having 2nd maximum salary.  Q. Write a query to display the name of employee having 2nd minimum salary.  Q. Write a query to display the name of employee having salary less than the average salary.