SELECT の例 (Transact-SQL)SELECT Examples (Transact-SQL)

適用対象:○SQL Server (2008 以降)○Azure SQL Database×Azure SQL Data Warehouse ×Parallel Data Warehouse THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008)yesAzure SQL DatabasenoAzure SQL Data Warehouse noParallel Data Warehouse

ここでは、SELECT ステートメントの使用例を紹介します。This topic provides examples of using the SELECT statement.

A.A. SELECT を使用して行および列を取得するUsing SELECT to retrieve rows and columns

3 つのプログラム例を次に示します。The following example shows three code examples. 最初の例では、 AdventureWorks2012AdventureWorks2012 データベース内の * テーブルから、WHERE 句を指定せずにすべての行を返し、また Product を使用してすべての列を返しています。This first code example returns all rows (no WHERE clause is specified) and all columns (using the *) from the Product table in the AdventureWorks2012AdventureWorks2012 database.

USE AdventureWorks2012;
GO
SELECT *
FROM Production.Product
ORDER BY Name ASC;
-- Alternate way.
USE AdventureWorks2012;
GO
SELECT p.*
FROM Production.Product AS p
ORDER BY Name ASC;
GO

この例では、 AdventureWorks2012AdventureWorks2012 データベース内の Name テーブルから、WHERE 句を指定せずにすべての行と、一部の列 (ProductNumberListPriceProduct) のみを返しています。This example returns all rows (no WHERE clause is specified), and only a subset of the columns (Name, ProductNumber, ListPrice) from the Product table in the AdventureWorks2012AdventureWorks2012 database. さらに、列ヘッダーが追加されています。Additionally, a column heading is added.

USE AdventureWorks2012;
GO
SELECT Name, ProductNumber, ListPrice AS Price
FROM Production.Product 
ORDER BY Name ASC;
GO

この例では、製品ラインが R で、製造所要日数が 4 日未満の Product の行のみを返しています。This example returns only the rows for Product that have a product line of R and that have days to manufacture that is less than 4.

USE AdventureWorks2012;
GO
SELECT Name, ProductNumber, ListPrice AS Price
FROM Production.Product 
WHERE ProductLine = 'R' 
AND DaysToManufacture < 4
ORDER BY Name ASC;
GO

B.B. 列ヘッダーおよび計算処理と共に SELECT を使用するUsing SELECT with column headings and calculations

次の例では、Product テーブルのすべての行を返します。The following examples return all rows from the Product table. 最初の例では、各製品の売上合計と売上割引を返します。The first example returns total sales and the discounts for each product. 2 番目の例では、製品ごとの合計収入が計算されます。In the second example, the total revenue is calculated for each product.

USE AdventureWorks2012;
GO
SELECT p.Name AS ProductName, 
NonDiscountSales = (OrderQty * UnitPrice),
Discounts = ((OrderQty * UnitPrice) * UnitPriceDiscount)
FROM Production.Product AS p 
INNER JOIN Sales.SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID 
ORDER BY ProductName DESC;
GO

次の例は、販売注文ごとに各製品の収入を計算するクエリです。This is the query that calculates the revenue for each product in each sales order.

USE AdventureWorks2012;
GO
SELECT 'Total income is', ((OrderQty * UnitPrice) * (1.0 - UnitPriceDiscount)), ' for ',
p.Name AS ProductName 
FROM Production.Product AS p 
INNER JOIN Sales.SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID 
ORDER BY ProductName ASC;
GO

C.C. DISTINCT を SELECT と共に使用するUsing DISTINCT with SELECT

次の例では、DISTINCT を使って重複しているタイトルを取得しないようにしています。The following example uses DISTINCT to prevent the retrieval of duplicate titles.

USE AdventureWorks2012;
GO
SELECT DISTINCT JobTitle
FROM HumanResources.Employee
ORDER BY JobTitle;
GO

D.D. SELECT INTO を使用してテーブルを作成するCreating tables with SELECT INTO

次の最初の例では、#Bicycles 内に tempdb という一時テーブルを作成します。The following first example creates a temporary table named #Bicycles in tempdb.

USE tempdb;
GO
IF OBJECT_ID (N'#Bicycles',N'U') IS NOT NULL
DROP TABLE #Bicycles;
GO
SELECT * 
INTO #Bicycles
FROM AdventureWorks2012.Production.Product
WHERE ProductNumber LIKE 'BK%';
GO

2 番目の例では、NewProducts という名前のパーマネント テーブルを作成します。This second example creates the permanent table NewProducts.

USE AdventureWorks2012;
GO
IF OBJECT_ID('dbo.NewProducts', 'U') IS NOT NULL
    DROP TABLE dbo.NewProducts;
GO
ALTER DATABASE AdventureWorks2012 SET RECOVERY BULK_LOGGED;
GO

SELECT * INTO dbo.NewProducts
FROM Production.Product
WHERE ListPrice > $25 
AND ListPrice < $100;
GO
ALTER DATABASE AdventureWorks2012 SET RECOVERY FULL;
GO

E.E. 相関サブクエリを使用するUsing correlated subqueries

次の例では、EXISTS キーワードと IN キーワードを使用した意味的に等しいクエリと、それらの違いを示します。The following example shows queries that are semantically equivalent and illustrates the difference between using the EXISTS keyword and the IN keyword. いずれも、製品モデルが長袖ジャージで、ProductModelID テーブルと Product テーブルの間で ProductModel 番号が一致する各製品名の 1 つのインスタンスを取得する有効なサブクエリの例です。Both are examples of a valid subquery that retrieves one instance of each product name for which the product model is a long sleeve logo jersey, and the ProductModelID numbers match between the Product and ProductModel tables.

USE AdventureWorks2012;
GO
SELECT DISTINCT Name
FROM Production.Product AS p 
WHERE EXISTS
    (SELECT *
     FROM Production.ProductModel AS pm 
     WHERE p.ProductModelID = pm.ProductModelID
           AND pm.Name LIKE 'Long-Sleeve Logo Jersey%');
GO

-- OR

USE AdventureWorks2012;
GO
SELECT DISTINCT Name
FROM Production.Product
WHERE ProductModelID IN
    (SELECT ProductModelID 
     FROM Production.ProductModel
     WHERE Name LIKE 'Long-Sleeve Logo Jersey%');
GO

次の例では、相関または繰り返しサブクエリ内で IN を使用しています。The following example uses IN in a correlated, or repeating, subquery. これは、外側のクエリによって値が決まるクエリです。This is a query that depends on the outer query for its values. このクエリは、外側のクエリが選択する行に対して 1 回ずつ、繰り返し実行されます。The query is executed repeatedly, one time for each row that may be selected by the outer query. このクエリは、SalesPerson テーブルのボーナス額が 5000.00 で、従業員の ID 番号が Employee テーブルと SalesPerson テーブルで一致する各従業員の姓名のインスタンスを 1 つ取得します。This query retrieves one instance of the first and last name of each employee for which the bonus in the SalesPerson table is 5000.00 and for which the employee identification numbers match in the Employee and SalesPerson tables.

USE AdventureWorks2012;
GO
SELECT DISTINCT p.LastName, p.FirstName 
FROM Person.Person AS p 
JOIN HumanResources.Employee AS e
    ON e.BusinessEntityID = p.BusinessEntityID WHERE 5000.00 IN
    (SELECT Bonus
     FROM Sales.SalesPerson AS sp
     WHERE e.BusinessEntityID = sp.BusinessEntityID);
GO

上記のステートメントのサブクエリは、外側のクエリから独立して評価できません。The previous subquery in this statement cannot be evaluated independently of the outer query. このサブクエリは Employee.EmployeeID の値を必要としますが、この値は、 SQL Server データベース エンジンSQL Server Database Engineが調べる Employee の行によって変化します。It requires a value for Employee.EmployeeID, but this value changes as the SQL Server データベース エンジンSQL Server Database Engine examines different rows in Employee.

相関サブクエリは、外側のクエリの HAVING 句でも使えます。A correlated subquery can also be used in the HAVING clause of an outer query. この例では、表示価格がモデルの平均値の 2 倍以上の製品モデルを検索します。This example finds the product models for which the maximum list price is more than twice the average for the model.

USE AdventureWorks2012;
GO
SELECT p1.ProductModelID
FROM Production.Product AS p1
GROUP BY p1.ProductModelID
HAVING MAX(p1.ListPrice) >= ALL
    (SELECT AVG(p2.ListPrice)
     FROM Production.Product AS p2
     WHERE p1.ProductModelID = p2.ProductModelID);
GO

この例では、2 つの相関サブクエリを使って、特定の製品を販売した従業員の名前を検索します。This example uses two correlated subqueries to find the names of employees who have sold a particular product.

USE AdventureWorks2012;
GO
SELECT DISTINCT pp.LastName, pp.FirstName 
FROM Person.Person pp JOIN HumanResources.Employee e
ON e.BusinessEntityID = pp.BusinessEntityID WHERE pp.BusinessEntityID IN 
(SELECT SalesPersonID 
FROM Sales.SalesOrderHeader
WHERE SalesOrderID IN 
(SELECT SalesOrderID 
FROM Sales.SalesOrderDetail
WHERE ProductID IN 
(SELECT ProductID 
FROM Production.Product p 
WHERE ProductNumber = 'BK-M68B-42')));
GO

F.F. GROUP BY を使用するUsing GROUP BY

次の例は、データベース内の各販売注文の合計を検索します。The following example finds the total of each sales order in the database.

USE AdventureWorks2012;
GO
SELECT SalesOrderID, SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID
ORDER BY SalesOrderID;
GO

GROUP BY 句があるため、各販売注文につき 1 行だけが返され、この行にその販売注文のすべての売上合計が含まれます。Because of the GROUP BY clause, only one row containing the sum of all sales is returned for each sales order.

G.G. GROUP BY を複数のグループと共に使用するUsing GROUP BY with multiple groups

次の例では、平均価格および今年に入ってからの売り上げの合計を、製品 ID と特価品 ID でグループ化して返します。The following example finds the average price and the sum of year-to-date sales, grouped by product ID and special offer ID.

USE AdventureWorks2012;
GO
SELECT ProductID, SpecialOfferID, AVG(UnitPrice) AS [Average Price], 
    SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail
GROUP BY ProductID, SpecialOfferID
ORDER BY ProductID;
GO

H.H. GROUP BY と WHERE を使用するUsing GROUP BY and WHERE

次の例では、表示価格が $1000 より多い行だけを取得した後、結果をグループ化します。The following example puts the results into groups after retrieving only the rows with list prices greater than $1000.

USE AdventureWorks2012;
GO
SELECT ProductModelID, AVG(ListPrice) AS [Average List Price]
FROM Production.Product
WHERE ListPrice > $1000
GROUP BY ProductModelID
ORDER BY ProductModelID;
GO

I.I. 1 つの式と共に GROUP BY を使用するUsing GROUP BY with an expression

次の例では、式によってグループ化します。The following example groups by an expression. 式に集計関数が含まれない限り、式によってグループ化することができます。You can group by an expression if the expression does not include aggregate functions.

USE AdventureWorks2012;
GO
SELECT AVG(OrderQty) AS [Average Quantity], 
NonDiscountSales = (OrderQty * UnitPrice)
FROM Sales.SalesOrderDetail
GROUP BY (OrderQty * UnitPrice)
ORDER BY (OrderQty * UnitPrice) DESC;
GO

J.J. ORDER BY と共に GROUP BY を使用するUsing GROUP BY with ORDER BY

次の例では、それぞれの製品の種類別の平均価格を求め、その結果を平均価格の順序で表示しています。The following example finds the average price of each type of product and orders the results by average price.

USE AdventureWorks2012;
GO
SELECT ProductID, AVG(UnitPrice) AS [Average Price]
FROM Sales.SalesOrderDetail
WHERE OrderQty > 10
GROUP BY ProductID
ORDER BY AVG(UnitPrice);
GO

K.K. HAVING 句を使用するUsing the HAVING clause

最初の例では、HAVING 句を集計関数と共に使用しています。The first example that follows shows a HAVING clause with an aggregate function. SalesOrderDetail テーブルの行を製品 ID 別にグループ化し、平均注文数が 5 以下の製品を除外しています。It groups the rows in the SalesOrderDetail table by product ID and eliminates products whose average order quantities are five or less. 2 番目の例では、HAVING 句を集計関数なしで使用しています。The second example shows a HAVING clause without aggregate functions.

USE AdventureWorks2012;
GO
SELECT ProductID 
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING AVG(OrderQty) > 5
ORDER BY ProductID;
GO

次のクエリでは、LIKE 句の中で HAVING 句を使用しています。This query uses the LIKE clause in the HAVING clause.

USE AdventureWorks2012 ;  
GO  
SELECT SalesOrderID, CarrierTrackingNumber   
FROM Sales.SalesOrderDetail  
GROUP BY SalesOrderID, CarrierTrackingNumber  
HAVING CarrierTrackingNumber LIKE '4BD%'  
ORDER BY SalesOrderID ;  
GO  

L.L. HAVING と GROUP BY を使用するUsing HAVING and GROUP BY

次の例では、1 つの SELECT ステートメントの中で GROUP BY 句、HAVING 句、WHERE 句、および ORDER BY 句を使用しています。The following example shows using GROUP BY, HAVING, WHERE, and ORDER BY clauses in one SELECT statement. これによって、$25 より高く平均注文数量が 5 未満の製品を除外した、グループとサマリー値が作成されます。It produces groups and summary values but does so after eliminating the products with prices over $25 and average order quantities under 5. この結果は ProductID 別にまとめられます。It also organizes the results by ProductID.

USE AdventureWorks2012;
GO
SELECT ProductID 
FROM Sales.SalesOrderDetail
WHERE UnitPrice < 25.00
GROUP BY ProductID
HAVING AVG(OrderQty) > 5
ORDER BY ProductID;
GO

M.M. HAVING を SUM および AVG と共に使用するUsing HAVING with SUM and AVG

次の例では、SalesOrderDetail テーブルから、注文合計額が $1000000.00 を超え、かつ平均注文数が 3 未満の製品を製品 ID 別にグループ化します。The following example groups the SalesOrderDetail table by product ID and includes only those groups of products that have orders totaling more than $1000000.00 and whose average order quantities are less than 3.

USE AdventureWorks2012;
GO
SELECT ProductID, AVG(OrderQty) AS AverageQuantity, SUM(LineTotal) AS Total
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) > $1000000.00
AND AVG(OrderQty) < 3;
GO

売上合計が $2000000.00 を超える製品を検索するには、このクエリを使用します。To see the products that have had total sales greater than $2000000.00, use this query:

USE AdventureWorks2012;
GO
SELECT ProductID, Total = SUM(LineTotal)
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) > $2000000.00;
GO

各製品の集計に最低 1,500 の品目が含まれているようにするには、HAVING COUNT(*) > 1500 を使って、1500 未満の合計を返す製品を除外します。If you want to make sure there are at least one thousand five hundred items involved in the calculations for each product, use HAVING COUNT(*) > 1500 to eliminate the products that return totals for fewer than 1500 items sold. クエリは次のようになります。The query looks like this:

USE AdventureWorks2012;
GO
SELECT ProductID, SUM(LineTotal) AS Total
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING COUNT(*) > 1500;
GO

N.N. INDEX オプティマイザー ヒントを使用するUsing the INDEX optimizer hint

次の例では、INDEX オプティマイザー ヒントの使用方法を 2 とおり示します。The following example shows two ways to use the INDEX optimizer hint. 最初の例では、オプティマイザーで非クラスター化インデックスを使用し、テーブルから行を取得しています。2 番目の例では、index = 0 を使ってテーブル スキャンを実行しています。The first example shows how to force the optimizer to use a nonclustered index to retrieve rows from a table, and the second example forces a table scan by using an index of 0.

USE AdventureWorks2012;
GO
SELECT pp.FirstName, pp.LastName, e.NationalIDNumber
FROM HumanResources.Employee AS e WITH (INDEX(AK_Employee_NationalIDNumber))
JOIN Person.Person AS pp on e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO

-- Force a table scan by using INDEX = 0.
USE AdventureWorks2012;
GO
SELECT pp.LastName, pp.FirstName, e.JobTitle
FROM HumanResources.Employee AS e WITH (INDEX = 0) JOIN Person.Person AS pp
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO

M.M. OPTION ヒントと GROUP ヒントを使用するUsing OPTION and the GROUP hints

次の例では、OPTION (GROUP) 句と共に GROUP BY 句を使用する方法を示します。The following example shows how the OPTION (GROUP) clause is used with a GROUP BY clause.

USE AdventureWorks2012;
GO
SELECT ProductID, OrderQty, SUM(LineTotal) AS Total
FROM Sales.SalesOrderDetail
WHERE UnitPrice < $5.00
GROUP BY ProductID, OrderQty
ORDER BY ProductID, OrderQty
OPTION (HASH GROUP, FAST 10);
GO

O. O. UNION クエリ ヒントを使用するUsing the UNION query hint

次の例では、MERGE UNION クエリ ヒントを使用します。The following example uses the MERGE UNION query hint.

USE AdventureWorks2012;
GO
SELECT BusinessEntityID, JobTitle, HireDate, VacationHours, SickLeaveHours
FROM HumanResources.Employee AS e1
UNION
SELECT BusinessEntityID, JobTitle, HireDate, VacationHours, SickLeaveHours
FROM HumanResources.Employee AS e2
OPTION (MERGE UNION);
GO

P.P. 単純な UNION を使用するUsing a simple UNION

次の例では、結果セットに ProductModelID テーブルと Name テーブルの ProductModel 列と Gloves 列の内容が含まれています。In the following example, the result set includes the contents of the ProductModelID and Name columns of both the ProductModel and Gloves tables.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.Gloves', 'U') IS NOT NULL
DROP TABLE dbo.Gloves;
GO
-- Create Gloves table.
SELECT ProductModelID, Name
INTO dbo.Gloves
FROM Production.ProductModel
WHERE ProductModelID IN (3, 4);
GO

-- Here is the simple union.
USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves
ORDER BY Name;
GO

Q. Q. UNION と共に SELECT INTO を使用するUsing SELECT INTO with UNION

この例では、2 番目の INTO ステートメントの SELECT 句で、ProductResults および ProductModel テーブルの指定された列のユニオンの最終的な結果セットを Gloves という名前のテーブルに格納することを指定します。In the following example, the INTO clause in the second SELECT statement specifies that the table named ProductResults holds the final result set of the union of the designated columns of the ProductModel and Gloves tables. Gloves テーブルは、最初の SELECT ステートメントで作成されます。Note that the Gloves table is created in the first SELECT statement.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.ProductResults', 'U') IS NOT NULL
DROP TABLE dbo.ProductResults;
GO
IF OBJECT_ID ('dbo.Gloves', 'U') IS NOT NULL
DROP TABLE dbo.Gloves;
GO
-- Create Gloves table.
SELECT ProductModelID, Name
INTO dbo.Gloves
FROM Production.ProductModel
WHERE ProductModelID IN (3, 4);
GO

USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
INTO dbo.ProductResults
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves;
GO

SELECT ProductModelID, Name 
FROM dbo.ProductResults;

R. R. ORDER BY 句を指定した 2 つの SELECT ステートメントで UNION 句を使用するUsing UNION of two SELECT statements with ORDER BY

UNION 句で使用するある種のパラメーターの順序には重要な意味があります。The order of certain parameters used with the UNION clause is important. 次の例では、出力時に列名を変更する 2 つの SELECT ステートメントでの UNION の誤った使用法と正しい使用法を示しています。The following example shows the incorrect and correct use of UNION in two SELECT statements in which a column is to be renamed in the output.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.Gloves', 'U') IS NOT NULL
DROP TABLE dbo.Gloves;
GO
-- Create Gloves table.
SELECT ProductModelID, Name
INTO dbo.Gloves
FROM Production.ProductModel
WHERE ProductModelID IN (3, 4);
GO

/* INCORRECT */
USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
ORDER BY Name
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves;
GO

/* CORRECT */
USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves
ORDER BY Name;
GO

S. S. 3 つの SELECT ステートメントで UNION を使用して、ALL とかっこの効果を示すUsing UNION of three SELECT statements to show the effects of ALL and parentheses

次の例では、UNION を使用して 3 つのテーブルのクエリ結果を結合します。これらのテーブルはすべて同じ 5 行のデータで構成されます。The following examples use UNION to combine the results of three tables that all have the same 5 rows of data. 最初の例では、UNION ALL を使用して、重複するレコードも含めて 15 行すべてを返します。The first example uses UNION ALL to show the duplicated records, and returns all 15 rows. 2 番目の例では、ALL を指定せずに UNION を使用して、3 つの SELECT ステートメントの結果を結合したものから重複する行を削除し、5 行を返します。The second example uses UNION without ALL to eliminate the duplicate rows from the combined results of the three SELECT statements, and returns 5 rows.

3 番目の例では、最初の ALL と共に UNION を使用し、UNION を使用していない 2 番目の ALL をかっこで囲んでいます。The third example uses ALL with the first UNION and parentheses enclose the second UNION that is not using ALL. 2 番目の UNION はかっこで囲まれているので、最初に処理されます。また、ALL オプションを使用せずに重複を削除するので、5 行を返します。The second UNION is processed first because it is in parentheses, and returns 5 rows because the ALL option is not used and the duplicates are removed. これらの 5 行は、UNION ALL キーワードを使用して最初の SELECT の結果と結合されます。These 5 rows are combined with the results of the first SELECT by using the UNION ALL keywords. これによって 2 組の 5 行の間での重複が削除されることはありません。This does not remove the duplicates between the two sets of 5 rows. 最終的な結果は 10 行になります。The final result has 10 rows.

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.EmployeeOne', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeOne;
GO
IF OBJECT_ID ('dbo.EmployeeTwo', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeTwo;
GO
IF OBJECT_ID ('dbo.EmployeeThree', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeThree;
GO

SELECT pp.LastName, pp.FirstName, e.JobTitle 
INTO dbo.EmployeeOne
FROM Person.Person AS pp JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO
SELECT pp.LastName, pp.FirstName, e.JobTitle 
INTO dbo.EmployeeTwo
FROM Person.Person AS pp JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO
SELECT pp.LastName, pp.FirstName, e.JobTitle 
INTO dbo.EmployeeThree
FROM Person.Person AS pp JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = pp.BusinessEntityID
WHERE LastName = 'Johnson';
GO
-- Union ALL
SELECT LastName, FirstName, JobTitle
FROM dbo.EmployeeOne
UNION ALL
SELECT LastName, FirstName ,JobTitle
FROM dbo.EmployeeTwo
UNION ALL
SELECT LastName, FirstName,JobTitle 
FROM dbo.EmployeeThree;
GO

SELECT LastName, FirstName,JobTitle
FROM dbo.EmployeeOne
UNION 
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeTwo
UNION 
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeThree;
GO

SELECT LastName, FirstName,JobTitle 
FROM dbo.EmployeeOne
UNION ALL
(
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeTwo
UNION
SELECT LastName, FirstName, JobTitle 
FROM dbo.EmployeeThree
);
GO

参照See Also

CREATE TRIGGER (Transact-SQL) CREATE TRIGGER (Transact-SQL)
CREATE VIEW (Transact-SQL) CREATE VIEW (Transact-SQL)
DELETE (Transact-SQL) DELETE (Transact-SQL)
EXECUTE (Transact-SQL) EXECUTE (Transact-SQL)
式 (Transact-SQL) Expressions (Transact-SQL)
INSERT (Transact-SQL) INSERT (Transact-SQL)
LIKE (Transact-SQL) LIKE (Transact-SQL)
UNION (Transact-SQL) UNION (Transact-SQL)
EXCEPT および INTERSECT (Transact-SQL) EXCEPT and INTERSECT (Transact-SQL)
UPDATE (Transact-SQL) UPDATE (Transact-SQL)
WHERE (Transact-SQL) WHERE (Transact-SQL)
PathName (Transact-SQL) PathName (Transact-SQL)
INTO 句 (Transact-SQL)INTO Clause (Transact-SQL)