Working with Unions - Exercise

Contact Us or call 1-877-932-8228
Working with Unions - Exercise

Working with Unions

Duration: 10 to 20 minutes.

In this exercise, you will practice using UNION.

  1. Create a report showing the contact name and phone numbers for all employees, customers, and suppliers.

Solution:

SubqueriesJoinsUnions/Solutions/Unions.sql
/******************************
SQL Server Solution
******************************/
SELECT FirstName + ' ' + LastName AS Contact, HomePhone As Phone
FROM Employees
	UNION
SELECT ContactName, Phone
FROM Customers
	UNION
SELECT ContactName, Phone
FROM Suppliers
ORDER BY Contact;

/******************************
Oracle Solution
******************************/
SELECT FirstName || ' ' || LastName AS Contact, HomePhone As Phone
FROM Employees
	UNION
SELECT ContactName AS Contact, Phone
FROM Customers
	UNION
SELECT ContactName AS Contact, Phone
FROM Suppliers
ORDER BY Contact;

/******************************
MySQL Solution
******************************/
SELECT CONCAT(FirstName, ' ', LastName) AS Contact, HomePhone As Phone
FROM Employees
	UNION
SELECT ContactName, Phone
FROM Customers
	UNION
SELECT ContactName, Phone
FROM Suppliers
ORDER BY Contact;
Next