|
SQLite is a lightweight and server-less relational database management system (R.D.B.M.S). It is a self-contained database and requires very minimal configuration. It is a server-less architecture that is good for mobile applications and simple desktop applications. In this article, we are going to explore how we can implement a SELECT DISTINCT statement to perform some real-world-like operations. We are going to explore different examples with examples. SELECT DISTINCT on Multiple Columns in SQLiteIn SQLite, the SELECT DISTINCT statement allows us to fetch unique values from one or more columns in a table. We can filter out duplicate rows in a table. In simple words, this statement returns only unique values from the specified column(s). Syntax: SELECT DISTINCT column_name01, column_name02,........ Example of SELECT DISTINCT on Multiple ColumnsBefore moving on to the examples of “SELECT DISTINCT on multiple columns“, we need a table in our database to perform relevant operations on it. To create a table in our database, we need to run the query below. Query:CREATE TABLE horje Now, let’s add data to our table and display it. Query:--Data Insertion Output: Table – horje Now , we have done with creating the table. Lets move on to the implementation part of SELECT DISCTINCT statement. Example 1: Fetching Unique Data From Potd & Courses ColumnsIn this example, we are going to fetch unique data from two columns i.e. potd column and courses column. We are going to use SELECT DISTINCT statement to achieve this task. Lets see the below query. Query:SELECT DISTINCT potd, courses Output:SELECT DISTINCT on potd and courses Explanation: We can clearly see that no two rows have the same value. Therefore we can clearly say that we have successfully achieved our task. However , we can clearly observe that “100” is repeated more than one time in potd column. But we can see that both the “100” ‘s have different values in its corresponding courses column. Therefore, it is considered as a valid distinct element. Example 2: SELECT DISTINCT Along With ORDER BY ClauseCASE 1: Sorting the Data in Ascending OrderIn this case, we will sort our data returned by SELECT DISTINCT statement in ascending order. We will sort our data with respect to potd column. Lets see the below query. Query: SELECT DISTINCT potd, courses Output: SELECT DISTINCT – ORDER BY ASCENDING Explanation: We can clearly see that our displayed output is sorted in ascending order in terms of potd column. CASE 2: Sorting the Data in Descending OrderIn this case, we will be doing the same task as we have done in case 1. The only thing we are going to change is that we will sort our data in descending order in place of ascending order. Query: SELECT DISTINCT potd, courses Output: SELECT DISTINCT – ORDER BY DESCENDING Explanation: In the above image, we can clearly see that our data has been sorted in descending order with respect to potd column. Example 3: SELECT DISTINCT With GROUP BY Clause and Count() Function.In this example, we are going to return count of distinct columns : potd and courses. We are also going to group them up with respect to there corresponding data on overall_score column. Lets see the query. Query: SELECT overall_score,count(DISTINCT CONCAT(potd, courses)) as unique_records Output: SELECT DISTINCT – GROUP BY Explanation: We can clearly see that output is displayed with overall_score and unique_records.
ConclusionOverall, the SELECT DISTINCT statement in SQLite is used for managing data retrieval by filter out duplicate rows and presenting only unique values from one or more columns. we have understood various examples its in different scenarios such as selecting distinct values from multiple columns, sorting data using the ORDER BY clause and grouping results with the GROUP BY clause. Now you have good understanding of How to SELECT DISTINCT on multiple columns in SQLite. You can easily perform queries and get the desired output. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |