Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (2.5k points)

I thought these were synonymous, but I wrote the following in Microsoft SQL:

Select Unique col from 
     (select col from table1 union select col from table2) alias

And it failed. Changing it to

Select Distinct col from 
     (select col from table1 union select col from table2) alias

fix it. Can someone explain?

1 Answer

0 votes
by (7.2k points)

The main difference between Unique and Distinct in SQL is that Unique helps to secure that all the values in a column are different while Distinct helps to remove all the duplicate records when reclaiming the records from a table.

Unique

An example of creating a table in MySQL with Unique is as follows.

CREATE TABLE student(

id int not null.

name varchar(255),

int age,

UNIQUE (ID) );

Distinct

SELECT DISTINCT age FROM students;

Moreover, the below statement will count different ages of the student.

SELECT COUNT (DISTINCT age) FROM Student;

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 22, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...