This is my table:
id name
1 j
1 jack
1 john
So I have j and want find all names which id is equal to the id of j in single SQL?
select name
from your_table
where id in
(
select id from your_table where name = 'j'
)
Self join time
select t1.name
from your_table t1
inner join your_table t2 on t2.id = t1.id
where t2.name = 'j'
Related
table looks like this:
id group name
1 1 A
2 1 A
3 2 A
4 2 B
5 3 A
I want to select the rows with more than one distinct names in the same group. The result should be the following:
id group name
3 2 A
4 2 B
Any idea how do achieve this?
You can get the groups with aggregation:
select group
from t
group by group
having min(name) <> max(name);
You can get the original rows using join, in, or exists:
select t.*
from t
where t.group in (select group
from t
group by group
having min(name) <> max(name)
);
Note: group is a lousy name for a column because it is a SQL keyword and a MySQL reserved word.
You could do it with a correlated subquery:
SELECT t1.id, t1.group, t1.name
FROM mytable AS t1
WHERE EXISTS (
SELECT * FROM mytable t2
WHERE t2.group=t1.group AND t2.name <> t1.name
);
Or you could do it by counting distinct names in the group:
SELECT t1.id, t1.group, t2.name
FROM mytable AS t1
INNER JOIN (
SELECT t2.group FROM mytable AS t2
GROUP BY t2.group HAVING COUNT(DISTINCT t2.name) > 1
) AS t2 USING (group);
I have this table
Table1
id | name |
1 | a |
2 | c |
and other table like this
Table2
id | name |
1 | b |
2 | d |
How can I sort this two table to result
a b c d
DONE READING THIS but it will only gives me this result
a c b d
when I try this
SELECT * FROM Table1 AS t1 JOIN Table2 AS t2 ON t2.id = t1.id
ORDER BY t1.name ASC, t2.name ASC;
You can try taking a UNION of the two tables instead:
SELECT id, name
FROM Table1
UNION ALL
SELECT id, name
FROM Table2
ORDER BY name
If you want to retain information about the original source of each record, you can add a column for that:
SELECT id, name, 't1' AS source
FROM Table1
UNION ALL
SELECT id, name, 't2'
FROM Table2
ORDER BY name
Update:
If Codeigniter does not support UNION out of the box, you can always put the above query into a string and execute it natively:
$sql = "SELECT id, name FROM Table1 UNION ALL SELECT id, name FROM Table2 ORDER BY name";
$this->db->query($sql);
SELECT *
INTO #TEMP_TBL
FROM Table1 AS t1
JOIN Table2 AS t2
ON t2.id = t1.id
SELECT *
FROM #TEMP_TBL
ORDER BY ID, NAME
DROP TABLE #TEMP_TBL
If you want to use join then try below query
select * from (select x.* from table1 as x left join table2 as y on x.id = y.id) as z order by z.name;
We can use this also:
SELECT * FROM (
SELECT id, NAME FROM Table1
UNION ALL
SELECT id, NAME FROM Table2) t ORDER BY name
How can I do something like this in HIVE:
Table1:
ID Name Friends
1 Tom 5
Table2:
ID Name DOB
1 Jerry 10/10/1999
1 Kate Null
1 Peter 02/11/1983
1 Robert Null
1 Mitchell 09/09/2000
What I want to do is:
For each ID in table 1, find num of not null DOB and then divide by Friends
I wrote a query as:
SELECT t.ID, t.Friends, COUNT(s.DOB)/ t. Friends from Table1 t join Table2 s on (t.ID = s.ID) GROUP BY t.ID
When I do this, I get the error as FRIENDS is not part of the GROUP BY Key
The answer I am expecting is: 3/5
Just add the FRIENDS to your GROUP BY section:
SELECT t.ID, t.FRIENDS, COUNT(s.DOB)/ t. FRIENDS
from Table1 t
join Table2 s
on (t.ID = s.ID)
GROUP BY t.ID. t.FRIENDS
I prefer to write this kind of query like this:
SELECT t.ID, case when t.FIREND>0 then Cnt / t.FRIENDS ELSE 0 END
FROM Table t1
JOIN (Select ID, Count(*) AS Cnt from Table 1 GROUP BY Id) t2
ON t1.ID = t2.ID
if you have declare id,friends in table 1 as integer and id in table2 as integer, then below query will get you intended output
select a.name, concat(cast(b.cnt as string),'/',cast(a.friends as string))
from table1 a
join
(select id, count(DOB) as cnt from table2 where DOB is not null group by id) b
on (a.id = b.id)
I do have a table contains 3 columns like
ID Name RID
1 xx 4
2 yy 3
3 zz 2
4 aa 1
Now I want the result as
ID Name Rname
1 xx aa
based on the RID it will refer to the ID column and brings the value of Name column as Rname. Please help me the query.
This will return the desired result:
select t1.ID, t1.Name, t2.Name as Rname
from Table t1
join Table t2 on t1.RID = t2.ID
where t1.ID = 1
You can do this with a self join:
SELECT a.id, a.name AS name, b.name AS rname
FROM mytable a
JOIN mytable b ON a.rid = b.id AND a.rid > b.rid
Suppose that I have a table called "tblTemp" with the following data
ID Name
1 A
2 B
3 C
4 A
4 B
5 A
5 B
5 C
6 C
6 B
I want to get ID from name of A&B only not A&B&C like below:
4 A
or
4 B
How can I do like this in sql?
I try the following sql but it return row 5 as well:
SELECT tblTemp.ID, tblTemp.Name
FROM tblTemp INNER JOIN
tblTemp AS tbltemp_1 ON tblTemp.ID = tbltemp_1.ID
WHERE (tblTemp.Name = 'A') AND (tbltemp_1.Name = 'B')
One of the ways to compare sets is to take the count of group, filter groups by search set, and see if number of matches per group equals original number of group members:
select tblTemp.ID
from tblTemp
inner join
(
select ID,
count(*) GroupCount
from tblTemp
group by ID
having count(*) = 2
) g
on tblTemp.ID = g.ID
where tblTemp.Name in ('A', 'B')
group by tblTemp.Id, g.GroupCount
having count (*) = g.GroupCount
This should work on both MySql and Sql Server.
You can play with this code # Sql Fiddle.
try:
SELECT distinct ID
FROM tblTemp a
LEFT JOIN tblTemp b
ON a.ID = b.ID AND
b.name = 'C'
WHERE b.ID IS NULL;
select id from table
group by id
having min(Name)='A' and max(Nmae)='B'
SELECT ID, Name FROM tblTemp WHERE (Name = 'A' OR Name = 'B') and ID not in(SELECT ID FROM tblTemp WHERE Name = 'C')
Do you just want a list of distinct IDs that have A or B but not C?
SELECT distinct ID
FROM tblTemp
WHERE Name = 'A' or Name = 'B'
EXCEPT
SELECT distinct ID
FROM tblTemp
WHERE Name = 'C'
My above solution work for 'A' and/or 'B', but I notice you actually want 'A' and 'B' with no or. In that case:
SELECT distinct ID
FROM tblTemp as T1
INNER JOIN tblTemp as T2
ON T1.ID = T2.ID
WHERE T1.Name = 'A' and T2.Name = 'B'
EXCEPT
SELECT distinct ID
FROM tblTemp
WHERE Name = 'C'
this is an extension of your original code and is perhaps not as elegant as #Madhivanan's solution but it is more general should A B and C change to words for example.