Академический Документы
Профессиональный Документы
Культура Документы
A SQL Join statement is used to combine data or rows from two or more tables based on a
common field between them.
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
Table created.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
2 Refrigerator red
2 Refrigerator blue
2 Refrigerator green
2 Refrigerator yellow
2 Refrigerator pink
2 Refrigerator black
24 rows selected.
2 Refrigerator red
2 Refrigerator blue
2 Refrigerator green
2 Refrigerator yellow
2 Refrigerator pink
2 Refrigerator black
24 rows selected.
2 Refrigerator
SQL> select pid,pname,color from products right join colors on products.pid=colors.pno;
black
yellow
blue
6 rows selected.
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
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> select * from emp where salary in (select salary from emp where salary>50000);
3 Chanakya 80000
4 Damodar 70000
5 Emanuel 60000
SQL> select * from emp where salary =any (select salary from emp where salary>50000);
3 Chanakya 80000
4 Damodar 70000
5 Emanuel 60000
SQL> select * from emp where salary >any (select salary from emp where salary>50000);
3 Chanakya 80000
4 Damodar 70000
SQL> select * from emp where salary <any (select salary from emp where salary>50000);
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);
1 Amith 50000
2 Bhanu 40000
3 Chanakya 80000
4 Damodar 70000
5 Emanuel 60000
6 Fernadez 30000
6 rows selected.