MOCKSTACKS
EN
Questions And Answers

More Tutorials









SQLServer SELECT

Select Individual Columns


SELECT
 PhoneNumber,
 Email,
 PreferredContact
FROM Customers

This statement will return the columns PhoneNumber, Email, and PreferredContact from all rows of the Customers table. Also the columns will be returned in the sequence in which they appear in the SELECT clause.

If multiple tables are joined together, you can select columns from specific tables by specifying the table name before the column name: [table_name].[column_name]

SELECT
 Customers.PhoneNumber,
 Customers.Email,
 Customers.PreferredContact,
 Orders.Id AS OrderId
FROM
 Customers
LEFT JOIN
 Orders ON Orders.CustomerId = Customers.Id

*AS OrderId means that the Id field of Orders table will be returned as a column named OrderId. See selecting with column alias for further information.

To avoid using long table names, you can use table aliases. This mitigates the pain of writing long table names for each field that you select in the joins. If you are performing a self join (a join between two instances of the same table), then you must use table aliases to distinguish your tables. We can write a table alias like Customers c or
Customers AS c. Here c works as an alias for Customers and we can select let's say Email like this: c.Email.

SELECT
 c.PhoneNumber,
 c.Email,
 c.PreferredContact,
 o.Id AS OrderId
FROM
 Customers c
LEFT JOIN
 Orders o ON o.CustomerId = c.Id



Conclusion

In this page (written and validated by ) you learned about SQLServer SELECT . What's Next? If you are interested in completing SQLServer tutorial, your next topic will be learning about: SQLServer GROUP BY.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.