Sql select distinct query

Sql select distinct query

Blog Bustle 13/03/2020

Sql select distinct query is used to fetch unique records from table. Sometimes we do not need to fetch duplicate records from table then we use distinct keyword with select query.

Sql select distinct query syntax

SELECT distinct column_1, column_2, column_3, ....
FROM table_name;

SELECT distinct column_1, column_2, column_3, ....
FROM table_name where condition;

In this sql select distinct query syntax, condition used with where clause is optional.

Sql select distinct query example

For example, i have student table.

sql select distinct query

In this table, there are many students who belong to different states and i want to fetch all different states from student table then sql select distinct query will be

SELECT distinct state FROM student;

if i want to fetch all different states and cities from student table then sql select distinct query will be

SELECT distinct state,city FROM student;

if i want to find total number of different states from student table then sql select distinct query will be

SELECT count(distinct state) as dcity FROM student;

if i want to fetch all different states from student table where city is lucknow then sql select distinct query will be

SELECT distinct state FROM student where city = 'lucknow';

sql select distinct query

Remember that first select database then run any sql query related to that database otherwise you will get 'no database selected' error.

So we can easily use sql select distinct query to fetch unique records from table. Hope you like this tutorial.

Other important links

Insert query in mysql

Update query in mysql

Delete query in mysql

Select query in mysql

Difference between sql and mysql

Sql select distinct query searches:

Tags: #mysql