Get Started: SQL SELECT DISTINCT

In This Article, You Will Know About SQL SELECT DISTINCT.

SQL SELECT Distinct – Before moving ahead, let’s know a bit about SQL SELECT.

Table of Contents

The SQL SELECT DISTINCT Statement

The SELECT DISTINCT statement is used to return only distinct (different) values.

In our table (data), we might have many times repeated values (duplicate values) but we want to get only unique (different) values.

SELECT DISTINCT Syntax

				
					SELECT DISTINCT column1, column2, ...
FROM table_name;

				
			

Example Database

Below is the example of a database table named “details”.

Id

name

class

section_id

1

Alex

5

1

2

Rolex

6

2

3

Salex

7

2

4

Lelex

8

3

5

Kelex

9

5

Example: Use the ‘SELECT’ statement to return all records from column ‘section_id’.

				
					SELECT section_id FROM details;
				
			

Now, let’s use ‘SELECT DISTINCT’ to return the different records.

SELECT DISTINCT Examples

Example: Use the ‘SELECT DISTINCT’ statement to return the different records from column ‘section_id’.

				
					SELECT DISTINCT section_id FROM details;
				
			

The following SQL statement lists the number of different (distinct) section_id from the table details

SELECT COUNT

Example: Use the ‘COUNT’ statement to return the number of different records in column section_id.

				
					SELECT COUNT (DISTINCT section_id) FROM details;
				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Leave a Comment

Stay in the loop

codingstreets