AdventureWorks Database: Returns all rows and a subset of the columns

SQL Query - AdventureWorks: Exercise-3 with Solution

3. From the following table write a query in SQL to return all rows and a subset of the columns (FirstName, LastName, businessentityid) from the person table in the AdventureWorks database. The third column heading is renamed to Employee_id. Arranged the output in ascending order by lastname.

Sample table: person.person

Click to view Full table

Sample Solution:

SELECT firstname, lastname, businessentityid as Employee_id  
FROM person.person AS e  
ORDER BY lastname;

Sample Output:

firstname               |lastname              |employee_id|
Syed                    |Abbas                 |        285|
Catherine               |Abel                  |        293|
Kim                     |Abercrombie           |         38|
Kim                     |Abercrombie           |        295|
Kim                     |Abercrombie           |       2170|
Sam                     |Abolrous              |       2357|
Hazem                   |Abolrous              |        211|
Humberto                |Acevedo               |        297|
Gustavo                 |Achong                |        291|
Pilar                   |Ackerman              |        299|
Pilar                   |Ackerman              |        121|
Luke                    |Adams                 |      16884|
Adam                    |Adams                 |      16901|
Natalie                 |Adams                 |      10262|
Isabella                |Adams                 |      10261|
Morgan                  |Adams                 |      10259|
Kaitlyn                 |Adams                 |      10258|

SQL AdventureWorks Editor:

Practice Online

Contribute your code and comments through Disqus.

Previous: Retrieve all rows, columns using table aliasing.
Next: Retrieve only the rows for product.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.