Programming & Development / April 18, 2025

How to Select Distinct Column Pairs in SQL (PostgreSQL)

sql distinct columns select unique rows sql postgres distinct select college_id course_id sql sql admission table example select without duplicates postgres select distinct pair

Article:

When working with SQL databases like PostgreSQL, it's common to encounter duplicate rows that you might want to filter out. If you're trying to extract unique combinations of values from two columns in a table—say, college_id and course_id from an admission table—you can use the DISTINCT keyword.

Here’s how to do it:

✅ SQL Query to Select Unique Column Pairs

sql

SELECT DISTINCT college_id, course_id
FROM admission;

🧠 Explanation

  • SELECT DISTINCT: This part tells the database to return only unique rows.
  • college_id, course_id: These are the specific columns you want to retrieve distinct combinations for.
  • FROM admission: Specifies the table from which you're retrieving the data.

🔍 Example Use Case

Imagine the admission table contains multiple entries for the same student enrolling in the same course multiple times. If you only want to know which combinations of colleges and courses exist without repetition, this query gives you that:

college_idcourse_id110121021102

No matter how many times a combination appears, only one row per unique pair will be shown.

🚀 Pro Tip

You can combine this query with filters or ordering:

sql

SELECT DISTINCT college_id, course_id
FROM admission
WHERE admission_year = 2025
ORDER BY college_id;

This returns distinct pairs for the year 2025, sorted by college_id.

Conclusion:

Using SELECT DISTINCT in SQL is a powerful way to remove duplicates and get unique data sets. It's especially useful when dealing with relationships across entities, like colleges and courses in an education system.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex