Equi Join

From SQLZOO
Revision as of 12:47, 11 July 2012 by Marek (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

How do you use Equi Join to join two tables with the same name?

I am trying to join two tables with the same name. I am required to use an inner join Also I need to show the managers name. Question is: Join the employee table with the employee table and department table. Show the employee id, name, dept code, manager id, manager name, managers department code, and name for that (mgr's) department.

We have a self join, each copy of the table is given an "alias" - here we use w for the worker and b for the boss. We can treat these as different tables. You get an inner join by default - this means that Robin (who has no boss) does not show up in the results.

 
CREATE TABLE employee(
  employee_id INTEGER PRIMARY KEY,
  first_name VARCHAR(10),
  dept_code VARCHAR(10),
  manager_id INTEGER REFERENCES employee);
INSERT INTO employee VALUES (1,'Robin','Eng',NULL);
INSERT INTO employee VALUES (2,'Jon','SoC',1);
INSERT INTO employee VALUES (3,'Andrew','SoC',2);
INSERT INTO employee VALUES (4,'Alison','SoC',2);
SELECT w.first_name AS worker, b.first_name AS boss
 FROM employee w, employee b
WHERE w.manager_id = b.employee_id
CREATE TABLE employee(
  employee_id INTEGER PRIMARY KEY,
  first_name VARCHAR(10),
  dept_code VARCHAR(10),
  manager_id INTEGER REFERENCES employee);
INSERT INTO employee VALUES (1,'Robin','Eng',NULL);
INSERT INTO employee VALUES (2,'Jon','SoC',1);
INSERT INTO employee VALUES (3,'Andrew','SoC',2);
INSERT INTO employee VALUES (4,'Alison','SoC',2);
SELECT w.first_name AS worker, b.first_name AS boss
 FROM employee w, employee b
WHERE w.manager_id = b.employee_id
CREATE TABLE employee(
  employee_id INTEGER PRIMARY KEY,
  first_name VARCHAR(10),
  dept_code VARCHAR(10),
  manager_id INTEGER REFERENCES employee);
INSERT INTO employee VALUES (1,'Robin','Eng',NULL);
INSERT INTO employee VALUES (2,'Jon','SoC',1);
INSERT INTO employee VALUES (3,'Andrew','SoC',2);
INSERT INTO employee VALUES (4,'Alison','SoC',2);
SELECT w.first_name AS worker, b.first_name AS boss
 FROM employee w, employee b
WHERE w.manager_id = b.employee_id
USE scott;
CREATE TABLE employee(
  employee_id INTEGER PRIMARY KEY,
  first_name VARCHAR(10),
  dept_code VARCHAR(10),
  manager_id INTEGER REFERENCES employee);
INSERT INTO employee VALUES (1,'Robin','Eng',NULL);
INSERT INTO employee VALUES (2,'Jon','SoC',1);
INSERT INTO employee VALUES (3,'Andrew','SoC',2);
INSERT INTO employee VALUES (4,'Alison','SoC',2);
SELECT w.first_name AS worker, b.first_name AS boss
 FROM employee w, employee b
WHERE w.manager_id = b.employee_id
CREATE TABLE employee(
  employee_id INTEGER PRIMARY KEY,
  first_name VARCHAR(10),
  dept_code VARCHAR(10),
  manager_id INTEGER REFERENCES employee);
INSERT INTO employee VALUES (1,'Robin','Eng',NULL);
INSERT INTO employee VALUES (2,'Jon','SoC',1);
INSERT INTO employee VALUES (3,'Andrew','SoC',2);
INSERT INTO employee VALUES (4,'Alison','SoC',2);
SELECT w.first_name AS worker, b.first_name AS boss
 FROM employee w, employee b
WHERE w.manager_id = b.employee_id
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense