English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I've created a table called :FACULTY
It has the following details --

FacultyCode : Not null, Primary key, Starts with ‘F’
FacultyName : Not null
DateOfJoin : Not null
DeptCode: Must be either CSE,IT, CA, CHEM, MTHS, PHYS, HUM, BBA

Another table that I've created is :DEPARTMENT
It has following details --

DeptCode: Not null, Primary key
DeptName: Not null
HOD : Foreign key references FacultyCode of table FACULTY

Then I've inserted appropriate data into these 2 tables. Now the question is "Name the departments having highest number of faculties and display the names of faculties" . How to solve this in SQL queries ?

2006-10-24 20:29:16 · 3 answers · asked by Innocence Redefined 5 in Computers & Internet Programming & Design

3 answers

You can use the following SQL for the same:-

select c.DeptCode, c.DeptName, d.FacultyCode, d.FacultyName
from DEPARTMENT c, FACULTY d
where c.DeptCode in (
select b.DeptCode
from FACULTY b
group by b.DeptCode
having count(*) =
(
select max(count(*))
from FACULTY a
group by DeptCode
)
)
andc.DeptCode = d.DeptCode;

Breaking down the SQL statement:-
1. The SQL statement to retrieve the maximum count of the faculty within a department:-
select max(count(*))
from FACULTY a
group by DeptCode

2. Corresponding to this maximum count we find out the departments for which that many number of faculty are present.

select b.DeptCode
from FACULTY b
group by b.DeptCode
having count(*) =
(
select max(count(*))
from FACULTY a
group by DeptCode
)

3. Corresponding to these retrieved departments we retrieve the Department Code, Department Name, Faculty Code & Faculty Name:-

select c.DeptCode, c.DeptName, d.FacultyCode, d.FacultyName
from DEPARTMENT c, FACULTY d
where c.DeptCode in (
select b.DeptCode
from FACULTY b
group by b.DeptCode
having count(*) =
(
select max(count(*))
from FACULTY a
group by DeptCode
)
)
andc.DeptCode = d.DeptCode;

2006-10-24 22:16:56 · answer #1 · answered by mashiur1973 2 · 0 0

I have this on my machine at the office and to be honest I never use it because it does not help me very well when analyzing a query. With a query this big and that many joins it may be an issue running it in SQL. I would probably recomend breaking this up into sub queries within the SQL statement and you may get better results. Are you using any Outer Joins or are all of these Inner Joins?

2016-03-28 06:55:44 · answer #2 · answered by Anonymous · 0 0

select deptname, facultyname, count(facultycode)
from faculty, department
where faculty.deptcode = department.deptcode and
group by faculty.deptcode

in not sure.. but its something like that :)

2006-10-24 21:39:00 · answer #3 · answered by deval_agrifarman 6 · 0 0

fedest.com, questions and answers