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);
Related
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;
Table1
ID X Y
1 0.5 0.5
2 ? ?
3 5 5
Hello All,
Basically I have a table just like the one above. Im trying to UPDATE the value of X and Y of ID = 2 with the result of a division of the values of other colums.
I tried running this query without much success,
"UPDATE Table1 SET X = X IN (WHERE ID = 1) / Y IN (WHERE ID = 3) WHERE ID = 2"
The result that I want would be
ID X Y
1 0.5 0.6
2 (0.5/6) (0.6/5)
3 5 6
In many databases, you can use a JOIN. In MySQL syntax, this looks like:
UPDATE Table1 t1 JOIN
Table1 t1_1
ON t1_1.ID = 1 JOIN
Table1 t1_3
ON t1_3.ID = 3
SET t1.X = t1_1.X / t1_3.X,
t1.Y = t1_3.X / t1_3.Y
WHERE t1.ID = 2;
Similar logic can be accomplished with using subqueries, but the code is a bit messier.
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;
I want to output two rows in a one entry but with two tables. here is my table:
table_A
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled
-----------------------------------------------------------------------
1 1 Y Y Y
2 2 Y Y N
3 10 N Y Y
table_B
-----------------------------------------------------------------------
checkkey checknum status
-----------------------------------------------------------------------
1 1 V
2 2 V
3 10 V
I want an output like this
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled status
-----------------------------------------------------------------------
1 1 Y Y Y
1 1 Y Y Y V
2 2 Y Y N
2 2 Y Y N V
3 10 N Y Y
3 10 N Y Y V
you can use a UNION , first query will get all with status as V based on tableB and second one gets all rows from A
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, b.status
from table_A a
inner join table_B b
on a.checkkey = b.checkkey
union all
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, NULL as status
from table_A a
order by checkkey, checknum;
I think this does what you want:
select a.*, 'V' as status
from table_A a
union all
select a.*, NULL as status
from table_A a
order by checkkey, checknum;
Table_B doesn't seem necessary at all.
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);