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 799499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:08:15+00:00 2026-05-14T23:08:15+00:00

Hope my subject line was descriptive enough. Basically, I have a table called Students,

  • 0

Hope my subject line was descriptive enough.

Basically, I have a table called Students, and one called Absences. The Students table contains student information (Name, DOB, StudentID etc.), the Absences table contains any absences (Date, Reason, related by StudentID). I want to get a full list of the students, plus a count of their absences.

Sample results would be:

StudentName    Absences
-------------  -----------
Johnny         3
Mark           2
James          1
Anthony        0
Sharon         0

Can someone suggest a way I should tackle this problem?

I would prefer to have a single SQL statement, possibly incorporating some views etc.

Thanks,

Jacob.

  • 1 1 Answer
  • 1 View
  • 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-05-14T23:08:16+00:00Added an answer on May 14, 2026 at 11:08 pm

    This will find the number of absences of each student, and sort with most absences first, as in your example.

    SELECT 
       studentname, count(absences_id)
    FROM
       Students s 
    LEFT OUTER JOIN 
       Absences a ON s.student_id=a.student_id
    GROUP BY studentname
    ORDER BY count(absences_id) DESC, student_name
    

    The LEFT OUTER JOIN is important, since there may be no rows in the absences table for some students. In that case, all fields from Absences for that row will be null, and the count will be zero, since it only counts non-null fields.

    If each absence can span a range of dates, e.g. absences due to sick leave or extended holiday, then you would add start_date, end_date to the Absences table and replace COUNT(absences_id) with SUM(DATEDIFF(day, start_date, end_date)), e.g.

    SELECT 
       studentname, SUM(ISNULL(DATEDIFF(day, start_date, end_date),0)
    FROM
       Students s 
    LEFT OUTER JOIN 
       Absences a ON s.student_id=a.student_id
    GROUP BY studentname
    

    You can set the ORDER BY clause to SUM(…) but it will improve readability and maintainability to wrap it in another query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to copy data for one student (clone student) that encompass three tables:
I hope the subject line is clear. I am aware of the command line
I hope the subject is clear enough, I haven't found anything specifically about this
I hope the subject line makes sense. I am working on a live chat
Hope I'm asking this correctly: I have a project Projects.Client I have my class
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
Hope to get solution to this problem. I have been stuck on it since
Hope you are fine. I have to make a Web Project (very simple) I
I have a query that is... SELECT m.msgFrom, m.msgTo, m.msgID, m.subject, m.dateTime, a.username FROM
I'm a CS master student, and next semester I will have to start working

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.