In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table. The DISTINCT statement can be used to return only distinct (different/that don'trepeat) values.
Syntax :
Select distinct(column name) from table_name ;
Script:
--first lets drop the table Test (if she exists, if not skip the drop line).
Drop table test;
create table test (id int,name varchar(20),email varchar(20),salary int ,de char(2));
--insert values into table
insert into test values (1,'Eve','[email protected]',1500,'HR');
insert into test values (2,'Jon','[email protected]',2500,'AD');
insert into test values (3,'Mike','[email protected]',3000,'AD');
insert into test values (4,'Paul','[email protected]',3200,'HR');
insert into test values (5,'Mary','[email protected]',1800,'IT');
insert into test values (6,'Jane','[email protected]',2200,'IT');
SQLselect * from test ;
ID NAME EMAIL SALARY DE
---------- -------------------- -------------------- ---------- -- --------
1 Eve [email protected] 1500 HR
2 Jon [email protected] 2500 AD
3 Mike [email protected] 3000 AD
4 Paul [email protected] 3200 HR
5 Mary [email protected] 1800 IT
6 Jane [email protected] 2200 IT
Distinct() examples:
Example 1:
-let's see how many distinct ID's values we have:SQLselect distinct(id) from test;
ID
----------
1
6
2
4
5
3
Example 2:
-now let's see how many distinct DE (departments) we have in our test table;SQLselect distinct(de) from test;
DE
--
AD
IT
HR