Interesting mysql query - join

I have 2 different tables.
My goal is to find people who use the same ip address with different names.
Table 1 - logs
Fields: member_id, ip_adress
Table 2 - members
Fields: id, name, last_name

You can use GROUP_CONCAT for that:
SELECT
ip_adress, GROUP_CONCAT(name)
FROM table1
LEFT JOIN
table2 ON table1.member_id = table2.id
GROUP BY
ip_adress

In MySQL 8.x you can use ROW_NUMBER() to identify which IP addresses have multiple members.
For example:
select id, name, last_name
from (
select m.*,
row_number() over(partition by l.ip_address
order by m.name, m.last_name) as rn
from members m
join logs l on l.member_id = m.id
) x
where rn = 2
EDIT FOR MYSQL 5.7
Since MySQL 5.x doesn't have window functions, you can do:
select m.*
from members m
join logs l on l.member_id = m.id
where l.ip_address in (
select l.ip_address
from members m
join logs l on l.member_id = m.id
group by l.ip_address
having min(m.name) <> max(m.name) or min(m.last_name) <> max(m.last_name)
)
See Running Example at DB Fiddle.

Related

LEFT JOIN using _PARTITIONDATE

I'm currently using StandardSQL in BigQuery, I tried to join two sets of table one of which is a pseudo-column table partitioned by day.
I tried to use this query below:
SELECT
DISTINCT DATE(create_time) AS date,
user_id,
city_name,
transaction_id,
price
FROM
table_1 a
LEFT JOIN (SELECT user_id, city_name FROM table_2) b
ON (a.user_id = b.user_id AND DATE(create_time) = _PARTITIONDATE)
I've tried this kind of JOIN (using _PARTITIONDATE) and worked out, but for this particular query I got an error message:
Unrecognized name: _PARTITIONDATE
Can anyone tell me why this happened, and how could I solve this? Thanks in advance.
The issue is that you are not selecting the _PARTITIONDATE field from table_2 when joining it so it can't recognize it:
SELECT user_id, city_name FROM table_2
In order to solve it you can add it as follows:
SELECT
DISTINCT DATE(create_time) AS date,
user_id,
city_name,
transaction_id,
price
FROM
table_1 a
LEFT JOIN (SELECT _PARTITIONDATE AS pd, user_id, city_name FROM table_2) b
ON (a.user_id = b.user_id AND DATE(create_time) = pd)
Note that you'll need an alias such as pd as it's a pseudocolumn
Probably it was working in the past if you were joining two tables directly such as in (you don't get selectivity benefits in that case):
FROM
table_1 a
LEFT JOIN table_2 b
ON (a.user_id = b.user_id AND DATE(create_time) = _PARTITIONDATE)

How can I join 2 tables?

I would like to join tables . Could you please help?
Select Number, OwnerId from DNIS.numbers
select ID,Name from DNIS.owners
Thank you.
Normally, SQL servers allow you to join tables from different databases as long as the former all belong to them. Here is an example showing you how to do this (all you have to do is to explicitly write the database names associated to each table in the query):
SELECT N.Number, N.OwnerId, O.ID, O.Name
FROM DB1.[dbo].DNIS numbers N
JOIN DB2.[dbo].DNIS owners O ON O.ID = N.OwnerId
You can also use the following syntax:
SELECT N.Number, N.OwnerId, O.ID, O.Name
FROM DB1..DNIS numbers N
JOIN DB2..DNIS owners O ON O.ID = N.OwnerId
In order to accomplish that you will have to specify the table and column names in your join statement, like so:
SELECT db1.tablename.column, db2.tablename.column
FROM db1.tablename INNER JOIN db2.tablename
ON db1.tablename.id = db2.tablename.id;

SQL placement join with student, friend, package

Issue:
You are given three tables: Students, Friends and Packages.
Students contains two columns: ID and Name.
Friends contains two columns: ID and Friend_ID (ID of the ONLY best friend).
Packages contains two columns: ID and Salary (offered salary in $ thousands per month).
Write a query to output the names of those students whose best friends got offered a higher salary than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no two students got same salary offer.
Code:
This is the code that I have come up with but it does not produce correct results. Can anyone let me know why?
select TableA.name
from
(select s.id,s.name,p.salary from students s inner join packages p on s.id=p.id) TableA,
(select f.id,f.friend_id, p2.salary from friends f inner join packages p2 on f.friend_id=p2.id) TableB
where TableA.id=TableB.id And TableA.salary>TableB.salary
order by TableB.salary desc;
I think in your query you wrote AND TableA.salary < TableB.salary instead of AND TableA.salary > TableB.salary.
Moreover I think your query can be written in a more synthetic way.
On MSSQL (but it works on MYSQL too, as query is very basic), you can try to use this one:
SELECT s.id
,s.NAME
,p.salary
, f.friend_id, p2.salary as friend_salary
FROM students s
INNER JOIN packages p ON s.id = p.id
LEFT JOIN friends f ON f.id = s.id
LEFT JOIN packages p2 ON f.friend_id = p2.id
WHERE p.salary <= p2.salary
ORDER BY s.id;
Output:
id NAME salary friend_id friend_salary
1 John 1000 2 1200
3 Pete 800 1 1000
Sample data:
CREATE TABLE students (id int, NAME VARCHAR(30));
CREATE TABLE packages (id int, salary INT);
CREATE TABLE friends (id int, friend_id INT);
INSERT INTO students values (1,'John');
INSERT INTO students values (2,'Arthur');
INSERT INTO students values (3,'Pete');
INSERT INTO packages values (1,1000);
INSERT INTO packages values (2,1200);
INSERT INTO packages values (3,800);
INSERT INTO friends values (1,2);
INSERT INTO friends values (2,3);
INSERT INTO friends values (3,1);
I used CTE for easy code readability. I am not sure whether it is fully optimized or not. But, it yields the result as expected from the question.
with std_salary as (
SELECT s.id, s.name, p.salary
FROM Students s
JOIN Packages p
ON s.id=p.id),
friend_salary as (
SELECT f.id, p.salary
FROM Friends f
JOIN Packages p
ON f.friend_id=p.id
)
SELECT name
FROM
(SELECT std_salary.name, std_salary.salary as own, friend_salary.salary as friend
FROM std_salary
JOIN friend_salary
ON std_salary.id=friend_salary.id) as final
WHERE final.own<final.friend
ORDER BY final.friend;
This worked for me in MS SQL
SELECT a.name
FROM (SELECT students.id as main_id, students.name, packages.salary
FROM students join packages on students.id = packages.id) a
JOIN (SELECT f.id as main_id1, p.salary
FROM friends f JOIN packages p ON f.friend_id = p.id) b
ON a.main_id = b.main_id1
WHERE b.salary>a.salary
ORDER BY b.salary ASC;
you have written 'where TableA.salary>TableB.salary' implying that you want to find rows where your salary is > than your friends. But the question asked was the opposite (to find names where the firends salary is > than your salary) so you can change that to 'where TableB.salary>TableA.salary' and it should work.
select my_name from
(select s.id as my_id,s.name my_name,p.salary as my_salary from students s
inner join packages p on s.id=p.id) as my_tbl inner join (select f.id as
id,f.friend_id as frnd_id,p.salary as frnd_salary from friends f inner join
packages p on f.friend_id=p.id ) as frnd_tbl on my_id=id where
frnd_salary>my_salary order by frnd_salary;

Join on Subquery: Google BigQuery

I am trying to join two tables:
select * from
(select *, STRING(ID) as ID_string from Dataset1.Table1 where create_date >= 1388514600) as A left join each Dataset2.Table1 as B on A.ID_string = B.ID
On running the above query, I get the following error:
Field 'ID_string' not found in table 'Dataset1.Table1'
Why is the join not recognizing the newly created column "ID_string"?
Solution: Try the same query, but specifying each field by its name (instead of using *).

SQL Server - Selecting records with specific, multiple entries in an associative table

I’m working in SQL Server with the following sample problem. Brandon prefers PC’s and Macs, Sue prefers PC’s only, and Alan Prefers Macs. The data would be represented something like this. I've had to make some compromises here but hopefully you get the picture:
TABLE 1: User
uID (INT PK), uName (VARCHAR)
1 'Brandon'
2 'Sue'
3 'Alan'
TABLE 2: Computer
cID (INT PK), cName (VARCHAR)
1 'Mac'
2 'PC'
TABLE 3: UCPref --Stores the computer preferences for each user
uID (INT FK), cID (INT FK)
1 1
1 2
2 1
3 2
Now, if I want to select everyone who likes PC’s OR Macs that would be quite easy. There's a dozen ways to do it, but if I'm having a list of items fed in, then the IN clause is quite straight-forward:
SELECT u.uName
FROM User u
INNER JOIN UCPref p ON u.uID = p.uID
WHERE cID IN (1,2)
The problem I have is, what happens when I ONLY want to select people who like BOTH PC’s AND Mac’s? I can do it in multiple sub queries, however that isn’t very scalable.
SELECT u.uName
FROM User u
INNER JOIN UCPref p ON u.uID = p.uID
WHERE u.uID IN (SELECT uID FROM UCPref WHERE cID = 1)
AND u.uID IN (SELECT uID FROM UCPref WHERE cID = 2)
How does one write this query such that you can return the users who prefer multiple computers taking into consideration that there may be hundreds, maybe thousands of different kinds of computers (meaning no sub queries)? If only you could modify the IN clause to have a key word like 'ALL' to indicate that you want to match only those records that have all of the items in the parenthesis?
SELECT u.uName
FROM User u
INNER JOIN UCPref p ON u.uID = p.uID
WHERE cID IN *ALL* (1,2)
Using JOINs:
SELECT u.uname
FROM USERS u
JOIN UCPREF ucp ON ucp.uid = u.uid
JOIN COMPUTER mac ON mac.cid = ucp.cid
AND mac.cname = 'Mac'
JOIN COMPUTER pc ON pc.cid = ucp.cid
AND pc.cname = 'PC'
I'm using table aliases, because I'm JOINing onto the same table twice.
Using EXISTS:
SELECT u.uname
FROM USERS u
JOIN UCPREF ucp ON ucp.uid = u.uid
WHERE EXISTS (SELECT NULL
FROM COMPUTER c
WHERE c.cid = ucp.cid
AND c.cid IN (1, 2)
GROUP BY c.cid
HAVING COUNT(*) = 2)
If you're going to use the IN clause, you have to use GROUP BY/HAVING but there is a risk in the COUNT(). Some db's don't allow more than the *, while MySQL allows DISTINCT .... The problem is that if you can't use DISTINCT in the count, you could get two instances of the value 2, and it would valid to SQL - giving you a false positive.

Resources