Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:51:44+00:00 2026-06-11T23:51:44+00:00

I have a task that I’m getting a bit hung up on with SQL

  • 0

I have a task that I’m getting a bit hung up on with SQL Server and wondered if anybody could help.

Task:
Within the database, I need to create a stored procedure with the name ClassRegistration. The columns of the result set for this stored procedure need to be as follows:

  1. [Class] – The name of the class to be taught
  2. [Teacher Name] – The name of the person teaching the class
  3. [Registrations] – The number of pupils who have registered to take the class
  4. [Number Paid] – The number of pupils who have paid their fees for the class

Tables:

  1. [Teacher] : Teacher_ID (PK), TeacherName
  2. [Class] : Class_ID (PK), ClassName, Teacher_ID (FK)
  3. [ClassRegistration] Student_ID (FK), Class_ID (FK), HasPaidFees
  4. [Student] Student_ID (PK), StudentName

The college needs to know about every class, even if nobody has yet registered.

My SQL so far:

Create  PROCEDURE ClassRegistration
AS
SELECT DISTINCT 
    c.ClassName, 
    t.TeacherName, 
    COUNT(cr.Student_ID) As Registrations, 
    COUNT(case when cr.HasPaidFees = 1 then 1 else null end) As NumberPaid
FROM 
    Class As c, 
    Teacher As t, 
    ClassRegistration As cr, 
    Student As s
WHERE
    (c.Class_ID = cr.Class_ID) 
AND (cr.Student_ID = s.Student_ID)
GROUP BY c.ClassName, t.TeacherName

I can create the stored procedure fine but the output doesn’t look correct, any help would be greatly appreciated :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:51:45+00:00Added an answer on June 11, 2026 at 11:51 pm

    There is no reason to join to Student and you had 4 tables but only 2 join criteria. This is one of the pitfalls of using old table,table join syntax instead of proper, explicit joins. I also:

    • listed your "inner" tables first
    • added dbo. prefix to table references
    • added SET NOCOUNT ON to the procedure
    • removed the unnecessary DISTINCT
    • simplified the CASE expression
    CREATE PROCEDURE dbo.ClassRegistration
    AS
    BEGIN
      SET NOCOUNT ON;
    
      SELECT c.ClassName, t.TeacherName, 
        COUNT(cr.Student_ID) As Registrations, 
        COUNT(NULLIF(cr.HasPaidFees, 0)) As NumberPaid
      FROM dbo.Class As c
      INNER JOIN dbo.Teacher As t
      ON t.Teacher_ID = c.Teacher_ID
      LEFT OUTER JOIN dbo.ClassRegistration As cr
      ON c.Class_ID = cr.Class_ID
      GROUP BY c.ClassName, t.TeacherName;
    END
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script task that executes a dynamically created SQL restore statement. This
I'm a little bit unfriendly with Visitor pattern, but I have a task that
I am using MySql Database, I have a task that is, Any item should
I have a cron task that logs a value in my database every 15
I have an async task that loads images. How could i display a spinner
I have a task that runs periodically 10 second. I do some picturebox refreshing
I have a task that takes a rather long time and should run in
I have a task that's driving me crazy because i have no clue where
Just stumbled upon propertyGrid and its awesome! However, i have one task that i
I have a periodic task that needs to execute once a minute (using delayed_job).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.