Difference between revisions of "Equi Join"

From SQLZOO
Jump to: navigation, search
(Created page with "How do you use Equi Join to join two tables with the same name? <div class='ht'> <div> I am trying to join two tables with the same name. I am required to use an inner join Al...")
 
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
How do you use Equi Join to join two tables with the same name?
 
How do you use Equi Join to join two tables with the same name?
 
<div class='ht'>
 
<div class='ht'>
 +
<div class=params>schema:scott</div>
 
<div>
 
<div>
 
I am trying to join two tables with the same name. I am required to use an inner join
 
I am trying to join two tables with the same name. I am required to use an inner join
Line 14: Line 15:
 
does not show up in the results.
 
does not show up in the results.
 
</div>
 
</div>
<source lang=sql class='tidy'>
+
<source lang=sql class='tidy'>DROP TABLE employee</source>
 +
<source lang=sql class='setup'></source>
  
</source>
 
 
<source lang='sql' class='def e-access'>CREATE TABLE employee(
 
<source lang='sql' class='def e-access'>CREATE TABLE employee(
 
   employee_id INTEGER PRIMARY KEY,
 
   employee_id INTEGER PRIMARY KEY,

Latest revision as of 13:21, 12 July 2012

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

schema:scott

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.

DROP TABLE employee
 
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