Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stuck on basic queries

Author  Topic 

cvocvo
Starting Member

2 Posts

Posted - 2012-02-07 : 12:38:30
Hey -- I'm just learning SQL and I've been writing queries on some test data that is set up with these six tables:


create table Person (
Name char (20),
ID char (9) not null,
Address char (30),
DOB date,
Primary key (ID));

create table Instructor ( InstructorID char (9) not null references Person(ID),
Rank char (12),
Salary integer,
Primary key (InstructorID));

create table Student ( StudentID char (9) not null references Person(ID),
Classification char (10),
GPA double,
MentorID char (9) references Instructor(InstructorID),
CreditHours integer,
primary key (StudentID));

create table Course (
CourseCode char (6) not null,
CourseName char (50),
PreReq char (6));

create table Offering (
CourseCode char(6) not null,
SectionNo integer not null,
InstructorID char(9) not null references Instructor(InstructorID),
Primary Key (CourseCode,SectionNo));

create table Enrollment ( CourseCode char(6) NOT NULL,
SectionNo integer NOT NULL,
StudentID char(9) NOT NULL references Student,
Grade char(4) NOT NULL,
primary key (CourseCode,StudentID),
foreign key (CourseCode, SectionNo) references Offering);


I'm stuck on figuring out how to do these queries though:
1. Report the course with lowest enrollments -- I want to output both the course code and the number of students enrolled in that course.
2. List the IDs and Mentor IDs of students who are taking some course, offered by their mentor.
3. List the student id, name, and completed credit hours of all freshman born in or after 1976.

Any help on these would be greatly appreciated. Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-07 : 12:41:52
i will give hints

1. look for GROUP BY and COUNT
2. a simple join involving the tables
3. again a simple join with filter on DOB

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cvocvo
Starting Member

2 Posts

Posted - 2012-02-07 : 12:51:36
For the first one I've tried a couple of things to no avail. The first thing I tried gets me the correct lowest number of students enrolled, but gives the wrong course name:

select e.CourseCode as "Course Code", min(mycount) as "Number of Students"
from (select e.CourseCode, count(distinct e.StudentID) mycount
from Enrollment e
group by e.CourseCode) eTemp, Enrollment e;


The second thing I tried returns all of the courses and the numbers enrolled for each:

select e.CourseCode, count(e.CourseCode) as Enrollment
from Enrollment e
group by e.CourseCode
having count(*) <= all( select e.StudentID from Enrollment e);


Any thoughts?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-07 : 12:51:57
is the assignment due today?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-07 : 13:40:02
quote:
Originally posted by cvocvo

For the first one I've tried a couple of things to no avail. The first thing I tried gets me the correct lowest number of students enrolled, but gives the wrong course name:

select e.CourseCode as "Course Code", min(mycount) as "Number of Students"
from (select e.CourseCode, count(distinct e.StudentID) mycount
from Enrollment e
group by e.CourseCode) eTemp, Enrollment e;


The second thing I tried returns all of the courses and the numbers enrolled for each:

select e.CourseCode, count(e.CourseCode) as Enrollment
from Enrollment e
group by e.CourseCode
having count(*) <= all( select e.StudentID from Enrollment e);


Any thoughts?


wont this be enough for 1?


SELECT TOP 1 e.CourseCode,count(distinct e.StudentID) mycount
from Enrollment e
group by e.CourseCode
order by mycount ASC


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -