I am Beginner in sql. I want following result using connecting two tables.
Here are details.
Table1(usertb)
id username password fname lname
---------------------------------
1 amit abc111 amit ganatra
2 rakesh r111 rakesh patel
3 sanajay s111 sanjay Mor
Table2 (friendlist)
id fid
--------
1 2
1 3
It means id no 1 has two friends whose idno is 1 and 2. When user of id=1 logged in there should be friendlist like,
2 rakesh patel
3 sanjay Mor
It's called a join.
Select t1.fname, t1.lname From table1 t1
inner join table2 t2 on t2.fid = t1.id
Where t2.id = 1
A couple of sql courses would stand you in good stead, stop what you are doing and take one.
Select table2.fid, tabel1.fname + table1.lname
from table1 innerjoin table2 on table2.fid = table1.id
Related
I have two tables like below. I am using SQL DB.
Table1:
Id Name
1 leo
2 lin
3 kim
4 ken
5 jon
6 kelly
Table2:
Id Name Date
1 leo 2019-04-11
2 lin 2019-04-17
3 kim 2019-02-15
4 ken 2018-04-11
I am trying to compare this two table and find out which name in table1 does not have a Date.
I need this output.
Result:
Id Name
5 jon
6 kelly
Could anyone please help me with this query.
Use a LEFT JOIN:
SELECT * FROM Table1
LEFT JOIN Table2
ON Table1.Name = Table2.Name
WHERE Table2.Name is NULL
Or you can use WHERE NOT EXISTS:
SELECT * From Table1
WHERE NOT EXISTS (SELECT 1 FROM Table2 WHERE Table1.Name = Table2.Name)
Or you can use WHERE NOT IN:
SELECT * From Table1
WHERE Name NOT IN (SELECT Name FROM Table2)
Try this below:
SELECT Id,Name FROM Table1
EXCEPT
SELECT Id,Name FROM Table2
I have two tables:
table1
ID | Name | Code
1 Joe 123
2 Sam 674
3 Mike 321
table2
ID | User Name| Code
1 Joe 123
2 Sam 674
3 Mike 321
4 John 457
5 Tim 235
Desired result:
4|John|457
5| Tim|235
Tabe1 and table2 code is identical. Table 1 code is a new field added thus contains no data for any record. Using the IDs as keys I took the codes from table2 and populated them in table1. However table1 has considerably less IDs then table2 so table2 has more codes then table1. I want to query which codes did not get transferred to table1. I thought it would be as simple as:
select *
from table2 t2
where t2.Code is not null and
t2.Code not in (select t1.Code from table1 t1 where t1.Code is not null);
This returns nothing which is strange to me. What do I need to adjust in this query? This is for oracle.
You might try the following:
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS ( SELECT 1 FROM table1 t1
WHERE t1.code = t2.code );
in my database i have 2 tables.
table1
i have ID and NAMES
table2
i have ID, IDASSOCIATION, QUANTITY
so
i have 2 names in table1:
john and tom
and in table2 i have 3 lignes
john, 1
tom, 1
john, 1
nombre one is the quantity
in my result i want get
john = 2
and tom = 1
so i do this:
sql = "SELECT t1.*, t2.IDASSOCIATION, (SELECT SUM(t2.id_qte) FROM associationdepotarticle t2 WHERE t1.fusiontable = t2.fusiontable GROUP BY t2.IDASSOCIATION) as id_qte FROM articletable t1, associationdepotarticle t2";
but i not get this:
john = 2
tom = 1
why ? what i will do, i need correction please
You can just join the tables together and use sum:
select t1.name, sum(t2.quantity)
from table1 t1
join table2 t2 on t1.id = t2.idassociation
group by t1.name
It's not completely clear from your sample data what to join on, but I assume it's the idassociation field. If you want to return those names in table1 which aren't in table2, then use an outer join.
The table is as follows:
id name
1 tom
2 bob
3 tom
4 tom
5 harry
I'm looking for a query that would result in the following:
id name duplicateposition
1 tom 1
2 bob 1
3 tom 2
4 tom 3
5 harry 1
So duplicateposition for the first tom is 1, for the second tom is 2, and for the third tom is 3. The first bob is 1 and the first harry is 1. Is there an easy query to do this?
Here is my attempt:
SELECT id, name, count(id) from table
I'm a little new to sql so that is my best shot.
You can do the following. It partitions the data by name and assigns a number within the partition. Forgot to mention I did this in SQL Server 2012.
select *,
row_number() over (partition by name order by id) as DuplicatePosition
from test
order by id
SQL Fiddle Demo
Here's a generic self join that works on most databases (although if you have access to window functions you should use them instead as they're generally faster)
select t1.id, t1.name, count(t2.id) duplicateposition
from mytable t1
join mytable t2
on t2.name = t1.name
and t2.id <= t1.id
group by t1.id, t1.name
order by t1.id
I have one complicated question. I'll try to explain it with example:
have one table that have primary key, and I want to join other table there the first's table primary key is foreign key, and I want If in the second table there is duplicate foreign key to select the number of repeatability. For example:
1-st table:
id name
--- -----
1 Greg
2 Alan
3 George
4 John
5 Peter
2-nd table
id aid data
--- ----- -------
1 2 CCCV
2 2 VVVV
3 3 DDDDD
4 3 SSSS
5 4 PPPPP
I want the result of the join to be:
id(1st table) aid name Data Number
----------- ---- ----- ----- -----
1 null Greg null 1
2 1 Alan CCCV 1
2 2 Alan VVVV 2
3 3 George DDDDD 1
3 4 George SSSS 2
4 5 John PPPPP 1
5 null Peter null 1
I searched a lot, I couldn't find anything. Maybe I do not know how search, or there is no such thing as what I want to do.
SELECT Table1.id, Table2.id as aid, Table1.name, Table2.data,
GREATEST(1, (SELECT COUNT(*)
FROM Table2 t2
WHERE t2.aid = Table1.id
AND t2.id <= Table2.id))
AS number
FROM Table1
LEFT JOIN Table2
ON Table2.aid = Table1.id
ORDER BY id, aid;
works in both MySQL and PostgreSQL.
As per my comment, you've tagged this both MySQL and PostgreSQL.
This answer is for PostgreSQL.
SELECT
table1.id,
table2.aid,
table1.name,
table2.data,
ROW_NUMBER() OVER (PARTITION BY table1.id ORDER BY table2.aid) AS number
FROM
table1
LEFT JOIN
table2
ON table1.id = table2.aid
Queries for PostgreSQL 8.3 which has no window functions.
With bigger tables it is regularly much faster to use a JOIN instead of a correlated sub-query.
The first query aggregates values for Table2 before joining to Table1, which should befaster, too:
SELECT t1.id, t2.aid, t1.name, t2.data, COALESCE(t2.ct, 1) AS number
FROM Table1 t1
LEFT JOIN (
SELECT x.aid, x.data, count(y.aid) + 1 AS ct
FROM Table2 x
LEFT JOIN Table2 y ON x.aid = y.aid AND x.id > y.id
GROUP BY x.aid, x.data
) t2 ON t2.aid = t1.id
ORDER BY t1.id, t2.ct;
And ORDER BY should be fixed.
Alternative without sub-query. Might be faster, yet:
SELECT t1.id, t2.aid, t1.name, t2.data, count(*) + count(t3.id) AS number
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.aid = t1.id
LEFT JOIN Table2 t3 ON t3.aid = t2.aid AND t3.id < t2.id
GROUP BY t1.id, t2.aid, t1.name, t2.data
ORDER BY t1.id, count(t3.id);
Not sure, didn't test with a bigger set. Test performance with EXPLAIN ANALYZE. Could you report back your results?