Skip to main content

SQL > SQL Commands > Distinct

SQL > SQL Commands > Distinct
In SQL, the DISTINCT keyword is used in the SELECT statement to retrieve unique values from a database table. Any value that has a duplicate will only show up once.

SQL में, डेटाबेस तालिका से अद्वितीय मानों को पुनः प्राप्त करने के लिए चयन कथन में DISTINCT कीवर्ड का उपयोग किया जाता है। कोई भी मूल्य जिसमें एक डुप्लिकेट है वह केवल एक बार दिखाएगा।

Syntax

SELECT DISTINCT "column_name"
FROM "table_name";
"table_name" is the name of the table where data is stored, and "column_name" is the name of the column containing the data to be retrieved.

"table_name" उस तालिका का नाम है जहां डेटा संग्रहीत किया जाता है, और "column_name" उस स्तंभ का नाम है जिसमें डेटा पुनर्प्राप्त किया जाना है।

Examples

The examples will use the following table:
Table Store_Information
Store_NameSalesTxn_Date
Los Angeles1500Jan-05-1999
San Diego250Jan-07-1999
Los Angeles300Jan-08-1999
Boston700Jan-08-1999

Example 1: Use DISTINCT on one column

To select all distinct stores in Table Store_Information, we key in,
SELECT DISTINCT Store_Name FROM Store_Information;
Result:
Store_Name
Los Angeles
San Diego
Boston

Example 2: Use DISTINCT on multiple columns

We can apply DISTINCT to multiple columns. If we want to get a list showing all unique combinations of stores and transaction dates, we would type in the following,

हम कई कॉलम में DISTINCT लगा सकते हैं। यदि हम स्टोर और लेन-देन की तारीखों के सभी विशिष्ट संयोजनों को दर्शाने वाली एक सूची प्राप्त करना चाहते हैं, तो हम निम्नलिखित में टाइप करेंगे,
SELECT DISTINCT Store_Name, Txn_Date FROM Store_Information;
Result:
Store_NameTxn_Date
Los AngelesJan-05-1999
San DiegoJan-07-1999
Los AngelesJan-08-1999
BostonJan-08-1999

Exercises

For these exercises, assume we have a table called Users with the following data:
Table Users
First_NameLast_NameBirth_DateGenderJoin_Date
SophieLeeJan-05-1960FApr-05-2015
RichardBrownJan-07-1975MApr-05-2015
JamalSantoOct-08-1983MApr-09-2015
CaseyHealySep-20-1969MApr-09-2015
JillWilkesNov-20-1979FApr-15-2015
1. Which of the following SQL statement is valid?
a) SELECT DISTINCT * FROM Users;
b) SELECT DISTINCT First_Name FROM Users;
c) SELECT DISTINCT First_Name Last_Name FROM Users;
2. What's the result of the following query?
SELECT DISTINCT Join_Date From Users;
3. What's the result of the following query?
SELECT DISTINCT Gender, Join_Date From Users;
1. b)
2. The result is:
Join_Date
Apr-05-2015
Apr-09-2015
Apr-15-2015
3. The result is:
GenderJoin_Date
FApr-05-2015
MApr-05-2015
MApr-09-2015
FApr-15-2015

Comments

Popular posts from this blog

What is Excel ? एक्सेल क्या है ?

Excel is a spreadsheet application which have rows and columns to organize data. We can easily organize and analysis data in excel. एक्सेल एक स्प्रेडशीट एप्लिकेशन है जिसमें डेटा व्यवस्थित करने के लिए पंक्तियां और कॉलम हैं। हम एक्सेल में आसानी से डेटा व्यवस्थित और विश्लेषण कर सकते हैं।

VBA Code to Clear Cells with Zero शून्य के साथ सेल को साफ़ करने के लिए VBA कोड

# VBA Code to Clear Cells with Zero:- To  clear cells with zero within a cell range using VBA , use a macro with the following statement structure: शून्य के साथ सेल को साफ़ करने के लिए VBA कोड For   Each   Cell  In   Range If   Cell.Value = myValue  Then   Cell.Clear Next   Cell VBA Statement Explanation Lines #1 and #3: For Each Cell In Range | Next Cell Item:  For Each… In… Next. VBA Construct:  For Each… Next statement. Description:  The For Each… Next statement repeats the statement within the loop (line #2) for each element (Cell) in the cell range (Range) you want to search for zeroes in. Item:  Cell. VBA Construct:  Element of the For Each… Next statement and object variable of the Range object data type. Description:  The Element of the For Each… Next statement is an object variable used to iterate through the elements (Cell) of the cell range (Range) you want to sear...

VBA Coding : For Sharing data by Email With Attachment using Excel data in other Place

' We have some data in Excel that we want to share by email with attachment हमारे पास Excel में कुछ डेटा हैं जिन्हें हम अनुलग्नक के साथ ईमेल द्वारा साझा करना चाहते हैं Sub For_GSTR3B_Report_Share() Dim sWorkbook1 As Workbook Dim wb As Workbook Dim objApp As Object  'For New Workbook Dim ws As Worksheet Dim savepath As String         'Delete all file from folder temp2         On Error Resume Next         Kill "C:\Users\PRAMOD.GARG\Desktop\pk_temp*.*"         On Error GoTo 0               On Error Resume Next         Set wb = Workbooks("Pkgarg_Control_Sheet_With_Gaurav_Coding.xlsm")         On Error GoTo 0         If Not wb Is Nothing Then         MsgBox "It's open"         wb.Activate         Worksheets("For_GSTR3B_Report_Share...