select employee with their manager in 2 table - mysql

I have 2 tables in my database, one with employees and the second with managers. I want to select all employees under a specified manager.
Employee table
id name
1 shivam
2 ravi
Manager table
id empId managerId
1 1 2
2 2 1
And I'd like this type of result
id name managername
1 shivam ravi
2 ravi shivam

Here is a link where I ran the code https://sqltest.net/#686252
If a table was created like this:
create table employee(
id int auto_increment,
name varchar(20),
primary key (id)
);
create table manager(
id int auto_increment,
empId int,
managerId int,
primary key (id)
);
insert into employee (name) values ('shivam'), ('ravi');
insert into manager (empId,managerId) values (1,2),(2,1);
then you could get the result you want with this statement:
SELECT e.id, e.name, e2.name managername from
employee as e
join manager as m on m.empId = e.id
join employee as e2 on e2.id = m.managerId;
Here are my results:
id name managername
1 shivam ravi
2 ravi shivam

Related

How to select differences using three tables?

I need to run a script in order to fix some rows from my table company_menu.
However, I can't build this query to get these registers.
I build the schema in this link: http://sqlfiddle.com/#!9/5ab86b
Below I show the expected result.
companies
id
name
1
company 1
2
company 2
3
company 3
menu_items
id
name
1
home
2
charts
3
users
4
projects
company_menu
id
company_id
menu_item_id
1
1
1
2
1
2
3
1
3
4
1
4
5
2
1
6
2
3
This is a result that I expected:
id
company_id
menu_item_id
1
2
2
2
2
4
3
3
1
4
3
2
5
3
3
6
3
4
CREATE TABLE companies(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)
);
CREATE TABLE menu_items(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)
);
CREATE TABLE company_menu(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
company_id INT,
menu_item_id INT,
FOREIGN KEY(company_id) REFERENCES companies(id),
FOREIGN KEY(menu_item_id) REFERENCES menu_items(id)
);
INSERT INTO companies (name) VALUES ("Company 1"),("Company 2"),("Company 3");
INSERT INTO menu_items (name) VALUES ("home"),("charts"),("users"),("projects");
INSERT INTO company_menu (company_id, menu_item_id) VALUES (1, 1),(1, 2),(1,3),(1,4);
INSERT INTO company_menu (company_id, menu_item_id) VALUES (2, 1),(2,3);
Two ways I can think of. Don't know which is more efficient. Both start with a full compannies-menu_items join to get all possible combinations, and then cut out the existing:
WHERE NOT EXISTS
select c.id company_id, m.id menu_item_id
from companies c
join menu_items m
where not exists (
select * from company_menu where company_id = c.id and menu_item_id = m.id
);
LEFT JOIN + IS NULL:
select c.id company_id, m.id menu_item_id
from companies c
join menu_items m
left join company_menu cm on cm.company_id = c.id and cm.menu_item_id = m.id
where cm.id is null;
Both are sortable on any company or menu_item column.
http://sqlfiddle.com/#!9/5ab86b/11

How to retrieve data solely by foreign key in MySQL?

The mission: Visualize the patients that were treated by surgeons.
Table: patients
id_patient
name
last_name
Table: doctors
id_doctor
name
last_name
speciality ("surgeon" is one of the possibilities)
Table: visits
id_visit
id_patient_fk (foreign key)
id_doctor_fk (foreign key)
With the id_doctor_fk pointing to id_doctor, I have to see if that doctor is a surgeon, right? Is this possible? I gave up, so I ask.
If you want patients who have seen a surgeon, you can use exists:
select p.*
from patients p
where exists (select 1
from visits v join
doctors d
on d.id_doctor = v.id_doctor_pk
where v.id_patient_fk = p.id_patient and
d.specialty = 'surgeon'
);
i created an example for your question . i hope this is help you:
CREATE TABLE #patients2 (
id_patient int,
[name] VARCHAR(100),
last_name VARCHAR(100)
);
CREATE TABLE #doctors2 (
id_doctor int,
[name] VARCHAR(100),
last_name VARCHAR(100),
speciality VARCHAR(100),
);
CREATE TABLE #dvisits2 (
id_visit int,
id_patient_fk int,
id_doctor_fk int
);
--drop table #table1
INSERT INTO #patients2
(id_patient, [name], last_name)
VALUES
(1,'patient1','last_name1'),
(2,'patient2','last_name2'),
(3,'patient3','last_name3'),
(4,'patient4','last_name4'),
(5,'patient5','last_name5'),
(6,'patient6','last_name6'),
(7,'patient7','last_name7'),
(8,'patient8','last_name8');
INSERT INTO #doctors2
(id_doctor, [name], last_name, speciality )
VALUES
(1,'doctor1','last_name1','surgeon'),
(2,'doctor2','last_name2','not surgeon'),
(3,'doctor3','last_name3','surgeon'),
(4,'doctor4','last_name4','not surgeon'),
(5,'doctor5','last_name5','surgeon'),
(6,'doctor6','last_name6','surgeon'),
(7,'doctor7','last_name7','not surgeon'),
(8,'doctor8','last_name8','surgeon');
INSERT INTO #dvisits2
(id_visit, id_doctor_fk,id_patient_fk)
VALUES
(1,1,1),
(2,2,2),
(3,3,3),
(4,4,4),
(5,5,5),
(6,6,6),
(7,7,7),
(8,8,8);
select * from #patients2 p
join #dvisits2 dv on p.id_patient = dv.id_patient_fk
join #doctors2 doc on dv.id_doctor_fk = doc.id_doctor
where doc.speciality = 'surgeon'
result =
id_patient [name] last_name id_visit id_patient_fk id_doctor_fk speciality id_doctor [name] last_name
1 patient1 last_name1 1 1 1 surgeon 1 doctor1 last_name1
1 patient1 last_name1 1 1 1 surgeon 1 doctor1 last_name1
3 patient3 last_name3 3 3 3 surgeon 3 doctor3 last_name3
3 patient3 last_name3 3 3 3 surgeon 3 doctor3 last_name3
5 patient5 last_name5 5 5 5 surgeon 5 doctor5 last_name5
6 patient6 last_name6 6 6 6 surgeon 6 doctor6 last_name6
8 patient8 last_name8 8 8 8 surgeon 8 doctor8 last_name8

How can I filter the information excluding some of the values in SQL?

I have the following tables:
create table Person (
id_person int,
name varchar(255),
primary key (id_person)
);
create table Journey (
id_journey int,
year int,
country varchar(255),
primary key (id_journey),
);
create table Person_Journey (
id_journey int references Journey (id_journey),
id_person int references Person (id_person),
primary key (id_journey, id_travel)
);
Imagine the data inserted into this tables are:
Table Person
id_person name
-----------------------------------------------
1 Jake
2 Kevin
3 Denis
Table Journey (and table Person_Journey):
id_person id_travel country
---------------------------------------------------
1 1 France
1 2 Germany
1 3 Portugal
2 4 UK
2 5 Germany
2 6 UK
3 7 UK
3 8 Portugal
3 9 Germany
As the previous example shows, person with id_person = 1 has travelled to Portugal, Germany and France. Person with id_person = 2 has travelled to the UK two times and to Germany. Finally, person with id_person = 3 has travelled to UK, Germany and Portugal.
I have to execute a SELECT on where I want to show the id_person and it's respective name from ONLY the persons that has travelled to the UK AND Germany.
So the output I want to get is:
id_person name
------------------------------
2 Kevin
I have tried executing this with no success:
select P.* from Person as P, Journey as J, Person_Journey as PJ
where P.id_person = PJ.id_person and J.id_journey = PJ.id_journey
and J.country in ("UK", "Germany")
group by (P.id_person)
having count(J.country) = 2;
How can I do this? I'm using MySQL
Try this:
SELECT Person.* FROM Person_Journey
INNER JOIN (
SELECT id_journey FROM Journey
WHERE
country in('Germany', 'UK')
GROUP BY id_journey
HAVING COUNT(DISTINCT country) = 2
) t
ON Person_Journey.id_journey = t.id_journey
INNER JOIN Person ON Person.id_person = Person_Journey.id_person
Well, one solution without joins could be with IN's:
SELECT * FROM Person
WHERE id_person IN (
SELECT id_person FROM Person_Journey
WHERE id_journey IN(
SELECT id_journey FROM Journey
WHERE
country in('Germany', 'UK')
GROUP BY id_journey
HAVING COUNT(DISTINCT country) = 2
)
)
Though, say in university, that joins are still better, than this ))
I would use EXISTS:
Pseudo code:
Select person names where their ID appear in a row "Germany" and in a row "UK" of the Journey table.
Which would translate to:
SELECT p.name FROM Person p
WHERE EXISTS (
SELECT 1 FROM Journey j
WHERE country='UK'
AND p.id_person = j.id_person
AND EXISTS (
SELECT 1 FROM Journey j
WHERE country='Germany'
AND p.id_person = j.id_person
)

how to create virtual table for inserting null value in mysql

employee
id name
1 mark
2 Clark
3 Johnson
4 peter
emp_salary
empid sal
1 2000
2 4000
2 null
4 6000
5 null
I need employee name whose salary is null...and also i need show 5000 salary where salary is null, without inserting value in emp_salary table
I tried
Select e.name,e.empid,e.salary
from table employee e inner join emp_salary em on e.id=em.empid
where em.salary=null
but this query is not working
I need to select null value and show 5000 on null value without affecting table
Select e.name,e.empid,'5000' salary
from table employee e inner join emp_salary em on e.id=em.empid
where em.salary is null
check for null with "IS NULL" not "=NULL"
This will show all employees whose salary is null, but your select will show the "salary" as 5000

how to delete duplicate rows from a table in mysql

I need to delete duplicate record from table in mysql.
So i have a table name "employee" fields are empid, empname, empssn
for getting duplicate record i have written a query
SELECT COUNT(empssn), empssn FROM employee
GROUP BY empssn
HAVING COUNT(empssn) > 1
Now I want to delete duplicate records. For that I have written query is.
DELETE FROM employee
WHERE (empid, empssn) NOT IN (
SELECT MIN(empid), empssn FROM employee
GROUP BY empssn
);
you can assume records in table are
EmpId EmpName EmpSSN
-------------------------------
1 Jack 555-55-5555
2 Joe 555-56-5555
3 Fred 555-57-5555
4 Mike 555-58-5555
5 Cathy 555-59-5555
6 Lisa 555-70-5555
7 Jack 555-55-5555
8 Mike 555-58-5555
9 Cathy 555-59-5555
10 Lisa 555-70-5555
11 Lisa 555-70-5555
but I have a mysql error is
You can't specify target table 'employee' for update in FROM clause
Does the trick of wrapping it into a derived table work for this case? (Based on http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/)
DELETE FROM employee
WHERE (empid, empssn) NOT IN (
SELECT empid, empssn FROM (
SELECT MIN(empid) AS empid, empssn FROM employee
GROUP BY empssn
) X
);
Edit Yep it seems to work this end.