Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (2.6k 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)

Browse Categories

...