Difference between revisions of "Union"

From SQLZOO
Jump to: navigation, search
Line 5: Line 5:
 
List a number of SELECT statements separated by the UNION key word. Be sure that you have the same number of columns in each of the SELECT statements.  
 
List a number of SELECT statements separated by the UNION key word. Be sure that you have the same number of columns in each of the SELECT statements.  
 
</div>
 
</div>
<source lang=sql class='tidy'>DROP TABLE bbc;</source>
+
<source lang=sql class='tidy'>DROP TABLE bbc;
 +
DROP TABLE actor;</source>
 
<source lang=sql class='setup'>CREATE TABLE bbc (name VARCHAR(10), region VARCHAR(10));
 
<source lang=sql class='setup'>CREATE TABLE bbc (name VARCHAR(10), region VARCHAR(10));
 
INSERT INTO bbc VALUES ('Poland', 'Europe');
 
INSERT INTO bbc VALUES ('Poland', 'Europe');
 
INSERT INTO bbc VALUES ('Japan', 'Asia');
 
INSERT INTO bbc VALUES ('Japan', 'Asia');
 +
INSERT INTO bbc VALUES ('Zambia', 'Africa');
 +
CREATE TABLE actor(name VARCHAR(30), id INT);
 +
INSERT INTO actor VALUES ('Zack Williams', '1');
 +
INSERT INTO actor VALUES ('Zoltán Fábri', '2');
 +
INSERT INTO actor VALUES ('Jean Claude Van Damme', '3');
 
</source>
 
</source>
  

Revision as of 12:09, 12 July 2012

Make union between different tables to build one single view or request?

schema:scott

List a number of SELECT statements separated by the UNION key word. Be sure that you have the same number of columns in each of the SELECT statements.

DROP TABLE bbc;
DROP TABLE actor;
CREATE TABLE bbc (name VARCHAR(10), region VARCHAR(10));
INSERT INTO bbc VALUES ('Poland', 'Europe');
INSERT INTO bbc VALUES ('Japan', 'Asia');
INSERT INTO bbc VALUES ('Zambia', 'Africa');
CREATE TABLE actor(name VARCHAR(30), id INT);
INSERT INTO actor VALUES ('Zack Williams', '1');
INSERT INTO actor VALUES ('Zoltán Fábri', '2');
INSERT INTO actor VALUES ('Jean Claude Van Damme', '3');
SELECT name FROM actor WHERE name LIKE 'Z%'
UNION
SELECT name FROM bbc WHERE name LIKE 'Z%'
SELECT name FROM actor WHERE name LIKE 'Z%'
UNION
SELECT name FROM bbc WHERE name LIKE 'Z%'
SELECT name FROM actor WHERE name LIKE 'Z%'
UNION
SELECT name FROM bbc WHERE name LIKE 'Z%'
SELECT name FROM actor WHERE name LIKE 'Z%'
UNION
SELECT name FROM bbc WHERE name LIKE 'Z%'
UNION will be available from version 4
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense