|
INTERVIEW QUESTIONS
DATABASE
MS SQL SERVER
DETAILS
Question: Can Group Functions Be Used in the ORDER BY Clause in MS SQL Server?
Answer: If the query output is aggregated as groups, you can sort the groups by using group functions in the ORDER BY clause. The following statement returns the maximum "counts" in each group, determined by a unique combination of tag and year. The group output is sorted by the maximum "counts" in each group in ascending order:
SELECT tag, YEAR(created), MAX(counts) FROM ggl_links GROUP BY tag, YEAR(created) ORDER BY MAX(counts) GO
tag year(created) max(counts)
DEV 2006 120
DBA 2006 390
DEV 2004 439
SQA 2007 728
SQA 2003 828
DBA 2005 960
DBA 2007 972
|
|
|
Category |
MS SQL Server Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 6969 users |
Added on |
9/23/2014 |
Views |
71482 |
Rate it! |
|
|
Question:
Can Group Functions Be Used in the ORDER BY Clause in MS SQL Server?
Answer:
If the query output is aggregated as groups, you can sort the groups by using group functions in the ORDER BY clause. The following statement returns the maximum "counts" in each group, determined by a unique combination of tag and year. The group output is sorted by the maximum "counts" in each group in ascending order:
SELECT tag, YEAR(created), MAX(counts) FROM ggl_links GROUP BY tag, YEAR(created) ORDER BY MAX(counts) GO
tag year(created) max(counts)
DEV 2006 120
DBA 2006 390
DEV 2004 439
SQA 2007 728
SQA 2003 828
DBA 2005 960
DBA 2007 972 Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Can Multiple Columns Be Used in SQL GROUP BY Clause in MS SQL Server?
|
View Answer
|
|
How To Count Duplicated Values in a Column in MS SQL Server?
|
View Answer
|
|
How To Apply Filtering Criteria at Group Level with The HAVING Clause in MS SQL Server?
|
View Answer
|
|
How To Divide Query Output into Multiple Groups with the GROUP BY Clause in MS SQL Server?
|
View Answer
|
|
Can Group Functions Be Mixed with Non-group Selection Fields in MS SQL Server?
|
View Answer
|
|
How To Use Group Functions in the SELECT Clause in MS SQL Server?
|
View Answer
|
|
What Are Group Functions in Query Statements in MS SQL Server?
|
View Answer
|
|
How To Filter Out Duplications in the Returning Rows in MS SQL Server?
|
View Answer
|
|
Can SELECT Statements Be Used on Views in MS SQL Server?
|
View Answer
|
|
How To Count Rows with the COUNT(*) Function in MS SQL Server?
|
View Answer
|
|
How To Sort Query Output in Descending Order in MS SQL Server?
|
View Answer
|
|
Can the Query Output Be Sorted by Multiple Columns in MS SQL Server?
|
View Answer
|
|
How To Sort the Query Output with ORDER BY Clauses in MS SQL Server?
|
View Answer
|
|
How To Add More Data to the Testing Table in MS SQL Server?
|
View Answer
|
|
How To Select Some Specific Rows from a Table in MS SQL Server?
|
View Answer
|
|
How To Select Some Specific Columns from a Table in a Query in MS SQL Server?
|
View Answer
|
|
How To Select All Columns of All Rows from a Table with a SELECT statement in MS SQL Server?
|
View Answer
|
|
What Is a SELECT Query Statement in MS SQL Server?
|
View Answer
|
|
How To Use ORDER BY with UNION Operators in MS SQL Server?
|
View Answer
|
|
How To Use UNION to Merge Outputs from Two Queries Together in MS SQL Server?
|
View Answer
|