I am trying to get the emp_id of everyone who sold more than 100000.
This query returns all employees and their sales:
SELECT SUM(total_sales), emp_id
FROM works_with
GROUP BY emp_id;
Something like this doesn't work:
SELECT emp_id
FROM works_with
WHERE SUM(total_sales), emp_id
GROUP BY emp_id > 100000;
Do I need to use nested queries?
For this solution you need to group the data by emp_id and then with group by you need to use having clause instead of where clause to filter the data with following condition SUM(total_sales)>100000.
So with your first query is perfect. You just need to add having SUM(total_sales)>100000 with it.
SELECT emp_id
FROM works_with
GROUP BY emp_id
having SUM(total_sales)>100000;
Related
I have 2 columns:
DEPT_ID number;
DEPT_SUB_ID varchar2(5);
I want to find all the DEPT_ID's which have more than one unique value for DEPT_SUB_ID.
How can this be done?
I would use:
select dept_id
from x
group by dept_id
having min(dept_sub_id) <> max(dept_sub_id);
In many cases, two simple aggregations (such as min() or max()) have better performance than a count(distinct).
select dept_id, count(distinct dept_sub_id)
from x
group by dept_id
having count(distinct dept_sub_id) > 1
The keys here are count(distinct) which counts the distinctly different dept_sub_id's within the dept_id
Group by, obviously groups the counts by dept_id
having is like a "where" ran after the group-by does its grouping.
Simply use count with distinct.
select dept_id,count(distinct dept_sub_id)
from dept
group by dept_id
having count(distinct dept_sub_id) > 1;
I have employee table with emp id (emp_id) and department (dep_id) fields. An employee could be working in more than one Department. I want to write a sql query to display unique emp_ids who work in more than one department.
Pl help me to write sql query.
Thx
Answered here: SQL query for finding records where count > 1
You need to use count, group by and having like this.
select emp_id, count(dep_id)
from employee_department
group by emp_id
having count(dep_id)>1
Query
SELECT COUNT(*)
FROM
(
SELECT id_employee, COUNT(*) AS CNT
FROM Department_Employee
GROUP BY id_employee
) AS T
WHERE CNT > 1
Given a staff table with the following attributes:
ec,name,code,dob,salary
List the staff members earning more than the average salary.
My solution:
select* from staff where salary > avg(salary);
What is wrong with it?
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Example using WHERE clause :
select *
from staff
where salary > (select avg(salary) from staff)
Example using HAVING clause :
select deptid,COUNT(*) as TotalCount
from staff
group by deptid
having count(*) > 2
Having clause specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.
I've retrieved a count of the duplicates and their occurrences using the below code
select empID, count(empID) AS DUPLICATEempID
from employees
group by empID
having count (empID) > 1
I now want the table to include the number of rows returned (i.e. insert the number on the table returned)
thanks in advance.
In SAS, you can do this with a subquery:
select empId, DUPLICateempID, count(*) as NumDuplicates
from (select empID, count(empID) AS DUPLICATEempID
from employees
group by empID
having count (empID) > 1
) t
When you have an aggregation function without a group by, it applies the function to the whole table and re-merges the results.
I have a table Employees, which has Fields as below:
Employee_name,Employee_id,Employee_status,Employee_loc,last_update_time.
This table does not have any constraint.
I have tried the below query.
select Employee_name, count(1)
from Employees
where Employee_status = 'ACTIVE'
Group by Employee_name,Employee_loc
having count(Employee_name) > 1
order by count(Employee_name) desc
In the select, I need to get Employee_id too.. Can any one help on how to get that?
You can just add Employee_id to the query, and also add it to the group by clause. (Adding it to the grouping won't make any difference in the query results, assuming each employee name each employee id is unique).
If the grouping does make a difference, that implies that some combinations of employee name and location have more than one ID associated with them. Your query would therefore need to decide which ID to return, possibly by using an aggregate function.
SELECT EMPLOYEE_NAME, EMPLOYEE_ID, COUNT(1)
FROM
EMPLOYEES
WHERE
EMPLOYEE_NAME IN
(
SELECT EMPLOYEE_NAME
FROM EMPLOYEES
WHERE Employee_status = 'ACTIVE'
GROUP BY Employee_name,Employee_loc
HAVING COUNT(*) > 1
)
GROUP BY EMPLOYEE_NAME, EMPLOYEE_ID
You can also use partition by clause and select whichever columns you want to see irrespective of the columns you are using for aggregation.
A very short and simple explanation here - Oracle "Partition By" Keyword