Distinct pairs MySQL - mysql

I need to find distinct pairs(x,y) from a table consisting 2 columns(X, Y).
The numbers which are repeated in the column shouldn't be included in the result set.
Table:
X Y
1 2
2 3
3 4
4 4
5 2
5 6
7 9
Result Set:
X Y
2 3
7 9
5 repeated in X and 2,4 in Y, so they will not form pairs with the corresponding Y and X.
The question was asked to me in an interview. Not able to find a solution. Need a query for this. Please help!

SELECT X, Y
FROM yourTable
WHERE X IN (SELECT X FROM yourTable GROUP BY X HAVING COUNT(*) = 1) AND
Y IN (SELECT Y FROM yourTable GROUP BY Y HAVING COUNT(*) = 1)
#strawberry appears to have found a way to do this using joins, but the option which popped into my head was to simply use non-correlated subqueries to find the X and Y values which appear only once in each respective column. Then, just use a WHERE clause to check that each X and Y value falls in these sets.

E.g.:
SELECT DISTINCT a.*
FROM my_table a
LEFT
JOIN my_table b
ON b.x = a.x
AND b.y <> a.y
LEFT
JOIN my_table c
ON c.y=a.y
AND c.x <> a.x
WHERE b.x IS NULL
AND c.x IS NULL;

Related

SQL Query - Find Duplicates with a Different Key

I have the following data:
id userid name group
1 1 A x
2 1 A y
3 1 A z
4 2 B x
5 2 B y
6 3 C y
7 4 D x
8 5 E x
9 5 E z
10 6 F x
I want to find those records that meet all this condition:
Select all rows where the a userid belongs to a group other than y but the userid also belongs to group y.
The resulting dataset will be as follows:
id userid name group
1 1 A x
3 1 A z
4 2 B x
If you see, it has resulted in two records for userid a because these are two two records belong to groups other than y but the userid 1 also belongs to group y. Same for userid 2.
I have been breaking my head on how to get this in an SQL statement but not even close to a solution.
Any help is appreciated.
Use a join:
SELECT t1.*
FROM mytable t1
INNER JOIN mytable t2
ON t1.user_id = t2.user_id AND t1.group <> t2.group AND t2.group = 'y'
I think that would be the fastest query (but please feel free to try the other solutions as well).
Add an index on user_id if not already there and maybe play with some other indexes as well (maybe a composite index on group and user_id can be utilized)
Use exists
select *
from MyTable a2
where name_group <> 'y'
and exists (select 1
from MyTable a2
where a2.name_group = 'y'
and a2.userid = a1.userid)
You can get all the users that meet the condition using aggregation and having:
select userid
from t
group by userid
having sum( group = 'y' ) > 0 and
sum( group <> 'y') > 0;
I leave it to your to put this into a query to get all the original rows.

SQL Number of Occurrences, summary Query

I have a table as below,
Product Promotion exists (Y/N) Week
A Y 1
B Y 1
C Y 1
A Y 2
B Y 2
C N 2
A Y 3
B Y 3
C Y 3
A Y 4
B Y 4
C N 4
I want to see an Promition exists combination Output on total table. Something like
A, B - 4
B,C - 2
A,C - 2
Since this is Just for 3 products looks simple.. I am looking at some thousands of records, and looking for same combinations where total count of occurrence is greater than some number. If taking the above example, if that count is 4.. then my output should be
A,B - 4
Try this:
SELECT p1, p2, COUNT(*) AS cnt
FROM (
SELECT t1.Product AS p1, t2.Product AS p2
FROM mytable AS t1
JOIN mytable AS t2
ON t1.Week = t2.Week AND
t1.Product < t2.Product AND
t1.Exists = 'Y' AND t2.Exists = 'Y') AS t
GROUP BY p1, p2
ORDER BY cnt DESC
To get only pairs exceeding a certain value, just wrap the above in a subquery and add a WHERE cnt >= someValue.
Demo here

MySQL - Select the minimum value of a column after selecting the minimum value of another column, and be in a specific range

I have following Table (tablea):
x y
------------------------
3 1
1 4
0 3
0 1
I want x to be as low as possible as top priority, and y as low as possible as lower priority, also i want y to be in a certain range.
I want x=0 and y=3 as Output and do following query:
Select x, MIN(y) AS y
FROM tablea
WHERE x = (SELECT MIN(x) FROM tablea where y between 2 AND 4);
It gives me x=0 and y=1 as Output (I understand why, but i don't know how to fix my query to get what i want).
Probably the best way is to
SELECT x, y
FROM tablea
WHERE y BETWEEN 2 AND 4
ORDER BY x, y LIMIT 1
Select x, MIN(y) AS y
FROM tablea
WHERE x = (SELECT MIN(x) FROM tablea where y between 2 AND 4)
and y between 2 AND 4;

MYSQL checking all values in a set match another set

I have two tables A and B with values below
Table A
X
------
1
2
3
Table B
X Y
------ ------
1 A
2 A
3 A
1 B
2 B
1 C
3 D
I need to find only the Y values from Table B which match All of the values in Table A.
So for the above example the only Y value that matches is A (A has an X value of 1,2,and 3)
This is an example of a "set-within-sets" subquery. I like to approach this with aggregation and a having clause.
select b.y
from tableB b join
tableA a
on b.X = a.X
group by b.y
having count(distinct b.x) = (select count(*) from tableA);

Search many props in mysql table

There is a table with 2 columns:
x y
1 1
1 2
1 3
2 2
2 3
2 4
3 1
3 2
3 5
If I need to find all X, where Y = 5. I use query
SELECT * FROM table WHERE y = 5
But, I need to find all X, where Y = 2 AND Y = 3.
How to do this on 1 query?
In result I must to get X = 1 and 2 (NOT 3!). And Y may be many
If i use SELECT * FROM table WHERE y IN (1, 2)
I will get x = 1, 2, 3, but I not need Y = 3 because in table no line x=3, y=3
SELECT x
FROM tableName
WHERE y IN (2,3)
GROUP BY X
HAVING COUNT(*) = 2
SQLFiddle Demo
if unique contraint was not specified for each x, then DISTINCT is a must
SELECT x
FROM tableName
WHERE y IN (2,3)
GROUP BY X
HAVING COUNT(DISTINCT y) = 2
SQLFiddle Demo
Try this:
SELECT * FROM TABLE b WHERE Y = 2 AND EXISTS (SELECT * FROM TABLE WHERE b.X = X AND Y = 3);
You need to use OR to create a boolean expression in your WHERE clause
SELECT * FROM table WHERE (y = 2) OR (y = 3);
If that gets tedious because of many y values, then use IN
SELECT * FROM table WHERE y IN (2,3);