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

  • Home
  • SEARCH
  • 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 8414091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:04:56+00:00 2026-06-10T01:04:56+00:00

I want to do something like the following: SELECT studentName, COUNT(lessonCode) FROM Lessons WHERE

  • 0

I want to do something like the following:

    SELECT studentName, COUNT(lessonCode) 
    FROM Lessons
    WHERE
    COUNT(lessonCode) = 10

Currently, I’m getting all the students in the lessons table, along with the number of lessons they have, and saving the information in a table variable called studentInfo.

    DECLARE @studentInfo TABLE 
    (
    studentName varchar,
    numLessons int
    )
    INSERT INTO @studentInfo (studentName, numLessons)
    SELECT studentName, COUNT(lessonCode) 
    FROM Lessons

Then I’m using that in another select statement:

    select studentName 
    FROM @studentInfo
    WHERE
    numLessons = 10

This works fine, but the only problem is that there are about 30,000 students in my database, so the query where I get the list of students and the number of lessons they have is taking a very long time, and generally doesn’t finish before I cancel the execution of the query, usually at around 30 minutes. Is there any other way to do this more efficiently, and to achieve what I’m looking for at the top of this post?

Thanks.

  • 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-10T01:04:57+00:00Added an answer on June 10, 2026 at 1:04 am

    Use the HAVING clause:

    SELECT studentName, COUNT(lessonCode) 
    FROM Lessons
    GROUP BY studentName
    HAVING COUNT(lessonCode) = 10
    

    Also, consider creating an index on Lessons(studentName, lessonCode), it will speed your query, since you’ll be able to process it only by accessing the index.

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

Sidebar

Related Questions

How do I do something like the following SELECT ADDDATE(t_invoice.last_bill_date, INTERVAL t_invoice.interval t_invoice.interval_unit) FROM
I want to achieve something like the following: Where I can select multiple folders
I simply want to select a boolean value that indicates something like the following
I want to create a view in which I select something like the following:
Basically I have a mysql query something like the following: mysql_query(SELECT n.title, v.value FROM
In mysql I can query as following: select studentname,'student' as profession from students It
I want something like the following: foo.should_receive(:bar).with(an_instance_of(Bla))..... Is this possible? Edit: According to the
I want something like the following but would like it to be reusable for
I want to do something like the following: void handleClick(){ //do stuff } void
I want to achieve something like the following: UrlBuilder ub = new UrlBuilder(http://www.google.com/search); ub.Parameters.Add(q,request);

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.