CREATE DATABASE emp;
CREATE TABLE emp.dept (
id INT(11) NOT NULL AUTO_INCREMENT,
deptName VARCHAR(30) DEFAULT NULL,
iocAdd VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET = utf8;
CREATE TABLE emp.emp (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(20) DEFAULT NULL,
deptId INT(11) DEFAULT NULL,
PRIMARY KEY (id),
KEY fk_dept_id (deptId)
) DEFAULT CHARSET = utf8;
INSERT INTO emp.dept (deptName, iocAdd) VALUES ('RD', 11);
INSERT INTO emp.dept (deptName, iocAdd) VALUES ('HR', 12);
INSERT INTO emp.dept (deptName, iocAdd) VALUES ('MK', 13);
INSERT INTO emp.dept (deptName, iocAdd) VALUES ('MIS', 14);
INSERT INTO emp.dept (deptName, iocAdd) VALUES ('FD', 15);
INSERT INTO emp.emp(name, deptId) VALUES ('z3', 1);
INSERT INTO emp.emp(name, deptId) VALUES ('z4', 1);
INSERT INTO emp.emp(name, deptId) VALUES ('z5', 1);
INSERT INTO emp.emp(name, deptId) VALUES ('w5', 2);
INSERT INTO emp.emp(name, deptId) VALUES ('w6', 2);
INSERT INTO emp.emp(name, deptId) VALUES ('s7', 3);
INSERT INTO emp.emp(name, deptId) VALUES ('s8', 4);
INSERT INTO emp.emp(name, deptId) VALUES ('s9', 51);
SELECT * FROM emp.emp;
SELECT * FROM emp.dept;
SELECT * FROM emp.dept, emp.emp;
SELECT * FROM emp INNER JOIN dept ON emp.deptId = dept.id;
SELECT * FROM emp LEFT JOIN dept ON emp.deptId = dept.id;
SELECT * FROM emp RIGHT JOIN dept ON emp.deptId = dept.id;
SELECT * FROM emp LEFT JOIN dept ON emp.deptId = dept.id WHERE dept.id IS NULL;
SELECT * FROM emp RIGHT JOIN dept ON emp.deptId = dept.id WHERE emp.id IS NULL;
SELECT * FROM emp LEFT JOIN dept ON emp.deptId = dept.id
UNION
SELECT * FROM emp RIGHT JOIN dept ON emp.deptId = dept.id;
SELECT * FROM emp LEFT JOIN dept ON emp.deptId = dept.id WHERE dept.id IS NULL
UNION
SELECT * FROM emp.RIGHT JOIN dept ON emp.deptId = dept.id WHERE emp.id IS NULL;