I have a database called employees and I want to write an SQL query where you count the least repeating office_id and get the persons name who sits in that office. I can't get it right, will I need to use a subquery for this or is it possible without subquerys? It seems so simple yet, I can't solve it.
id
first_name
office_id
1
Stan
1
2
Danny
1
3
Elle
2
So here I would want to get the name Elle since she has the least reapted office id.
All I have so far is:
SELECT first_name, COUNT(office_id)
FROM employees
GROUP BY first_name;
But all this does is return how many times each name appears in my table. Any ideas? Thanks in advance.
Assuming working with mysql or Postgresql you can try this :
with cte as (
SELECT office_id
FROM employees
GROUP BY office_id
order by count(1) asc
limit 1
)
select e.* from cte c
inner join employees e on e.office_id = c.office_id
Using with will get the least repeating office then with inner join we get all the persons.
You can limit 2 to get the two smallest offices ...
Demo here
Related
I'm prepping for a job interview and got stumped by this question...
-- Take the 5 lowest paid employees who have done at least 10 projects
-- Given Tables:
-- Table 1: Employees
-- ID | Salary
-- Table 2: Projects
-- Employee_id | project_id | Start_dt | End_dt
I was trying to use a sub-query to first select the folks with Projects greater than 10. However, not sure how to incorporate salary into the main query.
I'll try to give you a pseudo SQL to solve this. If you can specify the database I can try to provide you with exact sql.
Left outer join Projects table with Employee both tables and
Create a index/rank/row number order by descending on Salary
put above query in a subquery with calculated column e.g. rnk
In outer query do a group by on Employee_id and Project_Id
Filter the group by rnk<= 5 and Max(End_dt) is not null and Count(*) >= 10
SELECT e.ID
FROM Employees e
JOIN Products p
ON e.ID = p.Employee_id
WHERE end_date IS NOT NULL
GROUP BY e.id
HAVING COUNT(p.project_id) > 9
ORDER BY e.Salary
LIMIT 5;
I have a query which uses MULTIPLE tables and joins. It returns a list of items. I need to count how many times each item appears in that list. I'm working on Oracle database using SQL Developer.
Use GROUP, example:
select COUNT(employee_id), department_id
from employee
GROUP BY department_id
ORDER BY department_id;
Result:
COUNT(EMPLOYEE_ID) DEPARTMENT_ID
—————————————————— —————————————
6 10
2 20
2 30
1
Do you mean like this one?
Select COUNT(e.employee_id), d.dept_name
from employee e
inner department d
on d.id = e.dept_id
GROUP BY d.dept_name
I would like to see your query to understand better your issue and find an accurate solution, but if you use the count function like this, you have the list of repeated elements with a counter column:
SELECT COUNT(*) AS "Quantity", l1.attrib, l2.attrib
FROM list1 l1, list2 l2
WHERE l1.attrib = l2.attrib
GROUP BY l1.attrib, l2.attrib;
The limit of shown data could be the GROUP BY clause and the amount of fields with different data you want to see.
select count(*),manager_id
from departments
group by manager_id;
this is my idea of how to, but it dosent gives me the amount of employees for each manager
I suspect you are selecting from the wrong table, the logic seems correct but the fact that you are selecting from a table call departments is a little suspicious.
Do you have an employee tables? Does it contains a manager_id column? If so:
select count(*),manager_id
from employees
group by manager_id;
If employee tables has only department_id column then :
SELECT d.manager_id,count(*)
FROM employees e
INNER JOIN departments d
ON(e.department_id = d.id)
Using the sample table from the HR schema
select MANAGER_ID, count(*), count(distinct EMPLOYEE_ID)
from HR.EMPLOYEES
group by MANAGER_ID
order by 1 nulls first;
gives
MANAGER_ID COUNT(*) COUNT(DISTINCTEMPLOYEE_ID)
---------- ---------- --------------------------
1 1
100 14 14
101 5 5
102 1 1
Note the first row, with manager IS NULL - i.e. there is a one employee without a manager.
Not also that I use both count(*)and count(distinct EMPLOYEE_ID). This is not relevant for this table, where EMPLOYEE_ID is PK, but in general case the former returns the number of record the latter the number of employees (which can be lower).
I have faced a similar problem of yours.
Try adding inside the count the id that you are counting ie employeeid.
If it still doesnt work after that try adding the same id in the group by.
Can you please help me with a query that would display a table like this:
Dept_ID Dept_Name
10 Admin
10 Whalen
20 Sales
20 James
20 King
20 Smith
40 Marketing
40 Neena
and so on...The Schema is HR
Display the Department Id and the Department Name and then the subsequent employees last names working under that department
SELECT Dept_ID, Dept_Name
FROM Your_Table
Simple as I can make it. It's very difficult (near impossible) to tell exactly what the query should be without more detail in terms of your table structure and some sample data.
From your edit, you may need something more like this;
SELECT DT.Dept_ID, DT.Dept_Name, ET.Emp_Name
FROM Dept_Table AS DT INNER JOIN Emp_Table AS ET ON DT.Dept_ID = ET.Dept_ID
ORDER BY Dept_ID
This shows the employees in each department on the next column, you don't really want all that in the same column.
When you union two data sets, there is NO implicit ordering, you could get the results in any order.
The get a particular order you must use ORDER BY.
To use ORDER BY, then you must have fields to do that ordering by.
In your case, the pseudo code would be...
- ORDER BY [dept_id], [depts-then-employees], [dept_name]
The middle of those three is something that YOU are going to have to create.
One way of doing that is as follows.
note: Just because you have a field to order by, does not mean that you have to select it.
SELECT
dept_id,
dept_name
FROM
(
SELECT
d.dept_id,
d.dept_name,
0 AS entity_type_ordinal
FROM
department d
UNION ALL
SELECT
d.dept_id,
e.employee_name,
1 AS entity_type_ordinal
FROM
department d
INNER JOIN
employee e
ON e.dept_id = d.dept_id
)
dept_and_emp
ORDER BY
dept_id,
entity_type_ordinal,
dept_name
Assuming there's a table in your database called departments that holds this information, your code might look like this:
select
dept_id, dept_name
from
departments
If you want to display certain columns of the table like you have asked in the question above , you can use the following syntax :
select column_names from table_name
replace:
column_names with the column names you want to display separated by a coma
2.table_name with the name of the table whose columns you wish to display
for the above question , the following code will do:
select Dept_Id , Dept_Name from Department ;
The above code works if your table name is 'Department'
I have tables which looks like below.
Employee table
Date Employee ID Employer ID Salary
2/3/2011 10 20 45666
3/12/2009 43 53 2356
Employer Table
Employer ID State
53 OH
42 MI
Trying to get the total salary by month and by state using group by clause. But not getting the results. what am i doing wrong?? any help appreciated
select date, sum(salary) from employee
group by to_char(date,'MON')
select sum(salary) from employee A, Employer B
where A.employer id=B.employer id
group by B.state
Also i need to get the top 10 distinct employee ids based on their salary
select DISTINCT employee id from employee
where rownum<=10
order by salary desc
You have to group by the exact expression in your select list, e.g.,
select to_char(date,'MON'), sum(salary)
from employee
group by to_char(date,'MON');
You probably want to include the state in your second query:
select b.state, sum(salary)
from employee A, Employer B
where A.employer_id=B.employer_id
group by B.state;
Generally speaking, stating in your question that you're "not getting the results" is not very helpful to the folks you're asking help of. Please provide any error messages or output that describes what "not getting the results" means.