Home / mySQL / DISTINCT Keyword- SQL

DISTINCT Keyword- SQL

Hi all,
Welcome to SQL world. The SQL is very useful in programming. The sql queries helps in developers. There are different types of keyword, joins, sub-queries and commands are available.
The DISTINCT keyword is used for remove the duplicate values from database table in SELECT query. The SELECT DISTINCT statement is used to return only different values.
In database table, the column may contain duplicate values; and if you want to display only unique values or different values. For this process we use the distinct values in SQL SELECT query.



SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name,column_name FROM table_name;

OR

SELECT DISTINCT expressions FROM tables WHERE conditions;

expressions: The columns or calculations that you wish to retrieve.




tables:The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause and we can use more than one table and field.

conditions:The conditions that must be met for the records to be selected.

Example:Employee table


EmployeeID employee_name
1 Thomas
2 Varghese
3 Jose
4 Varghese




SELECT DISTINCT employee_name FROM Employee;

OUTPUT





employee_name
Thomas
Varghese
Jose

EXAMPLE – WITH SINGLE FIELD

We can use the SQL DISTINCT in SELECT query to return a single field that removes the duplicates from the result set.

Eg:

SELECT DISTINCT city FROM City_table;

This SQL DISTINCT example would return all unique city values from the City_table table.




EXAMPLE – WITH MULTIPLE FIELDS

SQL DISTINCT in SELECT query to remove duplicates from more than one field in your SQL SELECT statement.

Eg:

SELECT DISTINCT city, state FROM City_table;




Return each unique city and state combination. In this case, the DISTINCT applies to each field listed after the DISTINCT keyword.

Loading Facebook Comments ...
Social Media Auto Publish Powered By : XYZScripts.com