Task 4 – SQL SELECT DISTINCT

Actuary Forums Forums Learnings Softwares SQL Course Task 4 – SQL SELECT DISTINCT

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #22708
    Mayank Goyal
    Keymaster

      The SQL SELECT DISTINCT StatementThe SELECT DISTINCT statement is used to return only distinct (different) values.

       Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.The SELECT DISTINCT statement is used to return only distinct (different) values.

       SELECT DISTINCT Syntax

      Code:

      SELECT DISTINCT [i]column1[/i],[i] column2, …[/i]
      FROM [i]table_name[/i];


      [hr]
      Demo DatabaseBelow is a selection from the “Customers” table in the Northwind sample database:SELECT ExampleThe following SQL statement selects all (and duplicate) values from the “Country” column in the “Customers” table:

      [img]https://e3vkvq.dm2301.livefilestore.com/y4mGRBB0NjV5qbgTxHMFkI2x7Fc1NKQq7lGUHl2ub0Lkn56mDYMXnKBFvBdxDZmAsBa7o4z73hXorGiNvMELeRVMQAI_-A1gnlN1Z4Vzki_F7ltNtbzeOT2QgAGovtEbZzHfrpBDCauAhJ8ug4aeUKDDe4LrhqX8w4KM3Cg_PJ9eP_fSi1nRj_Aa1kAQx6EJOtE8eDAEWnvoGdm0SnnT7JUEQ?width=901&height=347&cropmode=none[/img]

      Example

      Code:

      SELECT Country FROM Customers;


      Try it Yourself »Now, let us use the DISTINCT keyword with the above SELECT statement and see the result.


      [hr]
      SELECT DISTINCT ExamplesThe following SQL statement selects only the DISTINCT values from the “Country” column in the “Customers” table:Example

      Code:

      SELECT DISTINCT Country FROM Customers;


      Try it Yourself »The following SQL statement lists the number of different (distinct) customer countries:

      Code:

      SELECT COUNT(DISTINCT Country) FROM Customers;


      ExampleTry it Yourself »

       Note: The example above will not work in Firefox and Microsoft Edge! Because COUNT(DISTINCT column_name) is not supported in Microsoft Access databases. Firefox and Microsoft Edge are using Microsoft Access in our examples.Here is the workaround for MS Access:Example

      Code:

      SELECT Count(*) AS DistinctCountries
      FROM (SELECT DISTINCT Country FROM Customers);


      Try it Yourself »

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.

    Actuary Forums Forums Learnings Softwares SQL Course Task 4 – SQL SELECT DISTINCT