SQL query inlvolving  "atleast"

Posted on 2007-07-24
Last Modified: 2013-12-07
     I have a question regarding SQL Query. For ex:- consider the following schema.

Student table:

studentID      name     sub1     sub2     sub3     sub4      sub5  
1001             abc         67         34          56         78         89      
1002              xyz         67         65          23         76          45

sub1,sub2,sub3... fields contain the marks in that particular subjects.

Query :-  Find the names of the students who got 50 in atleast 3 subjects.

to make this query generic, let
m   -   be the number of subjects(for the above query it is "5")
n    -   is the atleast count(for the above query it is "3")
           I am having a touch time to write a query which is generic i.e m , n can be any value.Any help would be greatly appreciated.

Database : SQL Server 2000
                     It would be great if the query is not specific to any Database vendor i.e if it is according to standards.


Question by:ramesh_axl
    LVL 92

    Accepted Solution

    SELECT studentID
    FROM Student
    WHERE ((CASE WHEN sub1 > 49 THEN 1 ELSE 0 END) +
        (CASE WHEN sub2 > 49 THEN 1 ELSE 0 END) +
        (CASE WHEN sub3 > 49 THEN 1 ELSE 0 END) +
        (CASE WHEN sub4 > 49 THEN 1 ELSE 0 END) +
        (CASE WHEN sub5 > 49 THEN 1 ELSE 0 END)) > 2

    Author Comment

    Hi Patrik,

              Thanks for your  reply. I am trying to write a query which is generic i.e i will be knowing the number of subjects at run time.

    Author Comment

    Hi Patrick,
               I am able to work with the solution you have given.

    Thanks a lot

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now