Misuse of aggregate function AVG() in SQL - sql

I have an Employees table which looks like this:
employee_id employee_name employee_salary
1 Tom 35000
2 Sarah 50000
3 David 45000
4 Rosie 55000
5 Michael 45000
I need to return the employees salary that is higher than the average salary but the below command is having an error saying '1 misuse of aggregate function AVG()'.
SELECT employee_salary
FROM Employees
WHERE employee_salary > AVG(employee_salary);
The output that I'm expecting to get is:
employee_id employee_name employee_salary
2 Sarah 50000
4 Rosie 55000
Please advise, thank you!

I need to write the SQL query to return the number of employees for each department.
I assume you're looking for something like this:
SELECT department_id
,COUNT(employee_id) AS TotalEmployees
FROM Department
LEFT JOIN Employees
ON Employees.department_id = Department.department_id
GROUP BY department_id
Also, I need to return the employees salary that is higher than the average salary
The simplest way to return the salaries that are higher than average as a beginner sql programmer is probably something like this:
SELECT employee_salary
FROM Employees
WHERE employee_salary > (SELECT AVG(employee_salary)
FROM Employees)
As the others said, the other questions just require a bit of research. There are tonnes of resources out there to learn, but it takes time...

I need to write the SQL query to return the number of employees for each
department. However, my below command is not correct:
This is not what you ask for.
You get the join correct, but you ask for:
SELECT COUNT(Employees.employment_id)
The count how often different employment id's exist - which is 1 for an employee in one department, or X with X being the number of entries in the join. As the department_id entry is part of the employee table, this CAN NOT HAPPEN. TOTALLY not asking what you want.
I'm using the LEFT JOIN here because I am returning the result from the
Employees table is this right?
Depends - a normal join should work here. Left is only sensible if the other side can be empty - which I would assume is not possible (there are no rows with Employees.department_id being NULL).
You you want is a count (without anything in the brackets) and a group by department_id. And obviously the department id:
SELECT Department.department_id, count() FROM....
Furthermore, are there any tips to speed up SQL Server's performance?
Just pointing you to https://use-the-index-luke.com/ - indices are a cornerstone for any decent performance.
Ignoring your second question - one per question please.

Related

How to group things what are not in a specific range in SQL?

Doing revision for a SQL test I've got coming up and I'm having issues finding a query to meet one of the requirements of the questions. Heres the question:
The HR department needs to find the high-salary and low-salary employees. Modify your query from (7) to display the last name and salary for all employees whose salary is not in the range 5,000 through 12,000
Here's what I've got:
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 5000 AND 12000;
When I execute the query I get all of the employees who land between those 2 values, I need the employees who land outside of the range of the 2 values. Do I need to use '<' '>'?
Any help would be greatly appreciated :)
SELECT last_name, salary
FROM employees
WHERE salary NOT BETWEEN 5000 AND 12000

Using ROUND, AVG and COUNT in the same SQL query

I need to write a query where I need to first count the people working in a department, then calculate the average people working in a department and finally round it to only one decimal place. I tried so many different variations.
That's what I got so far although it's not the first one I tried but I always get the same error message. (ORA-00979 - not a group by expression)
SELECT department_id,
ROUND(AVG(c.cnumber),1)
FROM employees c
WHERE c.cnumber =
(SELECT COUNT(c.employee_id)
FROM employees c)
GROUP BY department_id;
I really don't know what do to at this point and would appreciate any help.
Employees Table:
Try this (Oracle syntax) example from your description:
with department_count as (
SELECT department_id, COUNT(c.employee_id) as employee_count
FROM employees c
group by department_id
)
SELECT department_id,
ROUND(AVG(c.employee_count),1)
FROM department_count c
GROUP BY department_id;
But this query not make sense. Count is integer, and count return one number for one department in this case AVG return the same value as count.
Maybe you have calculate number of employee and averange of salary on department?

SQL query from Lynda.com

I have a question about two queries. Will these two queries give the same result? I am trying to find the average salary by department:
Select s1.department, avg(s1.salary)
From
(Select department, salary
From staff
Where salary > 100000) s1
Group by s1.department
vs
select department, avg(salary) as avg_salary
from staff
where salary > 100000
group by department
Yes, it gives the same amounts back.
the bottom query gets data from a sub select which gets its data from the table, whereas the top query gets it straight from the table itself.
There are no additional filters in there. So the result will be the same.
you can test it out however, don't take my word for it.

Where clause that includes IN and NOT IN

SELECT professor.pr_name, professor.pr_college, professor.pr_salary
FROM professor
WHERE professor.pr_salary NOT IN ('Education') > professor.pr_salary IN ('Education')
ORDER BY professor.pr_salary DESC;
I have to make it so that it lists the names of all the professors not in the college of education that earn more than the professors in the college of education. What am I doing wrong?
Since you appear to be asking lots of questions about your assignment, I am not going to present a complete solution - you will need to put that together.
You can find the salaries of the professors in education like this:
SELECT pr_salary
FROM professor
WHERE pr_college = 'Education'
(It is not clear which column the 'Education' value should reference as it is not a salary - you should update this as appropriate - or if it is not in the PROFESSOR table then you ought to update the question with your table definitions.)
You can then use the MAX() aggregation function to find the highest salary of those values.
Similarly, you can find the details of professors not in education like this:
SELECT pr_name,
pr_college,
pr_salary
FROM professor
WHERE pr_college <> 'Education'
ORDER BY pr_salary DESC;
Then you need to add in another filter condition to test if the salaries out of education are higher than the maximum in education using a correlated sub-query:
WHERE ...
AND pr_salary > ( /* Previous maximum value */ )
SELECT professor.pr_name from professor where pr_college<>'Education'and professor.pr_salary >(select max(pr_salary) from professor where pr_college='Education' group by pr_college);

SQL query syntax for count/sum

Suppose I had a table like follows:
ID RANK SALARY
---------------------
1 Manager 10
2 Temp 5
3 Manager 15
4 Manager 25
I want to find the count of all managers and sum of their salaries
SELECT COUNT(Rank), SUM(Salary)
FROM Staff
WHERE Rank = Manager
I wanted to know if my syntax was right.
Also for calculating max/min/avg salaries, would simply using the aggregate functions like min/max/avg work? (Having both min/max salaries in same table). I.e.
SELECT MAX(Salary),
MIN(Salary)
FROM Staff
I was just wondering since it seems too simple.
First of all, by the looks of your design, you might be missing quotes in your first query:
SELECT count(Rank), Sum (Salary)
FROM Staff
WHERE Rank = 'Manager'
Also, you might want to see all counts and sums for all Ranks (not only Managers), all at once. If so, you should try this:
SELECT Rank, count(Rank), Sum (Salary)
FROM Staff
GROUP BY Rank
Furthermore, you might want to use multiple aggregate functions in a Rank-by-Rank basis, which would look like this:
SELECT Rank, Max(Salary), min(Salary), AVG(Salary)
FROM Staff
GROUP BY Rank
And, yes, these are simple queries. In general cases SQL is simple, it is supposed to be.
Looks good to me. Aggregate functions are very useful!
Broadly speaking, it is simple for simple requirements like this. So yes!