CHAPTER 6

image

Advanced Select Techniques

by Wayne Sheffield

It’s easy to return data from a table. What’s not so easy is getting the data you need how you need it, utilizing fast, set-based methods. This chapter will show you some of the advanced techniques that can be used when selecting data.

6-1. Avoiding Duplicate Results

Problem

You need to see all of the dates on which any employee was hired. However, you have hired multiple employees on the same dates, and you want to see each relevant date only once.

Solution #1

Utilize the DISTINCT clause of the SELECT statement to remove duplicate values:

SELECT  DISTINCT TOP (10) HireDateFROM    HumanResources.Employee ...

Get SQL Server T-SQL Recipes, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.