Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Databases Programming Interviews:BtrieveClipperData ModelingData StructuresDatabase AdministrationDatabase Administrator (DBA)Database AnalystDatabase DeveloperDB DevelopmentEDI/Data Integration ExpertFirebirdHierarchicalIBM DB2InformixJava DatabaseMariaDBMicrosoft Access DeveloperMongoDBMS SQL ServerMySQL ProgrammingNetworkNoSQLObject RelationalPostgrePostgreSQLProgressRDBMSRelationalSQLSQL AdministratorSQL and PL/SQLSQL Notification ServicesSQL server 2008SSRSStored ProcedureSybaseTeradata
Copyright © 2018. All Rights Reserved
MS SQL Server Interview Question:
How To Filter Out Duplications in the Returning Rows in MS SQL Server?
Submitted by: AdministratorIf 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 ggl_team (first_name VARCHAR(8),
last_name VARCHAR(8))
GO
INSERT INTO ggl_team VALUES ('John', 'Gate')
GO
INSERT INTO ggl_team VALUES ('John', 'Russell')
GO
INSERT INTO ggl_team VALUES ('John', 'Seo')
GO
INSERT INTO ggl_team VALUES ('John', 'Gate')
GO
INSERT INTO ggl_team VALUES ('James', 'Gate')
GO
INSERT INTO ggl_team VALUES ('Peter', 'Gate')
GO
INSERT INTO ggl_team VALUES ('John', 'Gate')
GO
Submitted by: Administrator
CREATE TABLE ggl_team (first_name VARCHAR(8),
last_name VARCHAR(8))
GO
INSERT INTO ggl_team VALUES ('John', 'Gate')
GO
INSERT INTO ggl_team VALUES ('John', 'Russell')
GO
INSERT INTO ggl_team VALUES ('John', 'Seo')
GO
INSERT INTO ggl_team VALUES ('John', 'Gate')
GO
INSERT INTO ggl_team VALUES ('James', 'Gate')
GO
INSERT INTO ggl_team VALUES ('Peter', 'Gate')
GO
INSERT INTO ggl_team VALUES ('John', 'Gate')
GO
Submitted by: Administrator
</pre>SELECT * FROM ggl_team
GO
first_name last_name
John Gate
John Russell
John Seo
John Gate
James Gate
Peter Gate
John Gate
SELECT DISTINCT * FROM ggl_team
GO
first_name last_name
James Gate
John Gate
John Russell
John Seo
Peter Gate
SELECT DISTINCT last_name FROM ggl_team
Gate
Russell
Seo</pre>
Remember that * in select list represents all columns.
Submitted by: Administrator
GO
first_name last_name
John Gate
John Russell
John Seo
John Gate
James Gate
Peter Gate
John Gate
SELECT DISTINCT * FROM ggl_team
GO
first_name last_name
James Gate
John Gate
John Russell
John Seo
Peter Gate
SELECT DISTINCT last_name FROM ggl_team
Gate
Russell
Seo</pre>
Remember that * in select list represents all columns.
Submitted by: Administrator
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.