Save
Databases Final
JOIN
Save
Share
Learn
Content
Leaderboard
Learn
Created by
tiana okane
Visit profile
Cards (40)
JOIN
Staff_
ID
Staff_
Name
Job_
ID
View source
Modules
CSC1023
, Databases,
1
, 1
CSC1029
, Object Oriented Programming,
2,
2
CSC1030
, Web Technologies,
3,
3
CSC1031
, Software Design Principles, 3,
4
View source
Students
40319000, CSC1023,
Lee Chong Wei
, 123 3rd Street,
Los Angelas
, CA
40319001, CSC1023,
Victor Axelson
, 123 4th Street,
New York
, NY
40319002, CSC1029,
Low Kean Yew
,
123 6th
Street, Milwaukee, WI
40319003
, CSC1030,
John Dory
, 789 29th Street, Denver, CO
40319004, CSC1031, Harry Ramsdens, 789 26th Street, Miami, FL
View source
Relational Example, Dr Bee Yen Toh, Queen's University
Belfast
, CSC1023 -
Databases
View source
Job
_details
1,
Research
Fellow
2, Leturer
3, Senior
Lecturer
4, Reader
5,
Professor
View source
Assessments
1, Class Test, Online, wk 7
2, Project, hard-copy, wk 12
3, Exam, Online, wk
13
View source
A Join
is a query that combines data from multiple
tables
, based on a related attribute between the tables.
View source
Types
of JOIN
INNER
JOIN
LEFT
JOIN
Self
JOIN
RIGHT
JOIN
CROSS
JOIN
View source
INNER
JOIN
Retrieves
rows that have matching values in 2 or more tables. Used when you only want to see the records where there is a match in the
joined
tables.
View source
Self
JOIN
Join a
table
to itself, for comparing rows within the same
table.
View source
LEFT
JOIN
All Rows from
Left
table + matching rows from
Right
table
View source
RIGHT JOIN
All Rows from
Right
table + matching rows from
Left
table
View source
CROSS JOIN
Combines each row of one table with each row of another table, resulting in the
Cartesian
product of the sets.
View source
Attribute
names don't have to be identical, but there must be a matching value (
CSC1023
)
View source
If
the attribute name in both tables are the same, you can use USING(Module_ID) instead of ON Students.Module_ID =
Modules.courseID
View source
SELECT
Student_Name, Title FROM Students INNER JOIN Modules ON Students.Module_ID =
Modules.Module_ID
;
View source
SELECT Student_Name,
Title
FROM
Students INNER JOIN Modules USING
(Module_ID) ;
View source
INNER JOIN
- different
Column names
View source
ALTER
TABLE Modules
CHANGE Module_ID Course_ID
VARCHAR(20);
View source
SELECT
Student_Name, Title FROM Students INNER JOIN Modules ON Students.Module_ID =
Modules.Course_ID
;
View source
SELECT
Student_Name, Title FROM Students INNER JOIN Modules ON Students.Module_ID =
Modules.Module_ID
WHERE Students.module_ID = 'CSC1023' ;
View source
SELECT DISTINCT Student_Name, Title FROM Students INNER JOIN Modules ON course_ID = Module_ID WHERE course_ID =
'CSC1023'
ORDER BY
Student_Name
DESC ;
View source
SELECT DISTINCT * FROM Students
INNER
JOIN Modules ON
course_ID
= Module_ID ORDER BY Student_ID DESC ;
View source
SELECT DISTINCT * FROM Students
INNER
JOIN Modules ON
course_ID
= Module_ID ORDER BY Student_ID DESC LIMIT 0,3 ;
View source
Example
2
: Customer and Booking
View source
DROP TABLE
IF EXISTS booking;
View source
CREATE TABLE `booking` ( `id` INT NOT NULL AUTO_INCREMENT, `customerid` INT NULL, `booking`
DATETIME
, PRIMARY KEY(`id`), FOREIGN KEY(`customerid`) REFERENCES `customer`(`id`) ON DELETE
RESTRICT
ON UPDATE RESTRICT );
View source
INSERT
INTO `booking`(`customerid`, `booking`) VALUES (1,'
2018-01-11
12:30:00'), (1,'2018-01-23 19:30:00'), (3,'2018-01-20 20:00:00'), (4,'2018-01-21 13:00:00');
View source
DROP TABLE
IF EXISTS customer;
View source
CREATE TABLE `customer` ( `id` INT
NOT
NULL AUTO_INCREMENT , `name` VARCHAR(255)
NOT
NULL , PRIMARY KEY (`id`) );
View source
INSERT INTO customer (id, `
name
`)
VALUES
('Dave'), ('Sarah'), ('Jim'), ('Alice');
View source
Get
Bookings
View source
Get
CustomerID
View source
Get Customer Name from Customer
ID
View source
Display
Results
View source
Qs
: How are the tables arranged?
View source
SELECT
* FROM booking INNER JOIN customer
ON
booking.customerid=customer.id
View source
SELECT
name, booking FROM customer INNER JOIN booking
ON
booking.customerid=customer.id;
View source
LEFT JOIN Selects all records from the left table and matched records from the right table
View source
SELECT
* FROM customer LEFT JOIN booking
ON
booking.customerid=customer.id;
View source