Você está na página 1de 6

NESTED QUERIES AND JOIN QUERIES

AIM To execute and verify the SQL commands for Nested and Join Queries. NESTED QUERIES Subquery or Inner query or Nested query is a query in a query. Nested Query can have more than one level of nesting in one single query. A SQL nested query is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE SQL query. Subqueries with the SELECT Statement: Subqueries are most frequently used with the SELECT statement. The basic syntax is as follows: SELECT column_name [, column_name ] FROM table1 [, table2 ] WHERE column_name OPERATOR FROM table1 [, table2 ] Subqueries with the INSERT Statement: Subqueries also can be used with INSERT statements. The INSERT statement uses the data returned from the subquery to insert into another table. The selected data in the subquery can be modified with any of the character, date, or number functions. The basic syntax is as follows: INSERT INTO table_name [ (column1 [, column2 ]) ] SELECT [ *|column1 [, column2 ] FROM table1 [, table2 ] [ WHERE VALUE OPERATOR ] Subqueries with the UPDATE Statement: The subquery can be used in conjunction with the UPDATE statement. Either single or multiple columns in a table can be updated when using a subquery with the UPDATE statement. The basic syntax is as follows: UPDATE table SET column_name = new_value [ WHERE OPERATOR [ VALUE ] (SELECT COLUMN_NAME FROM TABLE_NAME) [ WHERE) ] (SELECT column_name [, column_name ] [WHERE])

Subqueries with the DELETE Statement: The subquery can be used in conjunction with the DELETE statement like with any other statements mentioned above. The basic syntax is as follows: DELETE FROM TABLE_NAME [ WHERE OPERATOR [ VALUE ] (SELECT COLUMN_NAME Correlated Subquery A query is called correlated subquery when both the inner query and the outer query are interdependent. For every row processed by the inner query, the outer query is processed as well. The inner query depends on the outer query before it can be processed. INNER JOIN/ NATURAL JOIN/ JOIN: The INNER JOIN keyword returns rows when there is at least one match in both tables. They are also referred to as an EQUIJOIN. Syntax SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name OUTER JOIN: It is an extension of join operation to deal with missing information. Left Outer Join or Left Join: It takes tuples in the left relation that did not match with any tuple in the right relation, pads the tuples with null values for all other attributes from the right relation and adds them to the result of the natural join. SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name Right Outer Join or Right Join: It takes tuples in the right relation that did not match with any tuple in the left relation, pads the tuples with null values for all other attributes from the left relation and adds them to the result of the natural join. FROM TABLE_NAME) [ WHERE) ]

SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name Full Outer Join: It combines tuples from both the left and the right relation and pads the tuples with null values for the missing attributes and them to the result of the natural join. SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name Cross Join A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. However, if a WHERE clause is added, the cross join behaves as an inner join. It is a binary operation that allows us to combine certain selections and a Cartesian product into one operation. Example: TO CREATE SSTUD1 TABLE SQL> create table sstud1 ( sname varchar2(20) , place varchar2(20)); Table created. SQL> insert into sstud1 values ( 'prajan','chennai'); 1 row created. SQL> insert into sstud1 values ( 'anand','chennai'); 1 row created. SQL> insert into sstud1 values ( 'kumar','chennai'); 1 row created. SQL> insert into sstud1 values ( 'ravi','chennai'); 1 row created. SQL> select * from sstud1; SNAME PLACE -------------------- -------------------Prajan chennai anand chennai kumar chennai ravi chennai TO CREATE SSTUD2 TABLE SQL> create table sstud2 ( sname varchar2(20), dept varchar2(10), marks number(10)); Table created. SQL> insert into sstud2 values ('prajan','cse',700);

1 row created. SQL> insert into sstud2 values ('anand','it',650); 1 row created. SQL> insert into sstud2 values ('vasu','cse',680); 1 row created. SQL> insert into sstud2 values ('ravi','it',600); 1 row created. SQL> select * from sstud2; SNAME DEPT MARKS -------------------- -----------------prajan cse 700 anand it 650 vasu cse 680 ravi it 600 SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from sstud2 ); SNAME -------------------anand prajan ravi SQL> select sname from sstud1 where sstud1.sname not in (select sstud2.sname from sstud2); SNAME -------------------kumar SQL> select sname from sstud2 where marks > some (select marks from sstud2 where dept='cse'); SNAME -------------------prajan SQL> select sname from sstud2 where marks >= some (select marks from sstud2 where dept='cse' ); SNAME -------------------prajan vasu SQL> select sname from sstud2 where marks > any (select marks from sstud2 where dept='cse' ); SNAME -------------------prajan SQL> select sname from sstud2 where marks >= any (select marks from sstud2 where dept='cse' );

SNAME -------------------prajan vasu SQL> select sname from sstud2 where marks > all (select marks from sstud2 where dept='cse' ); no rows selected SQL> select sname from sstud2 where marks < all (select marks from sstud2 where dept='cse' ); SNAME -------------------anand ravi SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2 where sstud1.sname=sstud2.sname ); SNAME -------------------prajan anand ravi SQL> select sname from sstud1 where not exists ( select sstud2.sname from sstud2 where sstud1.sname=sstud2.sname ); SNAME -------------------Kumar
JOIN OPERATIONS SQL> select sstud1.sname, dept from sstud1 inner join sstud2 on (sstud1.sname= sstud2.sname); SNAME DEPT -------------------- ---------Anand it Prajan cse ravi it SQL> select sstud1.sname, dept from sstud1 join sstud2 on (sstud1.sname= sstud2.sname); SNAME DEPT -------------------- ---------Anand it Prajan cse ravi it SQL> select sstud1.sname, dept from sstud1 left outer join sstud2 on (sstud1.sname= sstud2.sname); SNAME DEPT -------------------- ---------prajan cse anand it ravi it

kumar SQL> select sstud1.sname, dept from sstud1 right outer join sstud2 on (sstud1.sname= sstud2.sname) SNAME DEPT -------------------- ---------prajan cse anand it ravi it cse SQL> select sstud1.sname, dept from sstud1 full outer join sstud2 on (sstud1.sname= sstud2.sname); SNAME DEPT -------------------- ---------Prajan cse Anand it Ravi it kumar cse