Symmetric Pairs

Sort by

recency

|

1453 Discussions

|

  • + 0 comments
    WITH name_salary AS (
        SELECT s.id AS n_id, s.name AS name, p.salary AS nsalary
        FROM Students s
        JOIN Packages p
        USING (id)
    ),
    friend_salary AS (
        SELECT f.id AS id, f.friend_id AS f_id, p.salary AS fsalary
        FROM Friends f
        JOIN Packages p
        ON f.friend_id = p.id
    )
    SELECT ns.name
    FROM name_salary ns
    JOIN friend_salary fs
    ON ns.n_id = fs.id
    WHERE fs.fsalary > ns.nsalary
    ORDER BY fs.fsalary;
    
  • + 0 comments

    SELECT A.X,A.Y FROM Functions A JOIN Functions B ON (A.X = B.Y AND A.Y = B.X) OR (A.X = B.X AND A.Y = B.Y) WHERE A.X <= A.Y GROUP BY A.X,A.Y HAVING COUNT(*) > 1 ORDER BY A.X,A.Y;

  • + 0 comments

    Update: Forgot to order the output. It works once I added "order by a.x"...

    WITH CTE AS ( select *, row_number() over(order by X asc) as rowN from Functions)

    select distinct a.X, a.Y from CTE a inner join CTE b on a.X = b.Y and a.Y = b.X where a.rowN != b.rowN and a.X <= a.Y;

  • + 0 comments

    MySQL

    SELECT DISTINCT f.x, f.y
    FROM (SELECT x, y, 
          ROW_NUMBER() OVER (ORDER BY x ASC) AS id
          FROM Functions) AS f
    INNER JOIN (SELECT x, y, 
                ROW_NUMBER() OVER (ORDER BY x ASC) AS id
                FROM Functions) AS f2 ON f.x = f2.y
    WHERE f.x <= f.y AND f2.x = f.y AND f.id != f2.id
    ORDER BY f.x
    
  • + 0 comments

    WITH SMALLER AS ( SELECT DISTINCT X,Y FROM FUNCTIONS WHERE XY ), SAME AS ( SELECT X,Y, COUNT(*) AS COUNTSAME FROM FUNCTIONS WHERE X=Y GROUP BY X, Y ) SELECT SMALLER.X,SMALLER.Y FROM SMALLER JOIN BIGGER ON (SMALLER.X=BIGGER.Y AND SMALLER.Y=BIGGER.X) UNION ALL SELECT DISTINCT X,Y FROM SAME WHERE MOD(COUNTSAME,2)=0 ORDER BY X