Tải bản đầy đủ (.ppt) (22 trang)

slide môn học MySQL bài 5 using joins

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (536.11 KB, 22 trang )

Using Joins
Session 10


Review


Primary key is a unique identifier in a table. Foreign key is a
primary key in a different table







The DISTINCT option in the SELECT command enables user to
view the distinct values for a column
Grouping facilitates to group rows with similar values for a
specific column into a single row in order to operate on them
together
Keys are pieces of information that helps to identify a record in
a table

MySQL Database / Session 10 / Slide 2 of 22


Objectives








Concept of Joining Tables
Equi-Join
Inner-Join
Outer-Join
Self-Join
Sub-Selects and UNION
MySQL Database / Session 10 / Slide 3 of 22


Concept of Joining Tables


Combining two or more records of different tables from the
same database, into one complete structure, is known as joining
of tables



The tables can be joined either by using WHERE clause with
SELECT command or by using JOIN keyword with SELECT
command

MySQL Database / Session 10 / Slide 4 of 22


Equi Join-I



An Equi-Join displays only those records where a match is
found in both the tables. The syntax for Equi-Join is:
SELECT column_name,...[*] FROM table1 [as
alias],table2 [as alias] WHERE
(join_condition);

MySQL Database / Session 10 / Slide 5 of 22


Equi Join-II


Display first name, department name and designation of all
employees from EMP_DETAILS and EMP_DEPARTMENT tables
SELECT EMP_DETAILS.E_FNAME,EMP_DEPARTMENT.
D_NAME,EMP_DEPARTMENT.DESIGNATION FROM
EMP_DETAILS,EMP_DEPARTMENT WHERE (EMP_DETAILS.E_ID
= EMP_DEPARTMENT.E_ID);

MySQL Database / Session 10 / Slide 6 of 22


Inner Join-I


An INNER JOIN creates a virtual table by combining the
fields of both tables that satisfy the query for both the tables.
The syntax for inner-join is:

SELECT column_name1, column_name2,
column_name3 FROM table_name1 INNER JOIN
table_name2 ON
table_name1.primary_keyfield =
table_name2.foreign_keyfield;

MySQL Database / Session 10 / Slide 7 of 22


Inner Join-II


Display first name, department name, and designation of all
employees from EMP_DETAILS and EMP_DEPARTMENT tables
SELECT EMP_DETAILS.E_FNAME,EMP_DEPARTMENT.
D_NAME,EMP_DEPARTMENT.DESIGNATION FROM
EMP_DETAILS INNER JOIN EMP_DEPARTMENT ON
EMP_DETAILS.E_ID = EMP_DEPARTMENT.E_ID;

MySQL Database / Session 10 / Slide 8 of 22


Inner Join-III


Display all the employees whose E_ID are same and who live in
California city
SELECT EMP_DETAILS.E_FNAME,EMP_DEPARTMENT.D_NAME,
EMP_DETAILS.E_ADDRESS FROM EMP_DETAILS INNER JOIN
EMP_DEPARTMENT ON (EMP_DETAILS.E_ID =

EMP_DEPARTMENT.E_ID) AND (EMP_DETAILS.E_ADDRESS =
‘CALIFORNIA’);

MySQL Database / Session 10 / Slide 9 of 22


Inner Join using table aliases


Display employee details
using table aliases
SELECT E.E_FNAME,
E.E_LNAME
S.BASIC_SALARY FROM
EMP_DETAILS AS E,
EMP_SALARY AS S WHERE
E.E_ID = S.E_ID;

MySQL Database / Session 10 / Slide 10 of 22


Outer Join


An OUTER JOIN displays even those rows of the tables that may
not have any matching value in the other tables to appear in the
result set. There are three types of outer join. They are:




Left Outer Join
Right Outer Join
Full Outer Join




MySQL Database / Session 10 / Slide 11 of 22


Left Outer Join-I


Left Outer Join:
Displays all rows from the table specified on the left of the
OUTER JOIN operator in the result set, with or without any
match with the table specified on the right. The syntax is:
SELECT COLUMN1, COLUMN2 FROM TABLE1
LEFT OUTER JOIN TABLE2 ON (JOIN_
CONDITION);

MySQL Database / Session 10 / Slide 12 of 22


Left Outer Join-II


Display all the records from EMP_DETAILS, EMP_SALARY, and
EMP_DEPARTMENT tables by joining them with the common attribute
E_ID

SELECT E_FNAME,E_LNAME,E_ADDRESS FROM EMP_DETAILS LEFT
JOIN EMP_SALARY ON EMP_DETAILS.E_ID = EMP_SALARY.E_ID
LEFT JOIN EMP_DEPARTMENT ON EMP_SALARY.E_ID =
EMP_DEPARTMENT.E_ID;

MySQL Database / Session 10 / Slide 13 of 22


Right Outer Join-I


Right Outer Join:
Displays all rows from the table specified on the right of the
OUTER JOIN operator in the result set, with or without any
match with the table specified on the left. The syntax is:
SELECT COLUMN1, COLUMN2 FROM TABLE1
RIGHT OUTER JOIN TABLE2 ON (JOIN_
CONDITION);

MySQL Database / Session 10 / Slide 14 of 22


Right Outer Join-II


Display records of E_LNAME,
E_ADDRESS and E_PHONE_NO from
EMP_DETAILS table where the
employee ids are greater than 102
SELECT DISTINCT

E_LNAME,E_ADDRESS,E_PHONE_
NO FROM EMP_DETAILS E RIGHT
OUTER JOIN EMP_DEPARTMENT D
ON(E.E_ID > 102);

MySQL Database / Session 10 / Slide 15 of 22


Self-Join-I


A Self-Join is a query that is used to join or compare a
table to itself. The syntax is:
SELECT DISTINCT COLUMN1,COLUMN2 FROM TABLE
WHERE (JOIN_CONDITION);

MySQL Database / Session 10 / Slide 16 of 22


Self-Join-II


Display all the employees, who
are located in the same city
SELECT DISTINCT E1.E_ID,
E1.E_FNAME,E1.E_LNAME,
E.E_ADDRESS FROM EMP_DETAILS
AS E1,EMP_DETAILS AS E2
WHERE E1.E_ADDRESS =
E2.E_ADDRESS AND E1.E_ADDRESS

IS NOT NULL AND E2.E_ADDRESS
IS NOT NULL AND E1.E_ID <>
E2.E_ID ORDER BY
E1.E_ADDRESS,E1.E_ID;
MySQL Database / Session 10 / Slide 17 of 22


Sub-Select-I


Sub-Select:
When a SELECT query is placed inside another SELECT
query, then the inner SELECT query is said to be Sub-Select
query. The syntax is:
SELECT [*] column_name… FROM table_name1
WHERE column_name IN (SELECT column_name
FROM table_name2);

MySQL Database / Session 10 / Slide 18 of 22


Sub-Select-II


Display all records from EMP_DETAILS and EMP_DEPARTMENT
tables using sub-selects
SELECT * FROM EMP_DETAILS WHERE E_ID IN (SELECT
E_ID FROM EMP_DEPARTMENT);

MySQL Database / Session 10 / Slide 19 of 22



UNION-I


Union is used to combine many result sets used with
SELECT command into a single result set. The syntax is:
SELECT * FROM TABLE1 UNION [ALL | DISTINCT]
SELECT * FROM TABLE2 UNION [ALL | DISTINCT]

MySQL Database / Session 10 / Slide 20 of 22


UNION-II


Display all the records from EMP_DETAILS table where the employee
addresses are California and Troy
(SELECT * FROM EMP_DETAILS WHERE E_ADDRESS =
‘TROY’ORDER BY E_ID LIMIT 2)UNION (SELECT * FROM
EMP_DETAILS WHERE E_ADDRESS =‘CALIFORNIA’ ORDER
BY E_ID LIMIT 1) ORDER BY E_ID;

MySQL Database / Session 10 / Slide 21 of 22


Summary













Joining of tables means combining two or more records of different tables of
the same database into one comprehensive structure
The tables can be joined either with the WHERE clause used with SELECT
command or by using the JOIN keyword
In an Equi-Join, the comparison is made between two columns that carry
same values
An INNER JOIN creates a virtual table by combining the fields of both
tables that satisfy the query for both the tables
Union is used to combine many result sets used with SELECT command into
a single result set
A select query used inside another select query is known as Sub-selects query

MySQL Database / Session 10 / Slide 22 of 22



×