Вы находитесь на странице: 1из 11

EXERCISE 6: SQL JOINS

A SQL Join statement is used to combine data or rows from two or more tables based on a
common field between them.

Different types of Joins are:

 INNER JOIN
 LEFT JOIN
 RIGHT JOIN
 FULL JOIN

1. INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long
as the condition satisfies. This keyword will create the result-set by combining all rows
from both the tables where the condition satisfies i.e value of the common field will be
same.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;

2. LEFT JOIN: This join returns all the rows of the table on the left side of the join and
matching rows for the table on the right side of join. The rows for which there is no
matching row on right side, the result-set will contain null. LEFT JOIN is also known as
LEFT OUTER JOIN.
Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

LEFT JOIN
3. RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of
the table on the right side of the join and matching rows for the table on the left side of
join. The rows for which there is no matching row on left side, the result-set will contain
null. RIGHT JOIN is also known as RIGHT OUTER JOIN.
Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

RIGHT JOIN

4. FULL JOIN: FULL JOIN creates the result-set by combining result of both LEFT JOIN
and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows
for which there is no matching, the result-set will contain NULL values.
Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
FULL JOIN

SQL> create table products(pid int,pname varchar(20));

Table created.

SQL> create table colors(pno int,color varchar(20));

Table created.

SQL> insert into products values(1,'Tele Vision');

1 row created.

SQL> insert into products values(2,'Refrigerator');

1 row created.

SQL> insert into products values(4,'Air Conditioner');

1 row created.

SQL> insert into products values(6,'Vaccum Cleaner');

1 row created.

SQL> insert into colors values(1,'red');

1 row created.

SQL> insert into colors values(3,'blue');

1 row created.

SQL> insert into colors values(4,'green');

1 row created.

SQL> insert into colors values(5,'yellow');


1 row created.

SQL> insert into colors values(6,'pink');

1 row created.

SQL> insert into colors values(7,'black');

1 row created.

SQL> select pid,pname,color from products natural join colors;

PID PNAME COLOR

---------- -------------------- --------------------

1 Tele Vision red

1 Tele Vision blue

1 Tele Vision green

1 Tele Vision yellow

1 Tele Vision pink

1 Tele Vision black

2 Refrigerator red

2 Refrigerator blue

2 Refrigerator green

2 Refrigerator yellow

2 Refrigerator pink

2 Refrigerator black

4 Air Conditioner red

4 Air Conditioner blue

4 Air Conditioner green

4 Air Conditioner yellow


4 Air Conditioner pink

4 Air Conditioner black

6 Vaccum Cleaner red

6 Vaccum Cleaner blue

6 Vaccum Cleaner green

6 Vaccum Cleaner yellow

6 Vaccum Cleaner pink

6 Vaccum Cleaner black

24 rows selected.

SQL> select pid,pname,color from products cross join colors;

PID PNAME COLOR

---------- -------------------- --------------------

1 Tele Vision red

1 Tele Vision blue

1 Tele Vision green

1 Tele Vision yellow

1 Tele Vision pink

1 Tele Vision black

2 Refrigerator red

2 Refrigerator blue

2 Refrigerator green

2 Refrigerator yellow

2 Refrigerator pink

2 Refrigerator black

4 Air Conditioner red


4 Air Conditioner blue

4 Air Conditioner green

4 Air Conditioner yellow

4 Air Conditioner pink

4 Air Conditioner black

6 Vaccum Cleaner red

6 Vaccum Cleaner blue

6 Vaccum Cleaner green

6 Vaccum Cleaner yellow

6 Vaccum Cleaner pink

6 Vaccum Cleaner black

24 rows selected.

SQL> select pid,pname,color from products inner join colors on products.pid=colors.pno;

PID PNAME COLOR

---------- -------------------- --------------------

1 Tele Vision red

4 Air Conditioner green

6 Vaccum Cleaner pink

SQL> select pid,pname,color from products left join colors on products.pid=colors.pno;

PID PNAME COLOR

---------- -------------------- --------------------

1 Tele Vision red

4 Air Conditioner green

6 Vaccum Cleaner pink

2 Refrigerator
SQL> select pid,pname,color from products right join colors on products.pid=colors.pno;

PID PNAME COLOR

---------- -------------------- --------------------

1 Tele Vision red

4 Air Conditioner green

6 Vaccum Cleaner pink

black

yellow

blue

6 rows selected.

SQL> select pid,pname,color from products full join colors on products.pid=colors.pno;

PID PNAME COLOR

---------- -------------------- --------------------

1 Tele Vision red

4 Air Conditioner green

6 Vaccum Cleaner pink

2 Refrigerator

black

yellow

blue

7 rows selected.
EXERCISE 5: SQL SUBQUERIES

IN

IN condition (sometimes called the IN operator) allows you to easily test if an expression
matches any value in a list of values.

ANY

The ANY comparison condition is used to compare a value to a list or subquery. It must be
preceded by =, !=, >, <, <=, >= and followed by a list or subquery.

When the ANY condition is followed by a list, the optimizer expands the initial condition to all
elements of the list and strings them together with OR operators, as shown below.

 "x = ANY (...)": The value must match one or more values in the list to evaluate to
TRUE.
 "x != ANY (...)": The value must not match one or more values in the list to evaluate to
TRUE.
 "x > ANY (...)": The value must be greater than the smallest value in the list to evaluate
to TRUE.
 "x < ANY (...)": The value must be smaller than the biggest value in the list to evaluate to
TRUE.
 "x >= ANY (...)": The value must be greater than or equal to the smallest value in the list
to evaluate to TRUE.
 "x <= ANY (...)": The value must be smaller than or equal to the biggest value in the list
to evaluate to TRUE.

ALL

The ALL comparison condition is used to compare a value to a list or subquery. It must be
preceded by =, !=, >, <, <=, >= and followed by a list or subquery.

When the ALL condition is followed by a list, the optimizer expands the initial condition to all
elements of the list and strings them together with AND operators

 "x = ALL (...)": The value must match all the values in the list to evaluate to TRUE.
 "x != ALL (...)": The value must not match any values in the list to evaluate to TRUE.
 "x > ALL (...)": The value must be greater than the biggest value in the list to evaluate to
TRUE.
 "x < ALL (...)": The value must be smaller than the smallest value in the list to evaluate
to TRUE.
 "x >= ALL (...)": The value must be greater than or equal to the biggest value in the list to
evaluate to TRUE.
 "x <= ALL (...)": The value must be smaller than or equal to the smallest value in the list
to evaluate to TRUE.

EXISTS

EXISTS condition is used in combination with a subquery and is considered to be met, if the
subquery returns at least one row

SQL> create table emp(eid int,ename varchar(20),salary int);

Table created.

SQL> insert into emp values(1,'Amith',50000);

1 row created.

SQL> insert into emp values(2,'Bhanu',40000);

1 row created.

SQL> insert into emp values(3,'Chanakya',80000);

1 row created.

SQL> insert into emp values(4,'Damodar',70000);

1 row created.

SQL> insert into emp values(5,'Emanuel',60000);

1 row created.

SQL> insert into emp values(6,'Fernadez',30000);

1 row created.

SQL> select * from emp where salary in (select salary from emp where salary>50000);

EID ENAME SALARY

---------- -------------------- ----------

3 Chanakya 80000

4 Damodar 70000
5 Emanuel 60000

SQL> select * from emp where salary =any (select salary from emp where salary>50000);

EID ENAME SALARY

---------- -------------------- ----------

3 Chanakya 80000

4 Damodar 70000

5 Emanuel 60000

SQL> select * from emp where salary >any (select salary from emp where salary>50000);

EID ENAME SALARY

---------- -------------------- ----------

3 Chanakya 80000

4 Damodar 70000

SQL> select * from emp where salary <any (select salary from emp where salary>50000);

EID ENAME SALARY

---------- -------------------- ----------

1 Amith 50000

2 Bhanu 40000

4 Damodar 70000

5 Emanuel 60000

6 Fernadez 30000

SQL> select * from emp where salary =all (select salary from emp where salary>50000);

no rows selected

SQL> select * from emp where salary >all (select salary from emp where salary>50000);

no rows selected

SQL> select * from emp where salary <all (select salary from emp where salary>50000);
EID ENAME SALARY

---------- -------------------- ----------

1 Amith 50000

2 Bhanu 40000

6 Fernadez 30000

SQL> select * from emp where exists (select salary from emp where salary>50000);

EID ENAME SALARY

---------- -------------------- ----------

1 Amith 50000

2 Bhanu 40000

3 Chanakya 80000

4 Damodar 70000

5 Emanuel 60000

6 Fernadez 30000

6 rows selected.

Вам также может понравиться