Where clause that includes IN and NOT IN - sql

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);

Related

Misuse of aggregate function AVG() in 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.

Extract the entries with the highest number

I have a table that is called Teachers and looks like this
TeacherID | TeacherName | Office | Department | Email
I basically have to show which 3 departments have the most teachers in it. I know I have to use subqueries for this and that I need to first count every TeacherID that goes into a department and basically the first SELECT will be the one in which I choose these departments. Unfortunately I can't quite figure it out. I repeat that "I think" that is how I should be looking at the problem, but I am not certain. Can anyone help provide the answer for me, please?
SELECT department, COUNT(teacherid)
FROM Teachers
GROUP BY department
ORDER BY COUNT(teacherid) desc
This is the best answer I think but it can also be done like this
SELECT department, COUNT(department)
FROM Teachers
GROUP BY department
ORDER BY COUNT(department) DESC LIMIT 3
That's because every time a teacher is assigned to a certain department, the department appears again in the table. So I can just count the number of times a department appears and that will also give me the number of teachers in it. Thanks for the help all!
SELECT TOP 3 Department, COUNT(teacherid) FROM Teachers GROUP BY Department ORDER BY Teachers DESC

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?

Oracle 11g: Write a query that lists the highest earners for each department

This is a problem I've spent hours on now, and tried various different ways. It HAS to use Subqueries.
"Write a query that lists the highest earners for each department. Include the last_name, department_id, and the salary for each employee."
I've done a ton of subquery methods, and nothing works. I either get an error, or "No rows return". I'm assuming because one of the department_id is null, but even with NVL(department_id), I'm still having trouble. I tried splitting the table, and had no luck. Textbook's no help, my instructor is kind of useless, please... any help at all.
Here's a snapshot of the values, if that helps.
https://www.dropbox.com/s/bxtntlzqixdizzp/helpme.png?dl=0
You can rank the values within each department - then pull only the first place ranks in the outer query.
select a.last_name
,a.department_id
,a.salary
from (
select last_name
,department_id
,salary
,rank() over (partition by department_id order by salary desc) as rnk
from tablename
) a
where rnk=1
The partition groups all employees together who share the same department and should work regardless of the null value.
After grouping them - the order by tells that group to order on salary descending, and give a rank. You can run just the inner query to get an idea of what it does.

SQL, how to have the lowest average?

I have the following database
Student (NumStu, Name, Surname, BirthDate, Street, PC, City)
Teach (CodeTeach, Label, Coef)
Exam (numexam, DateExam, Place, #CodeTeach)
Notation (#NumStu, #NumTeach, Note)
And I want to have the list of the students who have the lowest average in the teach of Computer Science.
How to do it?
Do you need to select all students sorted by teach in Computer Science or only 1 student with lowest grade? You should add more description.
If students are inserted in Student table and their teaching grades in table Teach should be something like that(I can help you correctly after you give more detailed description):
SELECT S.NumStu, S.Name, S.Surname, S.BirthDate, S.Street, S.PC, S.City, T.ComputerScience
FROM Student AS S
INNER JOIN Teach AS T
ON T.AssociatedColumn = S.AssociatedColumn
ORDER BY T.ComputerScience -- Here you order students list from lowest grade (ascending)
SELECT *
FROM Student, Notation
ON Student.NumStu = Notation.NumStu
WHERE Notation.Note =
(
SELECT MIN(AVG(Note))
FROM Notation
GROUP BY NumStu
)
Explanation: This request will show you everything from the tables Student and Notation, when Note will be equal to the Minimum of the Average of Note.
I am not certain it will work, waiting for feedback.
P.-S: Am french too