0% found this document useful (0 votes)
22 views17 pages

It221 Act1 Joins

The document discusses different types of SQL joins including inner join, left join, right join, and cross join. It provides syntax examples and explanations of each join type. Activities are included to have the reader write and test queries using the various join clauses.

Uploaded by

Angel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views17 pages

It221 Act1 Joins

The document discusses different types of SQL joins including inner join, left join, right join, and cross join. It provides syntax examples and explanations of each join type. Activities are included to have the reader write and test queries using the various join clauses.

Uploaded by

Angel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIVERSITY OF SCIENCE AND TECHNOLOGY OF SOUTHERN PHILIPPINES

MySQL
JOINS: Activity 1
SETTING UP THE DATABASE
CREATE THE DATABASE

• Create a database and name it ACT1_yourlastname


• Use your database so we can access it and start creating the tables

• Create a table “MEMBERS”


With columns
• member_id int auto_increment and set as primary key
• Name varchar

• Create another table “committees”


With columns
• committee_id int auto_increment and set as primary key
• name varchar
INSERTING VALUES
INSERT VALUES IN THE DATABASE

“MEMBERS” table “COMMITTEE” table


member_id Name member_id Name
This is auto John This is auto John Some members are committee
incremented so incremented so
automatically Jane automatically Mary members, and some are not.
the id is 1-5 the id is 1-4 On the other hand, some
starting with Mary starting with Amelia
committee members are in the
John John
David Joe members table, some are not.
Amelia
INNER JOIN CLAUSE
INNER JOIN Table_1 and Table_2

SYNTAX: SELECT column_list – is for selecting the columns we want to appear in our table

SELECT column_list FROM – is for getting the values in our table


FROM table_1 INNER JOIN – is for JOINING the other table for our query
INNER JOIN table_2 ON
join_condition; ON join_condition – is for the condition we want to infer in joining our tables
INNER JOIN CLAUSE
ACTIVITY BY USING INNER JOIN IDENTIFY WHICH
MEMBERS are also in the COMMITTEE
AND VICE-VERSA.

HINTS:
SELECT table.column, table.column INNER JOIN the member_id,
FROM table member name, committee_id,
INNER JOIN table and committee name to see
ON join condition; identify members that are also in
the committee.
YOU can also use USING clause
instead of ON clause because you
can match the columns with their
names.
INNER JOIN CLAUSE
ACTIVITY

CALL THE ATTENTION OF YOUR INSTRUCTOR IF YOU HAVE


ACHIEVED THE QUERY SHOWN BELOW
LEFT JOIN CLAUSE
LEFT JOIN Table_1 and Table_2
The left join selects data starting from the left table. For each row in
the left table, the left join compares with every row in the right table.

If the values in the two rows satisfy the join condition, the left join
clause creates a new row whose columns contain all columns of the
SYNTAX: SYNTAX(USING): rows in both tables and includes this row in the result set.

SELECT column_list SELECT column_list If the values in the two rows are not matched, the left join clause still
creates a new row whose columns contain columns of the row in the
FROM table_1 FROM table_1 left table and NULL for columns of the row in the right table.
LEFT JOIN table_2 LEFT JOIN table_2
ON join_condition; USING (column_name); In other words, the left join selects all data from the left table whether
there are matching rows exist in the right table or not.

In case there are no matching rows from the right table found, the left
join uses NULLs for columns of the row from the right table in the result
set.
LEFT JOIN CLAUSE
VENN-DIAGRAM ILLUSTRATION
HINTS:
LEFT JOIN CLAUSE SELECT table.column, table.column
ACTIVITY
FROM table
LEFT JOIN table
USE THE LEFT JOIN CLAUSE TO JOIN THE MEMBERS TABLE AND
ON/USING join condition;
COMMITTEE TABLE

THE QUERY SHOULD RETURN THIS TABLE

CALL THE ATTENTION OF YOUR INSTRUCTOR IF YOU HAVE ACHIEVED THE QUERY
HINTS:
LEFT JOIN CLAUSE SELECT table.column, table.column
ACTIVITY
FROM table
LEFT JOIN table
Find members who are not the committee members, you will add a
USING join condition WHERE
WHERE clause and IS NULL operator as follows: table.column IS NULL;

THE QUERY SHOULD RETURN THIS TABLE

CALL THE ATTENTION OF YOUR INSTRUCTOR IF YOU HAVE ACHIEVED THE QUERY
RIGHT JOIN CLAUSE
Right Join table_1 and table_2
The right join clause is similar to the left join clause except that the
SYNTAX: SYNTAX(USING): treatment of left and right tables is reversed. The right join starts
selecting data from the right table instead of the left table.
SELECT column_list SELECT column_list
FROM table_1 The right join clause selects all rows from the right table and matches
FROM table_1
rows in the left table. If a row from the right table does not have
RIGHT JOIN table_2 RIGHT JOIN table_2 matching rows from the left table, the column of the left table will have
ON join_condition; USING (column_name); NULL in the final result set.

To find rows in the right table that SELECT column_list


do not have corresponding rows in FROM table_1
the left table, you also use a RIGHT JOIN table_2 USING
WHERE clause with the IS NULL (column_name)
operator: WHERE column_table_1 IS NULL;
RIGHT JOIN CLAUSE
VENN-DIAGRAM ILLUSTRATION
RIGHT JOIN CLAUSE
HINTS:
ACTIVITY SYNTAX:

SELECT column_list
Use the right join clause to get the table FROM table_1
below: RIGHT JOIN table_2
USING join_condition;
SYNTAX:

SELECT column_list
FROM table_1
RIGHT JOIN table_2
ON join_condition;

CALL THE ATTENTION OF YOUR INSTRUCTOR IF YOU HAVE ACHIEVED THE QUERY
RIGHT JOIN CLAUSE
ACTIVITY

HINT:
SELECT table.column, table.column
FROM table
Find the committee members who are not RIGHT JOIN table
in the members table, your table should USING join condition WHERE
look like this: table.column IS NULL;
CROSS JOIN CLAUSE
Cross Join Table_1 and Table_2

Unlike the inner join, left join, and right join, the cross join clause does
SYNTAX: not have a join condition.

The cross join makes a Cartesian product of rows from the joined
SELECT select_list tables. The cross join combines each row from the first table with every
FROM table_1 row from the right table to make the result set.
CROSS JOIN table_2;
Suppose the first table has n rows and the second table has m rows.
The cross-join that joins the tables will return nxm rows.
HINTS:
CROSS JOIN CLAUSE
SELECT
ACTIVITY table1.column,
table1.colum,
table2.colum,
table2.column,
FROM table_1
CROSS JOIN table_2

Use the CROSS JOIN


clause to retrieve
the table:
CALL THE ATTENTION OF
YOUR INSTRUCTOR IF YOU
HAVE ACHIEVED THE QUERY

You might also like