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

  • Home
  • SEARCH
  • 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 9071493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:55:49+00:00 2026-06-16T17:55:49+00:00

I have a database as: Student (ID, Name, Grade) Likes (ID1, ID2) Where ID1

  • 0

I have a database as:

Student (ID, Name, Grade)
Likes (ID1, ID2)

Where ID1 and ID2 in last table are foreign key referenced student(ID)

Note: Liking isn’t a mutual relation, e.g its not necessary that if (123, 456) is in Likes table, then (456,123) is also in Likes table.

I have to write query for the following statement:
“For every pair of students, who both like each other, return the name and grade of both students. Include each pair only once, with the two names in alphabetical order.”

So far I have given the data in which ID1 and ID2 mutually like each other:

SELECT s1.ID, s1.name, s2.ID, s2.name 
FROM student s1, student s2, likes l
WHERE s1.ID = l.ID1 AND s2.ID = l.ID2 
AND l.ID1 IN (SELECT ID2 FROM likes) 
AND l.ID2 IN (SELECT ID1 FROM likes);

Someone kindly help me how to avoid duplicate pairs.

Database is: (If someone needs it)

INSERT INTO `student` VALUES (1025,'John',12),(1101,'Haley',10),(1247,'Alexis',11),(1304,'Jordan',12),(1316,'Austin',11),(1381,'Tiffany',9),(1468,'Kris',10),(1501,'Jessica',11),(1510,'Jordan',9),(1641,'Brittany',10),(1661,'Logan',12),(1689,'Gabriel',9),(1709,'Cassandra',9),(1782,'Andrew',10),(1911,'Gabriel',11),(1934,'Kyle',12);
INSERT INTO `likes` VALUES (1689,1709),(1709,1689),(1782,1709),(1911,1247),(1247,1468),(1641,1468),(1316,1304),(1501,1934),(1934,1501),(1025,1101);

and according to data entered:

DATA I GET

1689    Gabriel     1709    Cassandra
1709    Cassandra   1689    Gabriel
1501    Jessica     1934    Kyle
1934    Kyle        1501    Jessica

IDEAL DATA

1689    Gabriel     1709    Cassandra
1501    Jessica     1934    Kyle
  • 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-16T17:55:50+00:00Added an answer on June 16, 2026 at 5:55 pm

    Since the question is “how to avoid duplicate pairs.“:

    You join 2 tables to get the ones where they both like each other, you will get 2 rows for each pair.

    You can discard one by comparing against some distinct value. ID is a great candidate:

    select * -- put fields here
    from likes li 
    join likes li2 on li2.ID1 = li.ID2 and li2.ID2 = li.ID1
    -- join 2 students here
    where li.ID1 < li.ID2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that has a student entity: create table student ( name
I have a database table named 'student' in which there is one column named
So I have these tables: STUDENTS: Student ID - First name - Last name
Suppose that I have a database with student information: {'student_name' : 'Alen', 'subjects' :
I have a database Student which contains about 20 tables. one of the tables
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have two objects (class, student) in a database with a many to many
I have these two class(table) @Entity @Table(name = course) public class Course { @Id
I have a table with a primary key (lets call it person), and another

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.