Nested Queries and Join Queries: Facilities Required and Procedure A) Facilities Required To Do The Experiment
Nested Queries and Join Queries: Facilities Required and Procedure A) Facilities Required To Do The Experiment
no.
1
Here more than one sub query is used. These multiple sub queries are combined by
means of and & or keywords
3. Correlated sub query
A sub query is evaluated once for the entire parent statement whereas a correlated
Sub query is evaluated once per row processed by the parent statement.
Relating Data through Join Concept
The purpose of a join concept is to combine data spread across tables. A join is
actually performed by the where clause which combines specified rows of tables.
Syntax; select columns from table1, table2 where logical expression;
Types of Joins 1. Simple Join 2. Self Join 3. Outer Join 4. Inner Join
1. Simple Join
a) Equi-join: A join, which is based on equalities, is called equi-join.
b) Non Equi-join: It specifies the relationship between
Table Aliases
Table aliases are used to make multiple table queries shorted and more readable. We
give an alias name to the table in the from clause and use it instead of the name
throughout the query.
Self join: Joining of a table to itself is known as self-join. It joins one row in a table
to another. It can compare each row of the table to itself and also with other rows of
the same table.
Outer Join: It extends the result of a simple join. An outer join returns all the rows
returned by simple join as well as those rows from one table that do not match any
row from the table. The symbol (+) represents outer join.
Inner join: Inner join returns the matching rows from the tables that are being
joined
24
Dr.N.N.C.E
CSE&IT / IV Sem
DBMS Lab - LM
c) SQL Commands:
Nested Queries:
Example: select ename, eno, address where salary >(select salary from employee where
ename =jones);
1.Subqueries that return several values
Example: select ename, eno, from employee where salary <any (select salary
from employee where deptno =10);
3.Correlated subquery
Example: select * from emp x where x.salary > (select avg(salary) from emp where deptno
=x.deptno);
Simple Join
a) Equi-join
Example: select * from item, cust where
item.id=cust.id; b) Non Equi-join
Example: select * from item, cust where item.id<cust.id;
Self join
Example: select * from emp x ,emp y where x.salary >= (select avg(salary) from x.emp where
x. deptno =y.deptno);
Outer Join
Example: select ename, job, dname from emp, dept where emp.deptno (+) = dept.deptno;
d) Queries:
Q1: Display all employee names and salary whose salary is greater than minimum salary of
the company and job title starts with M.
Solution:
1. Use select from clause.
2. Use like operator to match job and in select clause to get the result.
Ans:
SQL> select ename,sal from emp where sal>(select min(sal) from emp where job like 'A%');
ENAME
SAL
-------------------- ---------Arjun
12000
Gugan
20000
Karthik
15000
Q2: Issue a query to find all the employees who work in the same job as Arjun.
Ans:
SQL> select * from emp;
EMPNO ENAME
JOB
DEPTNO
SAL
---------- -------------------- ---------- ---------- ---------1 Mathi
AP
1 10000
2 Arjun
ASP
2 12000
3 Gugan
ASP
2 20000
4 Karthik
AP
1 15000
SQL> select ename from emp where job=(select job from emp where ename='Arjun');
ENAME
-------------Arjun
Gugan
25
Dr.N.N.C.E
CSE&IT / IV Sem
DBMS Lab - LM
Q3: Issue a query to display information about employees who earn more than any
employee in dept 1.
Ans:
SQL> select * from emp where sal>(select max(sal) from emp where empno=1);
EMPNO ENAME
JOB
DEPTNO
---------- -------------------- ---------- ---------- ---------2 Arjun
ASP
2 12000
3 Gugan
ASP
2
20000
4 Karthik
AP
1 15000
JOINS
SAL
Tables used
SQL> select * from emp;
EMPNO ENAME
JOB
DEPTNO
---------- -------------------- ---------- ---------- ---------1 Mathi
AP
1
10000
2 Arjun
ASP
2 12000
3 Gugan
ASP
2
20000
4 Karthik
AP
1 15000
SAL
26
Dr.N.N.C.E
CSE&IT / IV Sem
DBMS Lab - LM
Ans:
SQL> select * from emp,dept where emp.deptno!=dept.deptno;
EMPNO ENAME JOB
DEPTNO
SAL DEPTNO DNAME
LOC
---------- -------------------- ---------- ---------- ---------- ------------------------ ------------2 Arjun
ASP
2
12000
1 ACCOUNTING NEW YORK
3 Gugan
ASP
2
20000
1 ACCOUNTING NEW YORK
1 Mathi
AP
1
10000
2 RESEARCH
DALLAS
EMPNO ENAME
JOB
DEPTNO
SAL DEPTNO DNAME
---------- -------------------- ---------- ---------- ---------- ---------- -------------- ------------4 Karthik
AP
1 15000
2 RESEARCH
DALLAS
1 Mathi
AP
1
10000
30 SALES
CHICAGO
2 Arjun
ASP
2
12000
30 SALES
CHICAGO
EMPNO ENAME
JOB
DEPTNO
SAL DEPTNO DNAME
---------- -------------------- ---------- ---------- ---------- ---------- -------------- ------------3 Gugan
ASP
2
20000
30 SALES
CHICAGO
4 Karthik
AP
1 15000
30 SALES
CHICAGO
1 Mathi
AP
1 10000
40 OPERATIONS BOSTON
EMPNO ENAME
JOB
DEPTNO
SAL DEPTNO DNAME
---------- -------------------- ---------- ---------- ---------- ---------- -------------- ------------2 Arjun
ASP
2
12000
40 OPERATIONS BOSTON
3 Gugan
ASP
2
20000
40 OPERATIONS BOSTON
4 Karthik
AP
1 15000
40 OPERATIONS BOSTON
12 rows selected.
LOC
LOC
LOC
LEFTOUT-JOIN
Tables used
SQL> select * from stud1;
Regno Name
Mark2
Mark3
Result
---------- ----------- ---------- ---------- --------------------------------------101
john
89
80
pass
102 Raja
70
80
pass
103 Sharin
70
90
pass
104
sam
90
95
pass
SQL> select * from stud2;
NAME
GRA
----------- ---------john
s
raj
s
sam
a
sharin
Q6: Display the Student name and grade by implementing a left outer join.
Ans: SQL> select stud1.name,grade from stud1 left outer join stud2 on stud1.name=stud2.name;
Name
Gra
----------- ---------john
s
raj
s
sam
a
sharin
a
smith
null
27
Dr.N.N.C.E
CSE&IT / IV Sem
DBMS Lab - LM
RIGHTOUTER-JOIN
Q7: Display the Student name, register no, and result by implementing a right outer join.
Ans:
SQL> select stud1.name, regno, result from stud1 right outer join stud2 on stud1.name
= stud2.name;
Name
Regno Result
----------- ---------- -------------------------john
101
pass
raj
102
pass
sam
103
pass
sharin
104
pass
Rollno Name
Mark1 Mark2Total
---------- ---------- ---------- ---------- ---------1 sindu
90
95
185
2 arul
90
90 180
FULLOUTER-JOIN
Q8: Display the Student name register no by implementing a full outer join.
Ans:
SQL> select stud1.name, regno from stud1 full outer join stud2 on (stud1.name= stud2.name);
Name
Regno
----------- ---------john
101
raj
102
sam
103
sharin
104
SELFJOIN
Q9: Write a query to display their employee names
Ans:
SQL> select distinct ename from emp x, dept y where x.deptno=y.deptno;
ENAME
-------------------Arjun
Gugan
Karthik
Mathi
Q10: Display the details of those who draw the salary greater than the average salary.
Ans:
SQL> select distinct * from emp x where x.sal >= (select avg(sal) from emp);
EMPNO ENAME
JOB
DEPTNO SAL
---------- -------------------- ---------- ---------- ---------3 Gugan
ASP
2 20000
4 Karthik
AP
1
15000
11 kavitha
designer
12
17000
e) Result:
Thus the nested Queries and join Queries was performed successfully and executed.