Which SQL keyword is used to retrieve unique values?

196    Asked by ClaudineTippins in SQL Server , Asked on Jul 16, 2024

I am currently working as a data analyst for a particular retail company. My task is to generate a report that would show a list of all the unique product categories from the product table in my particular database. The table has the Column names such as Production ID, Product Name, Categories, Prices, and Stock quantity.  How can I write the SQL query that would be used to retrieve the unique values such as unique product categories? 

Answered by David Piper

 In the context of SQL, to retrieve the unique values from a specific Column in an SQL table, you can use the DISTINCT keyword. It is used in the SELECT statement to ensure that only the unique values should be retrieved in the set of the results. This is particularly useful when you need to eliminate duplicates and get a list of unique entries from a Column.

In this case, you would need to retrieve the unique product categories from the product table. The SQL query would use the DISTINCT keyword on the column called category.

The SQL query is as follows:-

Select unique product categories from the product table

SELECT DISTINCT Category
FROM Products;
-- Explanation:

-- SELECT DISTINCT Category: This part of the query selects unique values from the Category column.

-- FROM Products: This specifies the table from which to retrieve the data.

Here is also a java based example given which would demonstrate how you can retrieve unique product categories from a particular Database by using SQL and JDBC:-

Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.HashSet;
Import java.util.Set;
Public class UniqueProductCategories {
    // Database URL, username, and password
    Private static final String DB_URL = “jdbc:mysql://localhost:3306/your_database_name”;
    Private static final String USER = “your_database_username”;
    Private static final String PASS = “your_database_password”;
    Public static void main(String[] args) {
        // Initialize the connection object
        Connection connection = null;
        // Initialize the prepared statement object
        PreparedStatement preparedStatement = null;
        // Initialize the result set object
        ResultSet resultSet = null;
        Try {
            // Establishing the connection to the database
            Connection = DriverManager.getConnection(DB_URL, USER, PASS);
            // SQL query to retrieve unique product categories
            String sqlQuery = “SELECT DISTINCT Category FROM Products”;
            // Create a prepared statement
            preparedStatement = connection.prepareStatement(sqlQuery);
            // Execute the query and get the result set
            resultSet = preparedStatement.executeQuery();
            // Using a Set to store unique categories
            Set uniqueCategories = new HashSet<>();
            // Iterate through the result set and add categories to the set
            While (resultSet.next()) {
                String category = resultSet.getString(“Category”);
                uniqueCategories.add(category);
            }
            // Print the unique categories
            System.out.println(“Unique Product Categories:”);
            For (String category : uniqueCategories) {
                System.out.println(category);
            }
        } catch (SQLException e) {
            // Handle SQL exception
            e.printStackTrace();
        } finally {
            // Close resources to avoid memory leaks
            Try {
                If (resultSet != null) {
                    resultSet.close();
                }
                If (preparedStatement != null) {
                    preparedStatement.close();
                }
                If (connection != null) {
                    Connection.close();
                }
            } catch (SQLException e) {
                // Handle SQL exception during resource cleanup
                e.printStackTrace();
            }
        }
    }
}

In this way, you can easily retrieve unique values from the SQL database.



Your Answer

Interviews

Parent Categories