Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:15:17+00:00 2026-06-07T00:15:17+00:00

I have the following table drop table names; create table names( name varchar(200), surname

  • 0

I have the following table

drop table names;
create table names(
    name    varchar(200),
    surname varchar(200),
    primary key(name, surname)
);

insert into names values ('john', 'doe');
insert into names values ('john', 'richards');

I’d like to list the surnames where the name is the same.
i.e. I want this result

----------+----------
 doe      | richards
 doe      | doe
 richards | richards

(this is only an example, the real problem is quite different)

The followig query returns duplicates

select n1.surname, n2.surname
from names n1 join names n2
on n1.name = n2.name;

 surname  | surname  
----------+----------
 doe      | richards
 doe      | doe
 richards | richards
 richards | doe

So, I used the least and greatest postgresql functions, and got my result. But: least and greatest are not standard SQL, and I was wondering whether a more efficient solution exists, as using a join condition I cannot imagine now.

select distinct least(n1.surname, n2.surname), greatest(n1.surname,
n2.surname)
from names n1 join names n2
on n1.name = n2.name;

  least   | greatest 
----------+----------
 doe      | richards
 doe      | doe
 richards | richards
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:15:18+00:00Added an answer on June 7, 2026 at 12:15 am

    If you only want a tie-breaker, use < of > or >= or <=

    SELECT n1.surname, n2.surname
    FROM names n1 
    JOIN names n2 ON n1.name = n2.name
    WHERE n1.surname >= n2.surname
       ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following rows: CREATE TABLE #TEMP (id int, name varchar(255), startdate datetime,
I have the following table DROP TABLE IF EXISTS `test`.`foo`; CREATE TABLE `test`.`foo` (
I have the following script to create a table: -- Create State table. DROP
Given the following table and data CREATE TABLE #temps ( id int, name varchar(max)
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
I have a table with something like the following: ID Name Color ------------ 1
I have the following database -- MYSQL DROP TABLE IF EXISTS Attributes; DROP TABLE
I have created a table like this: CREATE TABLE #TEMP(RecordDate datetime, First VARCHAR(255), Last
I have following Table with records for ActivityLog: ID, UserId, Category, Created 1 11
I have following table Id | Status | date ------------------- 1 | Onsite |2007

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.