Where do we use SELECT DISTINCT COMMAND?
The "SELECT DISTINCT" command is used in SQL (Structured Query Language) to retrieve unique values from a specific column or columns in a database table. It is commonly used in queries when you want to eliminate duplicate values and retrieve only distinct or unique values.
Here are a few scenarios where you might use the "SELECT DISTINCT" command:
- Eliminating duplicate entries: If a table contains multiple rows with the same values in a particular column, using "SELECT DISTINCT" allows you to retrieve only the unique values from that column.
- Filtering unique values: When you have a large dataset and want to filter out duplicate values in a specific column, "SELECT DISTINCT" can help you identify the unique values.
- Aggregating unique values: In combination with aggregate functions like COUNT, SUM, AVG, etc., "SELECT DISTINCT" can be used to calculate aggregate values based on unique values in a column.
- Joining tables: When joining multiple tables, "SELECT DISTINCT" can be useful to retrieve unique combinations of columns across the joined tables. This helps prevent duplicate rows in the result set.
- Where do we use SELECT DISTINCT COMMAND?
- DISTINCT keyword on single column
- DISTINCT keyword on multiple columns
- DISTINCT keyword with COUNT() function
- Conclusion
DISTINCT keyword on single column
Here's an example of how to use the "DISTINCT" keyword in a SQL query:
Syntax
| SELECT DISTINCT column_name
FROM table_name; |
|---|
In the above query, you need to replace "column_name" with the actual name of the column from which you want to retrieve distinct values, and "table_name" with the name of the table you are querying.
Let's consider a table called "students" with the following structure:
Example
| CREATE TABLE students (
id INT PRIMARY KEY, name VARCHAR(50), course VARCHAR(50), age INT ); |
|---|
Now, let's assume the "students" table has the following data
| ID | Name | Course | Age |
|---|---|---|---|
| 1 | Priya | Biology | 19 |
| 2 | Rahul | Physics | 21 |
| 3 | Ananya | Chemistry | 20 |
| 4 | Siddharth | Biology | 19 |
| 5 | Nisha | Mathematics | 22 |
| 6 | Arjun | Physics | 20 |
| 7 | Aishwarya | Chemistry | 19 |
To retrieve the distinct courses from the "students" table, you can use the following query:
Example
| SELECT DISTINCT course
FROM students; |
|---|
The result of this query would be:
| Subject |
|---|
| Biology |
| Physics |
| Chemistry |
| Mathematics |
DISTINCT keyword on multiple columns
Example
| SELECT DISTINCT name, course
FROM students; |
|---|
| Name | Course |
|---|---|
| Priya | Biology |
| Rahul | Physics |
| Ananya | Chemistry |
| Siddharth | Biology |
| Nisha | Mathematics |
| Arjun | Physics |
| Aishwarya | Chemistry |
DISTINCT keyword with COUNT() function
Syntax
| SELECT COUNT(DISTINCT column_name) FROM table_name WHERE condition; |
|---|
Example
| SELECT COUNT(DISTINCT department) AS distinct_department_count
FROM employees; |
|---|
| distinct_department_count |
|---|
| 3 |
.png)
0 Comments
Post a Comment