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.
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.
For example, i have student table.
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';
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.
Difference between sql and mysql
Sql Insert Query
Sql Update Query
Sql & Mysql difference
Sql Delete Query
Sql Select Query
Mysql no database selected error 1046
Sql Create Table
Sql Drop Table
Sql Alter Query(add, drop, modify & change column)
Sql Truncate Table
Sql Insert Into Query
Sql Rename Table
Sql DISTINCT Keyword
Sql WHERE Clause
Sql Like operator
Sql AND operator
Sql OR operator
Sql NOT operator
Combining Sql AND, OR & NOT operator
Sql IN operator
Sql NOT IN operator
Sql Count, Sum, Avg, Min, Max
Sql Order By clause
Sql Limit clause
Sql BETWEEN operator
Sql Group By clause
Sql Aliases
Sql Inner Join
Sql Left Join
Sql Right Join
Mysql Create Database
Mysql Create Database Command Line
Mysql Delete Database
Mysql Delete Database Command Line
Mysql Show Databases
Mysql Show Databases Command Line
Mysql Select Database
Mysql Select Database Command Line
Mysql Backup Database
Mysql Import Database
Mysql Copy Database
Mysql Rename Database
Truncate Database Mysql