Displaying department_name in outer query - sql - mysql

i have this query
select last_name,job_id,department_id
from empl_demo
where (department_id) in
(select department_id
from departments
where department_id=60);
my question is how can i display the "department_name" from the departments table? for example :
select last_name,job_id,department_id,department_name
from empl_demo, departments;
but have it match my condition
thanks

Classic INNER Join, you really need to read some basics SQL courses (https://www.w3schools.com/sql/) for example.
SELECT e_d.last_name,
e_d.job_id,
e_d.department_id,
d.department_name
FROM empl_demo e_d
INNER JOIN departments d on d.department_id = e_d.department_id
where department_id=60

It seems department_id is a common attribute to both tables. So try using JOINS like:
select A.last_name, A.job_id, B.department_id, B.department_name
from empl_demo A join departments B
on A.department_id=B.department_id
where B.department_id=60;
If you want to use subqueries then try:
select A.last_name, A.job_id, B.department_id, B.department_name
from (select * from empl_demo where department_id=60) A join
(select * from departments where department_id=60) B
on A.department_id=B.department_id;

Related

Get all records in Table B that don't exist in Table A

Got this question from a job interview via skype. I managed to take a screenshot of the question.
I imagine it's the reverse of a RIGHT JOIN, but I can't figure it out.
One simple way of doing this via an anti-join:
SELECT d.*
FROM departments d
LEFT JOIN students s
ON d.DEPARTMENT_ID = s.DEPARTMENT_ID
WHERE
s.DEPARTMENT_ID IS NULL;
We can also use an EXISTS query:
SELECT d.*
FROM departments d
WHERE NOT EXISTS (SELECT 1 FROM students s WHERE s.DEPARTMENT_ID = d.DEPARTMENT_ID);
Try this:
SELECT *
FROM department_table D
WHERE
NOT EXISTS (SELECT NULL
FROM student_table B
WHERE A.department_id=B.department_id);
You can do this by a using a subquery. Here are two example of MySQL query. You can try this.
SELECT *
FROM DEPARTMENT
WHERE
DEPERTMENT_ID NOT IN (
SELECT DEPERTMENT_ID
FROM STUDENTS
GROUP BY DEPERTMENT_ID)
Another way using where not exists:
SELECT d.*
FROM departments d
WHERE NOT EXISTS (
SELECT 1 FROM students s WHERE s.DEPARTMENT_ID = d.DEPARTMENT_ID
);

Find Count of racers from Race Table

I am new to SQL,I have two tables RACER and SPONSOR,
RACER TABLE has these columns
RACER_NAME,
SPONSOR_ID
RACER_ID- Primary KEY
SPONSOR table has these columns
SPONSOR_ID,
SPONSOR_NAME
now I want to find the SPONSOR name and no.of racer associated with that SPONSOR.
Here is what I tried:
select s.sponsor_name , (select count(*) from racer r) where INNER JOIN s.sponsor_id = r.sponsor_id
You need to understand, how JOIN works and its syntax.
select s.sponsor_name, count(*) as total_racer
from
racer r inner join sponsor s
on r.sponsor_id=s.sponsor_id
group by r.sponsor_id
You need to specify both tables in FROM clause, which you were missing.
You could use a join ( left join if not al the sponsor have racer ) and get the result without subselect using group by and count
select s.sponsor_name , count(*)
from SPONSOR s r
left JOIN racer r s.sponsor_id = r.sponsor_id
GROUP BY s.sponsor_name
Your version, with a subquery is reasonable, particularly because in MySQL it can have better performance than the corresponding GROUP BY query. However, it needs to be a correlated subquery. That looks like:
select s.*,
(select count(*)
from racer r
where s.sponsor_id = r.sponsor_id
) as cnt
from sponsor s;
In other words, choose either the JOIN method or the subquery method, but not both for the same value.

Two table joins for one query -- confusing result

I'm trying to execute two separate inner table joins in my query to return values from two tables.
SELECT pname, avg(salary)
FROM project p INNER JOIN department d on p.dnum = d.dnumber
INNER JOIN employee e ON e.dno = d.dnumber;
I'm getting one row in the result set... pname = null, avg(salary) = null.
Result set should contain 11 rows because there are 11 projects in the schema.
Can someone point me in the right direction?
Thank you
You are missing the group by:
SELECT pname, avg(salary)
FROM project p INNER JOIN
department d
on p.dnum = d.dnumber INNER JOIN
employee e
ON e.dno = d.dnumber
GROUP BY pname;
In most databases, your version would fail with an obvious syntax error. MySQL only enforces the ANSI standard if you use the ONLY_FULL_GROUP_BY mode (see here).
Use left outer join instead of inner join
Or can you show me your data tables
Do you need the department table in your query?
Does the following query return all the data you need to summarize?
SELECT pname, salary
FROM ( SELECT salary, dno AS dnum FROM employee ) e
NATURAL JOIN project;
If it does, then this might be the summarization you require:
SELECT pname, AVG( salary ) AS average_salary
FROM ( SELECT salary, dno AS dnum FROM employee ) e
NATURAL JOIN project
GROUP
BY pname;

How to find departments which has no employees without using nested query?

I have two tables:
employee(e_id,e_name,d_id),department(d_id,d_name)
my query is:
select d.d_name from department d where d.d_id not in (
select distinct e.d_id from employee e);
is there any alternative way to do this?.
Try this.
select department.* from department
left join employee
on employee.d_id = department.d_id
where employee.d_id is null

Joining tables using foreign key

I have 2 tables , employees and departments.
departments(id, department)
employees(id, department_id, name, and a bunch more here)
so employees.department_id is a foreign key to departments.id.
I need to show the table employees, but instead of department_id (showing the IDs of the departments) I need to show the actual departments name, so in place of department_id, i need to place departments.department.
How should I do this?
Your friend told you the truth :p
You just have to use a inner join between your two tables like this:
SELECT d.name, e.name, e.email, ... FROM deparments d INNER JOIN employees e ON d.id = e.department_id.
You have to adapt your field to have the desired output :)
SELECT employees.id, employees.department_id, employees.name, departments.department
FROM employees
INNER JOIN departments ON employees.department_id = departments.id
You should not use SELECT * and just take the fields you really want if it's only to take a screenshot of the table values.
Like SELECT department.name
This should cover it for you:
SELECT
E.Id
, D.Department
, E.Name
-- More stuff
FROM Employees E
INNER JOIN Departments D
ON D.id = E.department_id
SELECT employees.*, department.department
FROM employees
INNER JOIN department ON employees.department_id = department.id