Select rows where column is two different values - sql

I have a table like this:
id
item
value
1
Apple
1
2
Apple
7
3
Banana
5
4
Banana
6
5
Pear
1
6
Pineapple
7
How do I only select rows that have both [1,7] as values?
Output:
id
item
value
1
Apple
1
2
Apple
7

Edit: You can try using a sub query like this
SELECT * FROM TABLE
WHERE ITEM IN
(SELECT ITEM FROM TABLE WHERE VALUE IN (1,2)
GROUP BY ITEM HAVING COUNT(ITEM) = 2)

Related

Select * in Postgres not selecting duplicates; how to get duplicates?

when I run this query:
SELECT item_id
FROM cart
WHERE user_id = 6
I get: 2 3 4 4 4 4 4 3 3 2 2
---when I run:
SELECT * FROM items
WHERE id IN(
SELECT item_id FROM cart
WHERE user_id = 6
I get:
1 Apple 0.40 red fruit
2 Banana 0.30 yellow
3 Mango 1 yellow fruit
4 Carrot 0.50 orange vegetable
I'd like to get it where it sends the duplicates too for how many times it is there. I had the same problem with SUM where it only add the values once but not for any duplicates. How is there a way to get the duplicates to be returned also? and hopefully with SUM too
Sounds like you want a join:
SELECT itm.*
FROM items itm
JOIN cart crt on crt.item_id = itm.id
WHERE crt.user_id = 6;

SQL Query with Distinct and Count

I want to know how to query a table with both distinct and count feature.
For example:
SELECT ID, Email, ProductName, ProductModel
FROM Products
What can I do to pull data with Distinct feature on ID and per ID, Count of Email?
From something like:
ID email ProductName
0 a abc#gmail.com apple
1 b bcd#gmail.com orange
2 a cde#gmail.com apple
3 b def#gmail.com orange
4 c efg#gmail.com grapefruit
5 a fgh#gmail.com apple
6 b ghi#gmail.com orange
7 c hij#gmail.com grapefruit
8 a ijk#gmail.com apple
9 a jkl#gmail.com apple
10 a klm#gmail.com apple
To something like:
ID Count ProductName
0 a 6 apple
1 b 3 orange
2 c 2 grapefruit
Any help would be appreciated.
SELECT
ID,
count(distinct Email) as distinct_emails,
ProductName
FROM Products
group by ID,ProductName

distinct value row from the table in SQL

There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value

Query for rows with specific string or null

I have the following tables, I need a query in Oracle SQL:
T1:
ID (PK)
1
2
3
4
5
6
7
8
T2:
Id T1-ID(FK) Value
1 1 apple
2 2 null
3 2 null
4 3 apple
5 3 null
6 4 apple
7 4 orange
8 4 null
9 5 orange
10 5 null
11 6 orange
12 6 apple
13 7 kiwi
14 8 mango
15 8 apple
16 8 null
how do I get the rows that have only apple or null or both. The query should not return any rows that have orange, kiwi, mango etc even if that ID has apple or null.
Output:
Id T1-ID(FK) Value
1 1 apple
2 2 null
3 2 null
4 3 apple
5 3 null
select t.*
from t2 t
where (t.value = 'apple' or t.value is null)
and not exists (select 1
from t2 x
where x.t1_id = t.t1_id
and x.value <> 'apple')
order by t.id;
SQLFiddle example: http://sqlfiddle.com/#!15/a9f28/1
Here is a solution using sub-query and GROUP_CONCAT.
SELECT *
FROM T2
WHERE `T1-ID` IN
(SELECT
`T1-ID`
FROM T2
GROUP BY `T1-ID`
HAVING 'apple' = GROUP_CONCAT(DISTINCT Value))

Ask for Int value in any of my columns from my Table

Lets say i have a table named 'Test' with these columns and values
ID Apple Pear Pineapple Orange
1 2 3 4 4
2 1 12 1 0
Now I want to make a SELECT statement to return all the rows where there is a value of 4 in any column.
I could do it like this, but I have many columns to check, so I need to find a other way to do this.
SELECT * FROM Test
WHERE test.apple = 4 or test.apple = 4 or test.pineapple = 4 or test.pineapple = 4
Try this:
SELECT *
FROM Test
WHERE 4 IN (Apple, Pear, Pineapple, Orange)
One method is to use in:
select t.*
from test t
where 4 in (t.apple, t.pear, t.pineapple, t.orange);
You still need to list the columns, but they just go in the in list.