It221 Act1 Joins
It221 Act1 Joins
MySQL
JOINS: Activity 1
SETTING UP THE DATABASE
CREATE THE DATABASE
SYNTAX: SELECT column_list – is for selecting the columns we want to appear in our table
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
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
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;
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.
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