Sub Queries
Sub Queries
where deptid= (Select deptid from employee where empid = 101) List all employees who have the same name as employee 101 Select * from employee where name = (Select name from employee where empid = 101)
List all employees who joined before employee 103 Select * from employee where hiredate < (Select hiredate from employee where empid = 103)
List all employees who joined after employee 103 Select * from employee where hiredate > (Select hiredate from employee where empid = 103) List all employees who did not join on the same day as employee 103 Select * from employee where hiredate <> (Select hiredate from employee where empid = 103) List all employees who do not share the department with employee 105 Select * from employee where deptid <> (Select deptid from employee where empid = 105) List all employees who share the department with employee 101 or 105 Select * from employee where deptid not in (Select deptid from employee where empid in (101,105))
List all employees who do not share the department with employee 101 or 105 Select * from employee where deptid not in (Select deptid from employee where empid in (101,105)) List all employee names who earn less than the average salary Select ename from employee where sal <
(Select avg(sal) from employee) List all employee names who earn less than any manager Select ename from employee where sal < any (Select sal from employee where empid = mgrid) List all employee names who earn equal to any manager but is not a manager Select ename from employee where empid <> mgrid and sal in (Select sal from employee where empid = mgrid) List all enames who do not earn the same as employee 102 or 104 Select ename from employee where sal not in ( select sal from employee where empid in (102,104))
List all employees who are in the same department as 105 but junior in service Select * from employee where deptid = (Select deptid from employee where empid =105) And hiredate > (select hiredate from employee where empid = 105) List all managers who are not employed in the same year as manager 108 Select * from employee where empid = mgrid and datepart(yy,hiredate) <> select (datepart(yy,hiredate where empid = 108)
List all managers who are not employed in the same month as manager 108 (irrespective of year) Select * from employee where empid = mgrid and datepart(mm,hiredate) = select (datepart(mm,hiredate where empid = 108)
Joins List all managers (using self join) Select ename from employee e join employee m on e.empid = m.empid List all employee names and their locations of work Select e.ename,d.locn from employee e join department d on e.deptid = d.deptid List all department names along with employeee names(if any) in that department (left join) Select dname,ename from department left join employee on department.deptid = employee.deptid
List all department names along with employeee names(if any) in that department (right join) Select dname,ename from employee right join department on employee.deptid = department.deptid Union ,intersect,except List all employee names of department 10 and 20 (using union excluding repetition of names) Select ename from employee where deptid = 10 union Select ename from employee where deptid = 20 List all employee names of department 10 and 20 (using union including repetition of names) Select ename from employee where deptid = 10 Union all Select ename from employee where deptid = 20 List all job titles applicable to department 10 and 20 (using union excluding repetition of job title) Select job from employee where deptid = 10 Union all Select job from employee where deptid = 20 List all job titles common to department 10 and 20 (using intersect) Select job from employee where deptid = 10 intersect Select job from employee where deptid = 20 List all job titles of department 10 but not found in department 20 (using except) Select job from employee where deptid = 10 except Select job from employee where deptid = 20