SQL distinct

Created By: chatGPT

The SQL DISTINCT keyword is used to return only unique (different) values from a column in a database. It eliminates duplicate rows from the result set. Understanding how to use DISTINCT can help in retrieving meaningful data from your tables. You can apply the DISTINCT clause on single or multiple columns.
SELECT DISTINCT column_name1,
       column_name2
FROM table_name;
Example: Consider a table named Employees with the following data:
EmployeeID | FirstName | LastName | Country
----------- | ---------- | --------- | --------
1           | John      | Doe      | USA
2           | Jane      | Smith    | UK
3           | John      | Doe      | USA
4           | Emily     | Jones    | USA
If you want to retrieve unique first names from the Employees table, you can use the following query:
SELECT DISTINCT FirstName
FROM Employees;
This query will return:
FirstName
----------
John
Jane
Emily
You can also use DISTINCT with multiple columns. For example, if you want to get unique combinations of FirstName and LastName, you can do this:
SELECT DISTINCT FirstName, LastName
FROM Employees;
This will return:
FirstName | LastName
---------- | ---------
John      | Doe
Jane      | Smith
Emily     | Jones
Keep in mind that when using DISTINCT, the specified columns must be in the SELECT clause. Additionally, performance may vary based on database size, as removing duplicates can be a resource-intensive task.
Introduction And SetupSelectFromWhereJoinInner JoinLeft JoinRight JoinFull JoinOnGroup ByHavingOrder ByLimitInsert IntoValuesUpdateSetDeleteCreate TableAlter TableDrop TableCreate IndexDrop IndexUnionUnion AllIntersectExceptCaseWhenThenElseEndInBetweenLikeIs NullIs Not NullExistsNot ExistsAndOrNotAsWithDistinctCountSumAvgMinMaxCastCoalesceNullifSubstringConcatLowerUpperTrimReplaceRoundFloorCeilAbsModCurrent_dateCurrent_timeCurrent_timestamp