Book Image

SQL Server 2014 Development Essentials

By : Basit A. Masood-Al-Farooq
Book Image

SQL Server 2014 Development Essentials

By: Basit A. Masood-Al-Farooq

Overview of this book

Table of Contents (14 chapters)
SQL Server 2014 Development Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Organizing and grouping data


We can use the SELECT statement and its clauses and keywords to organize and summarize data. In this section, we will cover these clauses.

The ORDER BY clause

By default, when you query the data in a table with no clustered index, SQL Server does not guarantee the order in which data is returned and returns rows in a random order. If the table has a clustered index, SQL Server returns the rows in a clustered index order. Therefore, we use the ORDER BY clause in your SELECT statements to sort the data returned by the query based on the columns' sort order specified in the ORDER BY clause. The ORDER BY clause guarantees the order in which data is returned.

For example, to display a sales representative's sales-related information ordered by the FirstName and LastName values, we would run the following query:

SELECT  *
FROM    [Sales].[vSalesPerson]
ORDER BY [FirstName] ASC,
         [LastName] ASC;

The GROUP BY clause

The GROUP BY clause is used to divide the table into...