Use DISTINCT to get non-dupliate records : Distinct « Select Clause « SQL / MySQL






Use DISTINCT to get non-dupliate records

   
/*
mysql> Drop table Bird;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE Bird (
    ->     name VARCHAR(20),
    ->     owner VARCHAR(20),
    ->     species VARCHAR(20),
    ->     sex CHAR(1),
    ->     birth DATE,
    ->     death DATE
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> INSERT INTO  Bird VALUES ('BlueBird','Joe','Car','f','1999-03-30',NULL);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-03-30',NULL);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-03-30',NULL);
Query OK, 1 row affected (0.00 sec)

mysql> select * from Bird;
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| BlueBird | Joe   | Car     | f    | 1999-03-30 | NULL  |
| RedBird  | Yin   | Bus     | m    | 1979-03-30 | NULL  |
| RedBird  | Yin   | Bus     | m    | 1979-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+
3 rows in set (0.00 sec)

mysql> SELECT DISTINCT owner FROM Bird;
+-------+
| owner |
+-------+
| Joe   |
| Yin   |
+-------+
2 rows in set (0.00 sec)


*/

Drop table Bird;

CREATE TABLE Bird (
    name VARCHAR(20), 
    owner VARCHAR(20),
    species VARCHAR(20), 
    sex CHAR(1), 
    birth DATE, 
    death DATE
);
  
INSERT INTO  Bird VALUES ('BlueBird','Joe','Car','f','1999-03-30',NULL);
INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-03-30',NULL);
INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-03-30',NULL);
  
select * from Bird;
  
SELECT DISTINCT owner FROM Bird;



           
         
    
    
  








Related examples in the same category

1.Use DISTICNT to get unique value
2.Eliminating Duplicate Data Using DISTINCT 1
3.Eliminating Duplicate Data Using DISTINCT 2
4.Select distinct records using JOIN
5.The SQL key word DISTINCT has the effect that equivalent data records are output only once.
6.DISTINCT works with multiple-column output too.
7.how many different drivers there are, use COUNT(DISTINCT)
8.DISTINCT with two columns
9.DISTINCT works with expressions, not just column values.
10.Inserting name values into the multisequence table generates separate sequences for each distinct name:
11.Count distinct
12.Distinct sub string
13.Average distinct value
14.Return a list of surnames, with each surname appearing only once?
15.The fraction of the records that contain unique or non-unique names: