|
Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - Filtering Out Duplications in the Returning Rows
By: FYIcenter.com
(Continued from previous topic...)
How To Filter Out Duplications in the Returning Rows?
If there are duplications in the returning rows, and you want to remove the
duplications, you can use the keyword DISTINCT in the SELECT clause.
The DISTINCT applies to the combination of all data fields specified in the SELECT clause.
The tutorial exercise below shows you how DISTINCT works:
CREATE TABLE fyi_team (first_name VARCHAR(8),
last_name VARCHAR(8))
GO
INSERT INTO fyi_team VALUES ('John', 'Gate')
GO
INSERT INTO fyi_team VALUES ('John', 'Russell')
GO
INSERT INTO fyi_team VALUES ('John', 'Seo')
GO
INSERT INTO fyi_team VALUES ('John', 'Gate')
GO
INSERT INTO fyi_team VALUES ('James', 'Gate')
GO
INSERT INTO fyi_team VALUES ('Peter', 'Gate')
GO
INSERT INTO fyi_team VALUES ('John', 'Gate')
GO
SELECT * FROM fyi_team
GO
first_name last_name
John Gate
John Russell
John Seo
John Gate
James Gate
Peter Gate
John Gate
SELECT DISTINCT * FROM fyi_team
GO
first_name last_name
James Gate
John Gate
John Russell
John Seo
Peter Gate
SELECT DISTINCT last_name FROM fyi_team
Gate
Russell
Seo
Remember that * in select list represents all columns.
(Continued on next topic...)
- What Is a SELECT Query Statement?
- How To Create a Testing Table with Test Data?
- How To Select All Columns of All Rows from a Table with a SELECT statement?
- How To Select Some Specific Columns from a Table in a Query?
- How To Select Some Specific Rows from a Table?
- How To Add More Data to the Testing Table?
- How To Sort the Query Output with ORDER BY Clauses?
- Can the Query Output Be Sorted by Multiple Columns?
- How To Sort Query Output in Descending Order?
- How To Count Rows with the COUNT(*) Function?
- Can SELECT Statements Be Used on Views?
- How To Filter Out Duplications in the Returning Rows?
- What Are Group Functions in Query Statements?
- How To Use Group Functions in the SELECT Clause?
- Can Group Functions Be Mixed with Non-group Selection Fields?
- How To Divide Query Output into Multiple Groups with the GROUP BY Clause?
- How To Apply Filtering Criteria at Group Level with The HAVING Clause?
- How To Count Duplicated Values in a Column?
- Can Multiple Columns Be Used in GROUP BY?
- Can Group Functions Be Used in the ORDER BY Clause?
|