I am trying to retrieve the full name (concatenated and with a column header), job title, department id, department name and salary for all employees from the database and nothing I try seems to work.
select first_name ||' '|| last_name "Full Name", department_id, salary from employees
This is as far as I got since the department_name is from the departments table and the job_title is from the jobs table. The rest is from the employees table and that's why this part works. can anyone help me?
Check out https://www.w3schools.com/sql/sql_alias.asp and search for "Alias Example for Tables"
select employees.first_name, employees.last_name, jobs.job_title, departments.department_name, employees.department_id
from employees, jobs, departments
where employees.job_id = jobs.job_id and employees.department_id = departments.department_id
its working now thank you !
Related
I am having trouble with the activity I have in MySQL.
Below are the tables of my employee database, and I want to show the dept_name column from the department table, first_name and last_name from the employee table, and the title_name from the title table.
ER diagram for employee database
The instruction is to query it using a select statement. If anyone knows how to do it, please help me :< thank you so much.
You can use JOIN or write as below
SELECT e.first_name, e.last_name, d.dept_name, t.title_name
FROM employee e, department d, title t, emp_dept ed, emp_title et
WHERE
e.emp_no = ed.emp_no AND ed.dept_no = d.dept_no
AND e.emp_no = et.emp_no AND et.title_no = t.title_no
I am trying to write a query to display the last name, department
number, and department name for all the employees.
And this my working code:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments USING(DEPARTMENT_ID);
When I was trying to make the query using JOIN ON ,I faced an error saying that
Column 'department_id' in field list is ambiguous through this code
query using JOIN ON:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments ON(
employees.DEPARTMENT_ID = departments.DEPARTMENT_ID
)
I want to know why it is not working .
The ambiguity should be resolved in select clause
SELECT
last_name,
employees.department_id,
department_name
FROM employees
JOIN departments USING(DEPARTMENT_ID);
to bring to the column the chiefs of their subordinates who are tied to one department. How can this be done? I tried different joins, but I couldn’t get the column of the remaining employees to their bosses. I'm new to SQL sorry, please.
I am getting the result only for the chief to name on the department but I can't add a column with employees.
SELECT
employee.name,
employee.chief_id,
department.name,
department_id
FROM
employee
INNER JOIN
department ON department.id = employee.chief_id
You can use the GROUP_CONCAT() function as follows:
SELECT
head.name as head ,
department.name as department,
GROUP_CONCAT(employee.name) as employess
FROM
employee
INNER JOIN
employee as head ON employee.chief_id = head.id
INNER JOIN
department ON department.id = employee.department_id
group by head.name,department.name
I'm triyng to list all department id and name, + number of managers per department without duplication.
Could you check these right or wrong and tell me where should I fix? Thank you for reading.
Below is the image of my work ,
Here are queries I used (inside image but just in case)
select departments.dept_no as department_ID,
departments.dept_name as department_Name,
dept_manager.emp_no, count(distinct dept_manager.emp_no) as Numbers_of_Managers
from departments
inner join dept_manager on dept_manager.dept_no=departments.dept_no
group by dept_manager.dept_no;
You shouldn't have dept_manager.emp_no in the SELECT list. The assignment doesn't ask for it, and you're just selecting one random manager out of the group.
Other than that, it's fine.
In addition to #Barmars answer, you could modify your query as follows to be easier to read:
SELECT
d.dept_no as department_ID,
d.dept_name as department_Name,
count(distinct dm.emp_no) as Numbers_of_Managers
FROM
departments d,
dept_manager dm
WHERE
dm.dept_no = d.dept_no
GROUP BY
dm.dept_no;
Question:
Get the first and last names of all employees who entered the project at the same time as at least one other employee.
I have four tables Employee, Department, Project, Works_on as shown in below.
I tried the query:
SELECT
emp_fname,
emp_lname
FROM employee a,
works_on b
WHERE a.empno=b.empno;
Please let me know what is wrong with what I am doing?
for this you need to use both Employee (emp_no,emp_fname,emp_lname) and Works_on (emp_no, Enter_date) relationship linked through emp_no on both tables.
in your query you just used Employee table which does not have connection with checking project submitted times.
You can use either of the below query to get the desired results. I have used partition by clause to group related data.
WITH QUERY1 AS (
SELECT EMP_NO,ENTER_DATE, COUNT(*) OVER (PARTITION BY ENTER_DATE ORDER BY EMP_NO ) "t" FROM works_on)
select t2.emp_fname, t2.emp_lname from query1 t1 inner join employee t2 on t1.emp_no=t2.emp_no where "t">1;
select emp_fname, emp_lname from employee where emp_no in (
SELECT EMP_NO FROM (
SELECT EMP_NO,ENTER_DATE, COUNT(*) OVER (PARTITION BY ENTER_DATE ORDER BY EMP_NO ) "t" FROM works_on
) query1 where query1."t">1);
First of all your query will return employee names who ever worked on any project, and better you would use INNER JOIN.
If I understand right; you want employee who work on the same project (let's say ProjectX) and again the employee who entered ProjectX on same time and also there must be at least 2 employee having the same time on ProjectX.
If I am correct with the above:
You need to group your data. For instance first group projects that have at least 2 same enter_date like
SELECT Project_no, Enter_date FROM Works_on having Count(emp_no)>=2
This will give you all projects and dates that have employee more or equal than 2. Now we can find employee that worked on these projects at these dates.
SELECT emp_fname, emp_lname FROM Employee INNER JOIN Works_on AS WO ON WO.emp_no=Employee.emp_no
INNER JOIN
(
SELECT Project_no, Enter_date FROM Works_on having Count(emp_no)>=2
) AS PWith2MoreEmployee
ON PWith2MoreEmployee.Project_no=WO.Project_no
AND
PWith2MoreEmployee.Enter_date=WO.Enter_date
I hope this will give you what you are looking for.