is it possible to UNION and INNER JOIN for sql

时间:2018-02-03 11:03:29

标签: mysql sql-server join union

im using the w3schools sql database to learn and i have come across a question. is it possible to sort the results according to CustomerName after i have done a INNER JOIN? this is the INNER JOIN statement

SELECT Orders.OrderID, Customers.CustomerName, Customers.ContactName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

1 个答案:

答案 0 :(得分:0)

The UNION command in SQL is used to combine the results of your first query with the entire results of a second query that has the same columns. It is not used for ordering the first query. With your INNER JOIN you can sort the results just by adding ORDER BY:

SELECT Orders.OrderID, Customers.CustomerName, Customers.ContactName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
ORDER BY Customers.CustomerName;