How to select records only in table b - sql

I have two tables. Table A and Table B. they both have two columns (Name and Salary). Both tables have duplicate name records but the salary is different. How can i write a query to select the name and salary of Table A where the name of table A is in table B's Name column.
MYSQL or MSSQL
Table A
Name Salary
john smith 100
john smith 100
sally smith 100
Dan smith 100
Table B
Name Salary
john smith 100
john smith 100
sally smith 100
result
Name Salary
john smith 100
john smith 100
sally smith 100

Try this:
SELECT Name, Salary
FROM Table_A
WHERE Name IN (SELECT DISTINCT Name
FROM Table_B) ;

Related

Pandas - join two dataframes based on one column from table and combining two columns from another table

I am trying to join two tables based on one column from one table and two column from other table
Table a
name, designation
Mr. james john, manager
Mr. jim james, tester
Mr. abe james, developer
Table b
first name, last name, emp id
james,john,1
jim, james,2
abe,james,3
I want to join table a name column with table b combining "Mr. "+first name+last name.
Here's a way to do what your question asks:
res = a.join(b.assign(name='Mr. ' + b['first name'] + ' ' + b['last name']).set_index('name'), on='name')
Input:
dataframe a:
name designation
0 Mr. james john manager
1 Mr. jim james tester
2 Mr. abe james developer
dataframe b:
first name last name emp id
0 james john 1
1 jim james 2
2 abe james 3
Output:
name designation first name last name emp id
0 Mr. james john manager james john 1
1 Mr. jim james tester jim james 2
2 Mr. abe james developer abe james 3

How to return a count of duplicates and unique values into a column in Access

I currently have this table:
First_Name
Last_Name
Jane
Doe
John
Smith
Bob
Smith
Alice
Smith
And I'm looking to get the table to look for duplicates in the last name and return a value into a new column and exclude any null/unique values like the table below, or return a Yes/No into the third column.
First_Name
Last_Name
Duplicates
Jane
Doe
0
John
Smith
3
Bob
Smith
3
Alice
Smith
3
OR
First_Name
Last_Name
Duplicates
Jane
Doe
No
John
Smith
Yes
Bob
Smith
Yes
Alice
Smith
Yes
When I'm trying to enter the query into the Access Database, I keep getting the run-time 3141 error.
The code that I tried in order to get the first option is:
SELECT first_name, last_name, COUNT (last_name) AS Duplicates
FROM table
GROUP BY last_name, first_name
HAVING COUNT(last_name)=>0
You can use a subquery. But I would recommend 1 instead of 0:
select t.*,
(select count(*)
from t as t2
where t2.last_name = t.last_name
)
from t;
If you really want zero instead of 1, then one method is:
select t.*,
(select iif(count(*) = 1, 0, count(*))
from t as t2
where t2.last_name = t.last_name
)
from t;

Update all peers with value from the single peer having data in a column

I have a table customers with multiple rows per name. I have the SELECT query reporting the right information, but I'm having trouble formulating the UPDATE statement.
How can I build an UPDATE statement to make all rows with the same name match on address, i.e. copy from the peer row with address data.
Here's my SELECT statement:
SELECT *
FROM customers b
WHERE (
SELECT count(*)
FROM customers b
WHERE a.name = b.name
) > 1
GROUP BY a.customerid
Example results:
Record 1 - John Smith / 123 Address ln
Record 2 - John Smith / <blank address>
Record 3 - John Smith / <blank address>
Record 4 - Tim Johnson / 456 Test Ave
Record 5 - Tim Johnson / <blank address>
Desired result:
Record 1 - John Smith / 123 Address ln
Record 2 - John Smith / 123 Address ln
Record 3 - John Smith / 123 Address ln
Record 4 - Tim Johnson / 456 Test Ave
Record 5 - Tim Johnson / 456 Test Ave
There are over 10,000 duplicated rows, so I'm having trouble writing sufficient constraints to build a 'catch-all' UPDATE statement to pick a 'parent' row as template for its 'child' rows. Every UPDATE statement I build seems to produce results like the following:
Record 1 - John Smith / 123 Address ln
Record 2 - John Smith / 123 Address ln
Record 3 - John Smith / 123 Address ln
Record 4 - Tim Johnson / 123 Address ln
Record 5 - Tim Johnson / 123 Address ln
UPDATE customers a
SET address = b.address
FROM customers b
WHERE a.name = b.name
AND a.address IS NULL
AND b.address IS NOT NULL;
Assuming:
<blank address> is supposed to mean NULL
There is at most one row with address per name.
Names are actually unique identifiers - which they are not for real people names.
Else you have to be more specific.

SQL query that will add a column returning a value when two other columns match (similar to vlookup in excel)

I have an employee table that lists all employees and the ID of their manager. The Manager ID refers back to the employee id in this table. I would like to add another column that returns the name of the manager since no one knows their ID numbers.
Current table is basically:
ID Name ManagerID
31 John Smith 10
32 Barb Jones 10
33 Craig Adams 32
I would like to add another column that looks up looks up the manager ID in the ID field and returns the assocaited name like below:
ID Name ManagerID ManagerName
31 John Smith 10 Ted Fish
32 Barb Jones 10 Ted Fish
33 Craig Adams 32 Barb Jones
You can use self-join here.
select e.ID, e.Name, m.ManagerID, e.Name as ManagerName
from employee e
inner join employee m
on e.ID = m.ManagerID;

How do I transpose multiple rows to columns in SQL

My first time reading a question on here.
I am working at a university and I have a table of student IDs and their supervisors, some of the students have one supervisor and some have two or three depending on their subject.
The table looks like this
ID Supervisor
1 John Doe
2 Peter Jones
2 Sarah Jones
3 Peter Jones
3 Sarah Jones
4 Stephen Davies
4 Peter Jones
4 Sarah Jones
5 John Doe
I want to create a view that turns that into this:
ID Supervisor 1 Supervisor 2 Supervisor 3
1 John Doe
2 Peter Jones Sarah Jones
3 Peter Jones Sarah Jones
4 Stephen Davies Peter Jones Sarah Jones
5 John Doe
I have looked at PIVOT functions, but don't think it matches my needs.
Any help is greatly appreciated.
PIVOT was the right clue, it only needs a little 'extra' :)
DECLARE #tt TABLE (ID INT,Supervisor VARCHAR(128));
INSERT INTO #tt(ID,Supervisor)
VALUES
(1,'John Doe'),
(2,'Peter Jones'),
(2,'Sarah Jones'),
(3,'Peter Jones'),
(3,'Sarah Jones'),
(4,'Stephen Davies'),
(4,'Peter Jones'),
(4,'Sarah Jones'),
(5,'John Doe');
SELECT
*
FROM
(
SELECT
ID,
'Supervisor ' + CAST(ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Supervisor) AS VARCHAR(128)) AS supervisor_id,
Supervisor
FROM
#tt
) AS tt
PIVOT(
MAX(Supervisor) FOR
supervisor_id IN ([Supervisor 1],[Supervisor 2],[Supervisor 3])
) AS piv;
Result:
ID Supervisor 1 Supervisor 2 Supervisor 3
1 John Doe NULL NULL
2 Peter Jones Sarah Jones NULL
3 Peter Jones Sarah Jones NULL
4 Peter Jones Sarah Jones Stephen Davies
5 John Doe NULL NULL
You will notice that the assignment to Supervisor X is done by ordering by the Supervisor-VARCHAR. If you want the ordering done differently, you might want to include an [Ordering] column; then change to ROW_NUMBER() OVER(PARTITION BY ID ORDER BY [Ordering]). Eg an [Ordering] column could be an INT IDENTITY(1,1). I'll leave that as an excercise to you if that's what's really needed.