Hello,
I have a question about the query below. I can't understand how T-SQL understands 1 in SELECT clause ? Could someone explain me this ? Dose SELECT * is the same if not why ? Where this 1 comes from and why second table doesn't have AS any more ? Thank you
SELECT country, region, city FROM (SELECT 1 AS sortcol, country, region, city FROM HR.Employees UNION ALL SELECT 2, country, region, city FROM Production.Suppliers) AS D ORDER BY sortcol, country, region, city;
Here is the task I was solving:
You are given the following query.
SELECT country, region, city FROM HR.Employees UNION ALL SELECT country, region, city FROM Production.Suppliers; You are asked to add logic to the query so that it guarantees that the rows from Employees are returned in the output before the rows from Suppliers. Also, within each segment, the rows should be sorted by country, region, and city.
■■ Tables involved: HR.Employees and Production.Suppliers ■■ Desired output: country region city --------------- --------------- --------------- UK NULL London UK NULL London UK NULL London UK NULL London USA WA Kirkland USA WA Redmond USA WA Seattle USA WA Seattle USA WA Tacoma Australia NSW Sydney Australia Victoria Melbourne Brazil NULL Sao Paulo Canada Québec Montréal Canada Québec Ste-Hyacinthe Denmark NULL Lyngby Finland NULL Lappeenranta France NULL Annecy France NULL Montceau France NULL Paris Germany NULL Berlin Germany NULL Cuxhaven Germany NULL Frankfurt Italy NULL Ravenna Italy NULL Salerno Japan NULL Osaka Japan NULL Tokyo Netherlands NULL Zaandam Norway NULL Sandvika Singapore NULL Singapore Spain Asturias Oviedo Sweden NULL Göteborg Sweden NULL Stockholm UK NULL London UK NULL Manchester USA LA New Orleans USA MA Boston USA MI Ann Arbor USA OR Bend