You are on page 1of 15

Aliases, Joins, Concatenation of

data,
UNION, INTERSECT and MINUS
clause
Aliases
 Aliases are used to give temporary names to the columns in a table.
 The AS Clause allows users to specify an alias name for items in
a SELECT statement.
 The alias name will be returned as the column heading in the query
result instead of the actual column name.
 Syntax: SELECT <column_name> AS <aliase_name> FROM
<table_name>
Example: SELECT ename AS "Employee Name", Dep_id AS
"Department ID" FROM employee;
Joins
 Sometimes it is necessary to work with multiple tables.
 Joins are used to combine records or manipulate data from two
or more tables in a database.
 Tables are joined on columns that have the same data type and
data width in the tables.
 Tables in a database can be related to each other with keys.
 The purpose is to bind data together, across tables, without
repeating all of the data in every table.
 The JOIN operator specifies how to relate tables in the query.
Joins
 Consider the following two tables
 Now, let us join these two tables in our SELECT statement:
SELECT ID, NAME, AGE, AMOUNT FROM CUSTOMERS, ORDERS WHERE
CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
CUSTOMERS ORDERS

ID NAME AGE ADDRESS SAL OID ODATE CUSTOMER_ID AMOUNT


102 08-Sep-2009 3 3000
100 08-Sep-2009 3 1500
101 20-Nov-2009 2 1560
103 20-May-2008 4 2060
ARY
1 Hayes 32 London 2000.00 RESULT
2 Smith 25 NY 1500.00
ID NAME AGE AMOUNT
3 Steve 23 Boston 2000.00
3 Steve 23 1500
4 Nick 25 Tokyo 6500.00
5 Gibson 27 Melbourne 8500.00 3 Steve 23 3000
6 Brad 22 Chicago 4500.00 2 Smith 25 1560
7 Paul 24 Philadelphia 10000.00 4 Nick 25 2060
Joins
 Here, it is noticeable that the join is performed in the WHERE clause.
 Several operators can be used to join tables, such as =, <, >, <>,
<=, >=, !=, BETWEEN, LIKE, and NOT; they can all be used to join
tables.
 However, the most common operator is the equal symbol.
 Types of JOIN
 INNER
 OUTER (LEFT, RIGHT, FULL)
 CROSS
 SELF
INNER JOIN
 The most frequently used and important of the joins is the INNER
JOIN.
 They are also referred to as an EQUIJOIN.
 It creates a new table by combining column values of two tables
(table1 and table2) based upon the join-predicate.
 The query compares each row of table1 with each row of table2 to
find all pairs of rows which satisfy the join-predicate.
 When the join-predicate is satisfied, column values for each matched
pair of rows of A and B are combined into a result row.
 Syntax:
 SELECT table1.column1, table2.column2... FROM table1 INNER JOIN
table2 ON table1.common_field = table2.common_field; (ANSI-Style)
 SELECT table1.column1, table2.column2... FROM table1, table2 WHERE
table1.common_field = table2.common_field; (Theta-Style)
INNER JOIN (Contd…)
 Example:
 ANSI-Style
SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS INNER JOIN
ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
OR
SELECT C.ID, C.NAME, O.AMOUNT, O.ODATE FROM CUSTOMERS C INNER
JOIN ORDERS O ON C.ID = O.CUSTOMER_ID;
 Theta-Style
SELECT C.ID, C.NAME, O.AMOUNT, O.ODATE FROM CUSTOMERS C,
ORDERS O WHERE C.ID = O.CUSTOMER_ID;
RESULT
ID NAME AMOUNT ODATE

3 Steve 1500 08-SEP-09


3 Steve 3000 08-SEP-09
2 Smith 1560 20-NOV-09
4 Nick 2060 20-MAY-08
LEFT JOIN
 LEFT JOIN returns all the values from the left table, plus matched
values from the right table or NULL in case of no matching join
predicate.
 Syntax:
 SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2
ON table1.common_field = table2.common_field; (ANSI-Style)
 SELECT table1.column1, table2.column2... FROM table1, table2 WHERE
table1.common_field = table2.common_field(+);(Theta-Style)
 Example:
 ANSI-Style
SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS LEFT JOIN
ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
 Theta-Style
SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID(+);
RIGHT JOIN
 RIGHT JOIN returns all the values from the right table, plus matched
values from the left table or NULL in case of no matching join
predicate.
 Syntax:
 SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN
table2 ON table1.common_field = table2.common_field; (ANSI-Style)
 SELECT table1.column1, table2.column2... FROM table1, table2 WHERE
table1.common_field(+) = table2.common_field;(Theta-Style)
 Example:
 ANSI-Style
SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS RIGHT JOIN
ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
 Theta-Style
SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID(+) = ORDERS.CUSTOMER_ID;
FULL JOIN
 FULL JOIN combines the results of both left and right outer joins.
 The joined table will contain all records from both tables, and fill in
NULLs for missing matches on either side.
 Syntax:
 SELECT table1.column1, table2.column2... FROM table1 FULL JOIN table2
ON table1.common_field = table2.common_field;
 Example:
 SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS FULL JOIN
ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
CROSS JOIN
 The CROSS JOIN or CARTESIAN JOIN returns the cartesian
product of the sets of records from the two or more joined tables.
 Thus, it equates to an inner join where the join-condition always
evaluates to True or where the join-condition is absent from the
statement.
 Syntax:
 SELECT table1.column1, table2.column2... FROM table1 CROSS JOIN
table2;
 Example:
 SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS CROSS JOIN
ORDERS;
OR
SELECT ID, NAME, AMOUNT, ODATE FROM CUSTOMERS, ORDERS;
SELF JOIN
 SELF JOIN is used to join a table to itself as if the table were two
tables, temporarily renaming at least one table in the SQL statement.
 Syntax:
 SELECT a.column_name, b.column_name…FROM table1 a, table1 b WHERE
a.common_field = b.common_field;
 Example:
 SELECT a.ID, a.NAME, a.SALARY FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY;
Concatenating data from table columns
 Example:
select CLIENT_NO, NAME, ADDRESS1 || ', ' || ADDRESS2 || ', ' ||
CITY || ', ' || STATE || '-' || PINCODE "Address" from client_master;
UNION, INTERSECT and MINUS clause
 UNION clause: Multiple queries can be put together and their
output can be combined using the UNION clause. It merges the
output of two or more queries into a single set of rows and columns.
 Syntax: <Query1> UNION <Query2>
 INTERSECT clause: Multiple queries can be put together and their
output can be combined using the INTERSECT clause. The output in
an INTERSECT clause will include only those rows that are retrieved,
common to both the queries.
 Syntax: <Query1> INTERSECT <Query2>
 MINUS clause: Multiple queries can be put together and their
output can be combined using the MINUS clause. The MINUS clause
outputs the rows produced by the first query, after filtering the rows
retrieved by the second query.
 Syntax: <Query1> MINUS <Query2>
End

You might also like