Introduction to SQL DISTINCT

The DISTINCT keyword in SQL is used to return unique values from a column or set of columns. This can be useful for getting a count of unique values, or for filtering out duplicate rows from a result set.

For example, let’s say we have a table of students, and we want to get a count of the number of unique students in the table. We can use the following SQL query:

SELECT COUNT(DISTINCT student_id)
FROM students;

This query will return the number 10, which is the number of unique students in the student’s table.

Examples in SQL DISTINCT

Here are some other examples of how to use the DISTINCT keyword in SQL:

  • To get a list of all of the unique cities in a country, we could use the following query:
SELECT DISTINCT city
FROM countries;
  • To filter out duplicate rows from a list of products, we could use the following query:
SELECT DISTINCT product_name, product_price
FROM products;

Conclusion

The DISTINCT keyword is a powerful tool that can be used to get unique values from a column or set of columns in a SQL database. By understanding how to use the DISTINCT keyword, you can write more efficient and effective SQL queries.

Course Schedule

Name Date Details
SQL Training 23 Mar 2024(Sat-Sun) Weekend Batch
View Details
SQL Training 30 Mar 2024(Sat-Sun) Weekend Batch
View Details
SQL Training 06 Apr 2024(Sat-Sun) Weekend Batch
View Details