PayPal Interview Question

SQL (weight: 10%) Consider the table below: table A ( id integer primary key, name varchar(20), age integer ) Write a query to return the list of unique names from table A

Interview Answers

Anonymous

Jan 22, 2012

We cant use DISTINCT here. As the question asks for the Unique names to return, which means names that are not duplicated in the table, we have to return the names that the count of that name in the table is 1. SELECT name FROM TableA t1 WHERE ( SELECT COUNT( name ) FROM TableA t2 WHERE t1.name = t2.name ) = 1; id name age 1 Test1 2 2 Test2 0 3 Test1 4 4 Test3 5 5 Test4 5 6 Test2 5 In the above table you will get the result Test3 and Test4 as those 2 are the only Unique names in the table. all the other names are duplicated.

12

Anonymous

May 24, 2015

SELECT name FROM TableA GROUP BY name HAVING COUNT(name) = 1; This will display only those names whose duplicates don't exist in the table.

1

Anonymous

Dec 19, 2011

SELECT DISTINCT name FROM tableA

2