MySQL Query to find out Matching Records - mysql

Below are the 2 tables:
Employee(id, name, salary, dept_id, supervisor_id)
Department(dept_id,name)
Write a query IN MySQL or Oracle:
Select all the names of supervisor whose age is greater than 60.
Select all the names of the department where at least 1 Employee is there.

I don't see age column.(not enough information)
select distinct(name) from Department where dept_id in(select dept_id from Employee);

Related

SQL Query to fetch emloyee name, salary where employee name is provided

For an Employee table with columns EmpId, Name and Salary Table with columns Id, EmpId, Salary, where EmpId of salary table is Foreign Key of Employee Table.
Is it possible to write a SQL query so that the result contains each employee's name and the salary of all employees provided by a employee name?
Sample Data
Employee Table
EmpId Name
1 Alice
2 Bob
3 Charlie
4 Doug
Salary Table
Sample Data
Id EmpId Salary
1 1 1000
2 2 2000
3 3 3000
4 4 4000
Query I tried, but could not see, how I can write employee name say, Bob, where I want to fetch employee who have higher salary than Bob. Below query gives result with salary perspective, but how to fetch by employee name?
--Updated with Order By Clause, Thanks #Strawberry
SELECT s.salary
, e.empname
FROM
test.employee as e, test.salary as s
WHERE s.empid = e.empid AND s.salary > 2000
ORDER BY s.Salary DESC
LIMIT 3;
Here is the query with which you can fetch the salary of Bob:
SELECT Salary
FROM salary
WHERE EmpId = 2;
Now you want to fetch all names and salaries of employees who have higher salary than what you fount with the query above. Therefore you will use the above query as a nested one in WHERE. Here you go:
SELECT employee.EmpId, employee.Name, salary.Salary
FROM employee
JOIN salary ON employee.EmpId = salary.EmpId
WHERE salary > (
SELECT Salary
FROM salary
WHERE EmpId = 2
);
Edit - below in the comments you said that you want to filter by Name, not EmpId. Therefore you probably want to filtler with WHERE Name="Bob". The problem is that usually names are not unique. Therefore you may have two people named "Bob". If that is the case, you must figure out a solution which salary to get - they may be different.
Here is an example query that will get the salary of person named "Bob" who have the highest salary among all Bobs, then filter the other employees to have higher salary than whatever is fount:
SELECT employee.EmpId, employee.Name, salary.Salary
FROM employee
JOIN salary ON employee.EmpId = salary.EmpId
WHERE salary > (
SELECT MAX(Salary)
FROM salary
WHERE EmpId IN (
SELECT EmpId
FROM employee
WHERE Name="Bob"
)
);
Eventual issue may come if there is NO person named "Bob". The above query will return an empty set. If you want to print all people instead, you can do something like this:
SELECT employee.EmpId, employee.Name, salary.Salary
FROM employee
JOIN salary ON employee.EmpId = salary.EmpId
WHERE salary > (
SELECT IF(MAX(Salary) IS NULL, 0, MAX(Salary))
FROM salary
WHERE EmpId IN (
SELECT EmpId
FROM employee
WHERE Name="Bob"
)
);
Now if there is no "Bob", it will still print all people with salaries above 0.

Query BETWEEN TWO TABLES OUTPUT (MYSQL)

two table EMPLOYEE and Department
EMPLOYEE's fields are ID,Name, Salary ,DEPT_ID(foreign key to department table)
DEPARTMENT'S fields are id,NAME,LOCATION
VALUES OF EMPLOYEE TABLE WILL Be
Values OF DEPARTMENT TABLE WILL BE
Output from these table should be
DEPARTMENT_Name should be alpabetically within their count If are there same Count DEPARTMENT_Name should appear in alpabetically and count will be desc order
EMPLOYEE TABLE Values
id name salary dept_id
1 Candice 4685 1
2 Julia 2559 2
3 Bob 4405 4
4 Scarlet 2305 1
5 Ileana 1151 4
Department TABLE Values
id name location
1 Executive Sydney
2 Production Sydney
3 Resources Cape Town
4 Technical Texas
5 Management Paris
OUTPUT DATA SHOULD BE
DEPARTMENT_Name Count_OF_EMPLOYEE_SAME_DEPARTMENT
Executive 2,
Technical 2,
PRODUCTION 1,
MANAGEMENT 0,
RESOURCES 0
For what you want to show all departments even if there are no employees is a LEFT JOIN. So, start with the department table (alias "d" in the query) and LEFT JOIN to the employee table (alias "e"). using shorter alias names that make sense with context makes readability easier.
Now, you have the common "count()" which just returns a count for however many records are encountered, even if multiple in the secondary (employee) table based on common ID. In addition to count(), I also did a sum of the employee salary just for purposes that you can get multiple aggregate values in the same query.. Use it or don't, just wanted to present as an option for you.
Now the order. You want that based on the highest count first, so the COUNT(*) DESC (descending order) is the first sorting. Secondary is the department name to keep alphabetized if within the same count.
select
d.`name` Department_Name,
d.Location,
count(*) NumberOfEmployees,
sum( coalesce( e.salary, 0 )) as DeptTotalSalary
from
Department d
left join employee e
on d.dept_id = e.id
group by
d.`name`
order by
count(*) desc,
d.`name`

How to find those departments whose sum of employee's salary is greater than or equal to 10,000 in SQL?

Here is the screenshot of the table and my attempt of this query
here's the query that I am running
SELECT department
FROM employees
where ( select sum(salary)
group
by department
having sum(salary) >=10000);
It returns 0 rows whereas it should return 'COE' as the sum of its employees is 14000
I think you just want this:
select department
from employees
group by department
having sum(salary) >= 10000;

SQL: Do all attributes in a GROUP BY clause need to be listed in the SELECT clause?

Say I have a table called Employee with attributes Name, Salary, Department.
I know that this will work:
SELECT Department, AVG(Salary)
FROM EMPLOYEE
GROUP BY Department;
Would it be incorrect to discard 'Department' from the SELECT clause like so:
SELECT AVG(Salary)
FROM EMPLOYEE
GROUP BY Department;
Or would it still work?
Both queries will work and provide the output. However, second query will only result in one column (i.e. average salary) and hence, you won't be able to trace it back to department id from second query alone, e.g.:
Query 1 output:
dept | salary
1 | 5000
2 | 6000
Query 2 output:
salary
5000
6000
no, not necessarily. You can use the 2nd query too.But you can't see for which depart the salary got sorted by 'group-by' key-word.

Why are there less records returned from MySQL when using a group by clause?

I recently started work with MySQL using MySQL Workbench. I have a total of 1000 rows in my table. When I run this query I get 1000 rows:
SELECT Id, Dname, DeptId
FROM DEPARTMENT;
However, when I add a GROUP BY clause I get only 44 rows:
SELECT Id, Dname, DeptId
FROM DEPARTMENT
GROUP BY DeptId;
Why is it happening ? Is it that MySQL Workbench is truncating the rows while displaying them?
EDIT:
When I give this query, it too returns me 44 rows.
SELECT DISTINCT(DEPTID)
FROM DEPARTMENT;
Duplicate DeptId's will not be selected when u are using groupby..your table may contain only 44 unique DeptId's so it is displaying 44 rows
To get the 1st level sub-departments you should use:
select *
from DEPARTMENT
where
DeptId in (select ID from DEPARTMENT where DeptId is null)
Probably your table has 44 unique DeptId.
It isn't a bug or whatever in MySQL Workbench. It is how GROUP BY works. If you GROUP BY by Id (and it is your PK) you will get the 1000 rows.
GROUP BY clause aggregates the data as you can see here: https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
EDIT
SELECT #pv:=Id AS d_id, Dname, DeptId
FROM DEPARTMENT
JOIN
(SELECT #pv:=1) tmp
WHERE DeptId=#pv
In #pv:=1 1 is the first id in the table.
Tell me if it works. Reference: How to do the Recursive SELECT query in MySQL?