IT tutorials
 
Technology
 

SQL Server 2012 : Ordering the Result Set (part 1) - Specifying the ORDER BY Using Column Names, Specifying the ORDER BY Using Expressions

10/18/2014 8:41:34 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Logically, relational data should always be considered an unordered list. The primary key's purpose is to uniquely identify the row, not sort the table. SQL Server usually returns the data in the order of the primary key (because that's probably the clustered index), but there's no logical guarantee of that order. The only correct way to sort the results is with an ORDER BY clause.

SQL can sort by multiple columns, and the sort columns don't have to be columns that are returned by the SELECT, so there's a lot of flexibility in how you can specify the columns. Using Management Studio's Query Designer, you can create the ORDER BY by selecting the sort order for the column, as shown in Figure 1.

Figure 1 Within Management Studio's Query Designer, you can define the sort order and sort type in the column pane. The TOP() predicate is set for the Query Designer inside the query's Properties page.

6.4

Although there is not a limit on the number of columns that you can specify in the ORDER BY clause of the select statement, an internal operation exists that indirectly enforces a limit.

Specifying the ORDER BY Using Column Names

The best way to sort the result set is to completely spell out the ORDER BY columns:

USE AdventureWorks;

SELECT FirstName, LastName
FROM Person.Person

ORDER BY LastName, FirstName;

Result:

FirstName   LastName
------------- --------------------
Syed Abbas
Catherine Abel
Kim Abercrombie
. . .
Note
ORDER BY and the order of columns in the select list are completely independent.

Specifying the ORDER BY Using Expressions

In the case of sorting by an expression, you can repeat the entire expression in the ORDER BY clause. This does not cause a performance hit because the SQL Server Query Optimizer is smart enough to avoid recomputing the expression:

SELECT
LastName + ‘, ‘ + FirstName AS FullName
FROM Person.Person
ORDER BY LastName + ‘, ‘ + FirstName;

Result:

FullName
----------------------
Abbas, Syed
Abel, Catherine
Abercrombie, Kim
. . .

Using an expression in the ORDER BY clause can solve some headaches. For example, some database developers store product titles in two columns: One column includes the full title, and the duplicate column stores the title stripped of the leading “The.” In terms of performance, such denormalization might be a good idea, but using a case expression within the ORDER BY clause correctly sorts without duplicating the title.

The AdventureWorks sample database includes a list of Product Descriptions. If the Description includes a leading “This,” then the CASE expression removes it from the data and passes to the ORDER BY:

USE AdventureWorks;
SELECT Description, LEN(Description) AS TextLength
FROM Production.ProductDescription
WHERE
Description LIKE ‘Replacement%'
ORDER BY
CASE
WHEN LEFT(Description, 5) = ‘This ‘
THEN Stuff(Description, 1, 5, ‘')
ELSE
Description
END;

Result:

Description                        TextLength
--------------------------------- ----------
Replacement mountain wheel for entry-level rider. 49
Replacement mountain wheel for entry-level rider. 49
Replacement mountain wheel for the casual to serious rider. 59
Replacement mountain wheel for the casual to serious rider. 59
Replacement rear mountain wheel for entry-level rider. 54
Replacement rear mountain wheel for the casual to serious rider. 64
Replacement rear wheel for entry-level cyclist. 47
Replacement road front wheel for entry-level cyclist. 53
Replacement road rear wheel for entry-level cyclist. 52
. . .

 
Others
 
- Microsoft Exchange Server 2013 : Addressing Exchange - The display or Details Templates Editor
- Microsoft Exchange Server 2013 : Addressing Exchange - MailTips and group metrics (part 2) - User experience, Custom MailTips
- Microsoft Exchange Server 2013 : Addressing Exchange - MailTips and group metrics (part 1) - Client interaction, Configuring MailTips
- Sharepoint 2013 : Wikis
- Sharepoint 2013 : Blogs
- Sharepoint 2013 : Ratings
- Sharepoint 2013 : My Sites (part 5) - My Content
- Sharepoint 2013 : My Sites (part 4) - My Newsfeed
- Sharepoint 2013 : My Sites (part 3) - My User Profile Page
- Sharepoint 2013 : My Sites (part 2) - Capacity Planning for My Sites
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us