Difference between revisions of "Combine tables"

From SQLZOO
Jump to: navigation, search
(Created page with "Combine tables containing different data <div class='ht'> <div class=params>schema:scott</div> <source lang=sql class='tidy'>DROP TABLE staff; DROP TABLE student;</source> <so...")
 
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Combine tables containing different data
+
<p>Combine tables containing different data.</p>
 +
<p>In this example you are shown how to take tables with different data and put them into a single table</p>
 +
<p>that is more understandable allowing all the information from two or more tables to be seen.</p>
 +
<p>Table 1 and table 2 show the two tables you want to combine and table 3 is the table they are combined into.</p>
 +
<div class="ref_section">
 +
<table class= "db_ref">
 +
<caption>Table 1</caption>
 +
<tr><th align='center'>'''staffid'''</th><th>'''email'''</th><th align='center'>'''name'''</th><th>'''salary'''</th></tr>
 +
<tr><td align='left'>0173</td><td align='left'>stan@bos.edu</td><td align='left'>Stern, Stan</td><td align='left'>99000</td></tr>
 +
<tr><td align='left'>0101</td><td align='left'>ali@bos.edu</td><td align='left'>Aloof, Alison</td><td align='left'>30000</td></tr>
 +
</table>
 +
<table class="db_ref">
 +
<caption>Table 2</caption>
 +
<tr><th align='center'>'''id'''</th><th>'''fname'''</th><th align='center'>'''lname'''</th><th align='center'>'''gpa'''</th></tr>
 +
<tr><td align='left'>1007</td><td align='left'>Peter</td><td align='left'>Perfect</td><td align='left'>590</td></tr>
 +
<tr><td align='left'>1008</td><td align='left'>Donald</td><td align='left'>Dunce</td><td align='left'>220</td></tr>
 +
</table>
 +
<table class="db_ref">
 +
<caption>Table 3</caption>
 +
<tr><th align='center'>'''id'''</th><th>'''name'''</th><th align='center'>'''email'''</th><th align='center'>'''salary'''</th><th align='center'>'''gpa'''</th><th align='center'>'''species'''</th></tr>
 +
<tr><td align='left'>F173</td><td align='left'>Stern,Stan</td><td align='left'>stan@bos.edu</td><td align='left'>99000</td><td align='left'></td><td align='left'>Staff</td></tr>
 +
<tr><td align='left'>F101</td><td align='left'>Aloof,Alison</td><td align='left'>ali@bos.edu</td><td align='left'>30000</td><td align='left'></td><td align='left'>Staff</td></tr>
 +
<tr><td align='left'>S1007</td><td align='left'>Perfect,Peter</td><td align='left'>1007@bos.edu</td><td align='left'></td><td align='left'>590</td><td align='left'>Student</td></tr>
 +
<tr><td align='left'>S1008</td><td align='left'>Dunce,Donald</td><td align='left'>1008@bos.edu</td><td align='left'></td><td align='left'>220</td><td align='left'>Student</td></tr>
 +
</table>
 +
</div>
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
Line 9: Line 34:
 
   name VARCHAR(20),
 
   name VARCHAR(20),
 
   salary INTEGER );
 
   salary INTEGER );
INSERT INTO staff VALUES (0173,stan@bos.edu,Stern','Stan,99','000);
+
INSERT INTO staff VALUES (0173,'stan@bos.edu','Stern, Stan',99000);
INSERT INTO staff VALUES (0101,ali@bos.edu,Aloof','Alison,30','000);
+
INSERT INTO staff VALUES (0101,'ali@bos.edu','Aloof, Alison',30000);
 
CREATE TABLE student (
 
CREATE TABLE student (
 
   id INTEGER,
 
   id INTEGER,
Line 16: Line 41:
 
   lname VARCHAR(20),
 
   lname VARCHAR(20),
 
   gpa INTEGER );
 
   gpa INTEGER );
INSERT INTO staff VALUES (1007,Peter,Perfect,590);
+
INSERT INTO student VALUES (1007,'Peter','Perfect',590);
INSERT INTO staff VALUES (1008,Donald,Dunce,220);
+
INSERT INTO student VALUES (1008,'Donald','Dunce',220);
 
</source>
 
</source>
 
<div>
 
<div>
<p>You can use UNION when you have two tables you want to combine
+
<p>You can use <code>UNION</code> when you have two tables you want to combine
 
but that contain different data.</p>
 
but that contain different data.</p>
<p>You will have to make the two tables agree before you can do the UNION though</p>
+
<p>You will have to make the two tables agree before you can do the <code>UNION</code> though, this is done by making the
<p>In this example a staff table and a student table are combined</p>
+
final table contain all information from all tables with <code>NULL</code> entries in the rows that don't
 +
have the data required.</p>
 +
<p>In this example a staff table and a student table are combined.</p>
 
</div>
 
</div>
<source lang='sql' class='def e-oracle'>SELECT 'F' || staffid id, name name,
+
<source lang='sql' class='def e-oracle'>
 +
SELECT 'F' || staffid id, name name,
 
email email, salary salary, NULL gpa, 'Staff' species
 
email email, salary salary, NULL gpa, 'Staff' species
FROM staff
+
  FROM staff
UNION
+
  UNION
SELECT 'S' || id id, lname || ',' || fname name,
+
  SELECT 'S' || id id, lname || ',' || fname name,
 
id || '@bos.edu' email, NULL salary, gpa gpa,
 
id || '@bos.edu' email, NULL salary, gpa gpa,
 
'Student' species
 
'Student' species
FROM student</source>
+
    FROM student</source>
<source lang='sql' class='def e-postgres'>SELECT 'F' || staffid id, name name,
+
<source lang='sql' class='def e-postgres'>
 +
SELECT 'F' || staffid id, name name,
 
email email, salary salary, NULL gpa, 'Staff' species
 
email email, salary salary, NULL gpa, 'Staff' species
FROM staff
+
  FROM staff
UNION
+
  UNION
SELECT 'S' || id id, lname || ',' || fname name,
+
  SELECT 'S' || id id, lname || ',' || fname name,
 
id || '@bos.edu' email, NULL salary, gpa gpa,
 
id || '@bos.edu' email, NULL salary, gpa gpa,
 
'Student' species
 
'Student' species
FROM student</source>
+
    FROM student</source>
<source lang='sql' class='def e-mysql'>SELECT CONCAT('F',staffid) id, name name,
+
<source lang='sql' class='def e-mysql'>
 +
SELECT CONCAT('F',staffid) id, name name,
 
email email, salary salary, NULL gpa, 'Staff' species
 
email email, salary salary, NULL gpa, 'Staff' species
FROM staff
+
  FROM staff
UNION
+
  UNION
SELECT CONCAT('S',id) id, CONCAT(lname,',',fname) name,
+
  SELECT CONCAT('S',id) id, CONCAT(lname,',',fname) name,
 
CONCAT(id,'@bos.edu') email, NULL salary, gpa gpa,
 
CONCAT(id,'@bos.edu') email, NULL salary, gpa gpa,
 
'Student' species
 
'Student' species
FROM student</source>
+
    FROM student</source>
<source lang='sql' class='def e-sqlserver'>SELECT 'F' + staffid id, name name,
+
<source lang='sql' class='def e-sqlserver'>
 +
SELECT 'F' + staffid id, name name,
 
email email, salary salary, NULL gpa, 'Staff' species
 
email email, salary salary, NULL gpa, 'Staff' species
FROM staff
+
  FROM staff
UNION
+
  UNION
SELECT 'S' + id id, lname + ',' + fname name,
+
  SELECT 'S' + id id, lname + ',' + fname name,
 
id + '@bos.edu' email, NULL salary, gpa gpa,
 
id + '@bos.edu' email, NULL salary, gpa gpa,
 
'Student' species
 
'Student' species
FROM student</source>
+
    FROM student</source>
<source lang='sql' class='def e-access'>SELECT 'F' + staffid id, name name,
+
<source lang='sql' class='def e-access'>
 +
SELECT 'F' + staffid id, name name,
 
email email, salary salary, NULL gpa, 'Staff' species
 
email email, salary salary, NULL gpa, 'Staff' species
FROM staff
+
  FROM staff
UNION
+
  UNION
SELECT 'S' + id id, lname + ',' + fname name,
+
  SELECT 'S' + id id, lname + ',' + fname name,
 
id + '@bos.edu' email, NULL salary, gpa gpa,
 
id + '@bos.edu' email, NULL salary, gpa gpa,
 
'Student' species
 
'Student' species
FROM student</source>
+
  FROM student</source>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
</div>
 
</div>
  
 
{{Hacks Ref}}
 
{{Hacks Ref}}

Latest revision as of 15:12, 2 August 2012

Combine tables containing different data.

In this example you are shown how to take tables with different data and put them into a single table

that is more understandable allowing all the information from two or more tables to be seen.

Table 1 and table 2 show the two tables you want to combine and table 3 is the table they are combined into.

Table 1
staffidemailnamesalary
0173stan@bos.eduStern, Stan99000
0101ali@bos.eduAloof, Alison30000
Table 2
idfnamelnamegpa
1007PeterPerfect590
1008DonaldDunce220
Table 3
idnameemailsalarygpaspecies
F173Stern,Stanstan@bos.edu99000Staff
F101Aloof,Alisonali@bos.edu30000Staff
S1007Perfect,Peter1007@bos.edu590Student
S1008Dunce,Donald1008@bos.edu220Student
schema:scott
DROP TABLE staff;
DROP TABLE student;
 CREATE TABLE staff(
  staffid INTEGER,
  email VARCHAR(20),
  name VARCHAR(20),
  salary INTEGER );
INSERT INTO staff VALUES (0173,'stan@bos.edu','Stern, Stan',99000);
INSERT INTO staff VALUES (0101,'ali@bos.edu','Aloof, Alison',30000);
CREATE TABLE student (
  id INTEGER,
  fname VARCHAR(20),
  lname VARCHAR(20),
  gpa INTEGER );
INSERT INTO student VALUES (1007,'Peter','Perfect',590);
INSERT INTO student VALUES (1008,'Donald','Dunce',220);

You can use UNION when you have two tables you want to combine but that contain different data.

You will have to make the two tables agree before you can do the UNION though, this is done by making the final table contain all information from all tables with NULL entries in the rows that don't have the data required.

In this example a staff table and a student table are combined.

SELECT 'F' || staffid id, name name,
email email, salary salary, NULL gpa, 'Staff' species
  FROM staff
  UNION
  SELECT 'S' || id id, lname || ',' || fname name,
id || '@bos.edu' email, NULL salary, gpa gpa,
'Student' species
    FROM student
SELECT 'F' || staffid id, name name,
email email, salary salary, NULL gpa, 'Staff' species
  FROM staff
  UNION
  SELECT 'S' || id id, lname || ',' || fname name,
id || '@bos.edu' email, NULL salary, gpa gpa,
'Student' species
    FROM student
SELECT CONCAT('F',staffid) id, name name,
email email, salary salary, NULL gpa, 'Staff' species
  FROM staff
  UNION
  SELECT CONCAT('S',id) id, CONCAT(lname,',',fname) name,
CONCAT(id,'@bos.edu') email, NULL salary, gpa gpa,
'Student' species
    FROM student
SELECT 'F' + staffid id, name name,
email email, salary salary, NULL gpa, 'Staff' species
  FROM staff
  UNION
  SELECT 'S' + id id, lname + ',' + fname name,
id + '@bos.edu' email, NULL salary, gpa gpa,
'Student' species
    FROM student
SELECT 'F' + staffid id, name name,
email email, salary salary, NULL gpa, 'Staff' species
  FROM staff
  UNION
  SELECT 'S' + id id, lname + ',' + fname name,
id + '@bos.edu' email, NULL salary, gpa gpa,
'Student' species
   FROM student
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense