Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

SQL | Distinct Clause

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The distinct keyword is used in conjunction with the select keyword. It is helpful when there is a need to avoid duplicate values present in any specific columns/table. When we use distinct keywords only the unique values are fetched. 

Syntax : 

SELECT DISTINCT column1, column2 

FROM table_name

  1. column1, column2: Names of the fields of the table.
  2. Table_name: Table from where we want to fetch the records.

This query will return all the unique combinations of rows in the table with fields column1, and column2. 

NOTE: If a distinct keyword is used with multiple columns, the distinct combination is displayed in the result set. 

Distinct Operations

Query:

CREATE TABLE students (
  ROLL_NO INT,
  NAME VARCHAR(50),
  ADDRESS VARCHAR(100),
  PHONE VARCHAR(20),
  AGE INT
);

Inserting some random data to perform distinct operations.

INSERT INTO students (ROLL_NO, NAME, ADDRESS, PHONE, AGE)
VALUES 
  (1, 'Shubham Kumar', '123 Main Street, Bangalore', '9876543210', 23),
  (2, 'Shreya Gupta', '456 Park Road, Mumbai', '9876543211', 23),
  (3, 'Naveen Singh', '789 Market Lane, Delhi', '9876543212', 26),
  (4, 'Aman Chopra', '246 Forest Avenue, Kolkata', '9876543213', 22),
  (5, 'Aditya Patel', '7898 Ocean Drive, Chennai', '9876543214', 27),
  (6, 'Avdeep Desai', '34 River View, Hyderabad', '9876543215', 24);

Output:

 

Now, to fetch unique names from the NAME field.

Query:

SELECT DISTINCT NAME FROM Student;

Output : 

 

Now, to fetch a unique combination of rows from the whole table.

Syntax:

SELECT DISTINCT *  FROM Table_name;

Query:

SELECT DISTINCT * FROM students;

Output : 

IMG4

 

Using Distinct Clause with Order By

Here, we will check the order by clause with a Distinct clause which will filter out the data on the basis of the order by clause.

Query:

SELECT DISTINCT ROLL_NO FROM Students ORDER BY AGE;

Output:

IMG5

 

How the DISTINCT Clause Handles NULL Values?

Finally, does the DISTINCT clause considers a NULL to be a unique value in SQL? The answer is yes. 

CREATE TABLE:

CREATE TABLE students (
  ROLL_NO INT,
  NAME VARCHAR(50),
  ADDRESS VARCHAR(100),
  PHONE VARCHAR(20),
  AGE INT
);
INSERT INTO students (ROLL_NO, NAME, ADDRESS, PHONE, AGE)
VALUES 
  (1, 'Shubham Kumar', '123 Main Street, Bangalore', '9876543210', 23),
  (2, 'Shreya Gupta', '456 Park Road, Mumbai', '9876543211', 23),
  (3, 'Naveen Singh', '789 Market Lane, Delhi', '9876543212', 26),
  (4, 'Aman Chopra', '246 Forest Avenue, Kolkata', '9876543213', 22),
  (5, 'Aditya Patel', '7898 Ocean Drive, Chennai', '9876543214', 27),
  (6, 'Avdeep Desai', '34 River View, Hyderabad', '9876543215', NULL);

Output:

IMG4

 

Query:

SELECT DISTINCT AGE 
FROM students;
IMG6

 

Note: Without the keyword distinct in both the above examples 6 records would have been fetched instead of 4, since in the original table there are 6 records with the duplicate values.

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Last Updated : 10 May, 2023
Like Article
Save Article
Similar Reads