Extract the entries with the highest number - sql

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

Related

SQL : Getting count of a column and then multiplying it with a column from another table

I have three tables in my database,
Students-has student id,
Instructors which has instructor id, course name and hourly pay.
Enrollment table which has student id, course name, instructor.
I need to write a query to get the max amount of money paid to an instructor. Somethings to consider
A student can enroll into multiple courses
An instructor can teach multiple courses (Instructor is paid the same for each course)
Multiple instructors can teach the same course.
I came up with the required tables to get the entries first.
select hourlyPay, ins_id, stu_id from Instructors, Enrollment where Enrollment.ins_id = Instructors.Instr_id group by hourlyPay,ins_ID,stu_id
This gives me the output:
My end goal is to multiply the hourly pay with the student count and get the maximum, Could somebody please help me do it? I want the student count per instructor multiplied with the hourly pay.
Since I don't know the data and complete requirement. You can try something below:
(This top 1 works in SQL Server). I think in Orcle you need try FETCH FIRST number ROWS ONLY;)
select top 1 ins_id,sum(hourlyPay) Pay
from(
select hourlyPay
, ins_id
, stu_id
from Instructors
Join Enrollment ON Enrollment.ins_id = Instructors.Instr_id
group by hourlyPay,ins_ID,stu_id
) a
group by ins_id
order by 2 desc

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

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.

Using aggregate function for seprate rows

There is a table with name jobs where for different department number salary is giving. Same department can have more than one job so the salary may vary. Now i want to solve this query:
"Find the average salaries for each department without displaying the respective department numbers."
Here i should use avg but how to use it so that i can get my result of each department no separately?
I think what you're looking for is the GROUP BY clause. If I understand your question correctly then something like this should do the trick.
SELECT AVG(salary) FROM table_name GROUP BY department

SQL - Add total from columns of two seperate tables

Complete novice here, trying to find out the total number of students from both the part-time students and full-time students and display the total in a named column.
partTimeStudents**(bannerID, moduleCode, modStartDate, rvisitorID)
fullTimeStudents**(bannerID, courseCode, crsStartDate, rvisitorID)
Thank you in advance for any help given :)
select
(select count(*) from partTimeStudents)+
(select count(*) from fullTimeStudents) as Total
I agree what others have said about the database design, but here's one example of a query that would fulfill your requirement.
SELECT SUM(students_count)
FROM (
SELECT COUNT(*) AS students_count
FROM partTimeStudents
UNION ALL
SELECT COUNT(*) AS students_count
FROM fullTimeStudents
)
You should not take 2 tables just to distinguish it by full/part time student. You can simply take a flag in table like:
students(bannerID, Code, modStartDate, rvisitorID, timeFlag)
In timeFlag you can manage whether the student is full time or part time.
Now to get count of all students[full time + part time]:
select count(*) from students;
And to have counts for fullTime students or partime students:
select count(*) from students where tiemFlag=1; //--- assuming 1 is for fulltime
select count(*) from students where tiemFlag=0; //--- assuming 0 is for parttime