![]() ![]() (See WITH Clause below.)Īll elements in the FROM list are computed. A WITH query that is referenced more than once in FROM is computed only once. These effectively serve as temporary tables that can be referenced in the FROM list. The general processing of SELECT is as follows:Īll queries in the WITH list are computed. ![]() SELECT retrieves rows from zero or more tables. GROUPING SETS ( grouping_element )Īnd with_query is: with_query_name ) ] AS ( select | values | insert | update | delete ) This write-up considered several examples to explain the working of the SELECT DISTINCT clause in PostgreSQL.Synopsis with_query ] In PostgreSQL, the DISTINCT keyword or the SELECT DISTINCT clause can be used to fetch single or multiple columns of a result set returned by the SELECT statement. The DISTINCT keyword removes all the redundant/duplicate records and returns only the unique ones. In PostgreSQL, the DISTINCT keyword/clause is used with the collaboration of the SELECT statement to fetch the unique records from a result-set. The SELECT DISTINCT clause skipped only those records that were duplicated in both bike_model and bike_color columns. ![]() This proved that the DISTINCT keyword evaluated the duplicated values based on both bike_model and bike_color columns. But the SELECT DISTINCT didn’t eliminate it because bike_color for both these records is different. From the above snippet, we can observe that the bike_model 2022 occurs twice. You have to follow the comma-separated syntax to fetch more than one column using the DISTINCT keyword: SELECT DISTINCT bike_model, bike_color The output clarified that the SELECT DISTINCT clause returned the unique and sorted(descending order) records of the bike_details table.Įxample 4: Fetch Multiple Columns Using DISTINCT Keyword Let’s execute the following query to get the sorted and unique records from the bike_model column: SELECT DISTINCT bike_model The output proved that the DISTINCT keyword fetched only the unique records and skipped duplicated ones.Įxample 3: Fetch Sorted and Unique Records Let’s run the SELECT statement with the collaboration of the DISTINCT keyword to get only unique records from the bike_model column: SELECT DISTINCT bike_model FROM bike_details The output shows that the bike_model column has some unique as well as some duplicate records.Įxample 2: Fetch Table Records With DISTINCT Keyword Let’s execute the below-given query to fetch the bike_model column: SELECT bike_model FROM bike_details The SELECT DISTINCT represents that only unique records will be fetched/returned, and the duplicate records will be neglected.Įxample 1: Fetch Table Records Without DISTINCT Keywordįirstly, we will run the SELECT statement to get all the records of the bike_details table: SELECT * FROM bike_details col_1, col_2.col_N are the n number of columns to be fetched by the SELECT statement. ![]() The below-given snippet illustrates the syntax of the DISTINCT keyword: SELECT DISTINCT col_1, col_2.col_N How to Use DISTINCT Keyword/Clause in PostgreSQL? In this write-up, we will explain the different use cases of the SELECT DISTINCT clause using some examples. In the case of multiple columns, the SELECT DISTINCT clause skips only those records that have duplicates in all the specified columns. In PostgreSQL, the SELECT DISTINCT clause can be used to fetch unique records for a single or multiple columns of a result set returned by the SELECT statement. In PostgreSQL, the DISTINCT keyword/clause is used within the SELECT statement to fetch the unique records from the result-set. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |