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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:54:42+00:00 2026-06-15T20:54:42+00:00

Table structure: Table Structure http://imagebin.org/index.php?mode=image&id=238883 I want to fetch data from both the tables

  • 0

Table structure:

Table Structure http://imagebin.org/index.php?mode=image&id=238883

I want to fetch data from both the tables which satisfy some of the conditions like

    WHERE batch='2009', sex='male',course='B.Tech', branch='cs', xth_percent>60, 
     x2percent>60, gradpercent>60 and (if ranktype='other' ) then 
     no._of_not_null_semester>number elseifranktype='Leet') then 
     no._of_not_null_semester>number-2

sem 1-8 contains percentage for 8 semesters, and I want to filter results for each student if they have cleared 3 semesters or 4 semester i.e. not null values out of 8 values

 no._of_not_null_semester 

needs to be calculated, it is not a part of database, need help with that as well.

Required Query

             SELECT * FROM student_test 
                      INNER JOIN master_test ON student_test.id=master_test.id 
                      WHERE sex='male' and batch='2009' and course='B.Tech' 
                      and xthpercent>60 and x2percent>60 and 
                      WHEN ranktype='Leet' THEN 
                              SUM(CASE WHEN sem1 IS NOT NULL THEN 1 ELSE 0
                                  WHEN sem2 IS NOT NULL THEN 1  ELSE 0
                                  WHEN sem3 IS NOT NULL THEN 1 ELSE 0
                                  WHEN sem4 IS NOT NULL THEN 1  ELSE 0
                                  WHEN sem5 IS NOT NULL THEN 1  ELSE 0) >2
                      ELSE 
                             SUM(CASE WHEN sem1 IS NOT NULL THEN 1 ELSE 0
                                 WHEN sem2 S NOT NULL THEN 1  ELSE 0
                                  WHEN sem3 IS NOT NULL THEN 1 ELSE 0
                                  WHEN sem4 IS NOT NULL THEN 1  ELSE 0
                                  WHEN sem5 IS NOT NULL THEN 1  ELSE 0) >4
  • 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-15T20:54:44+00:00Added an answer on June 15, 2026 at 8:54 pm

    Without changing the structure you can’t use COUNT to achieve this.

    One way to solve the problem would be to create a semester table which would contain a row for each finished semester for each student. This would have a foreign key pointing to test_student.id and you could use COUNT(semester.id)

    If that is an option for you, it would be the best solution.

    EDIT:

    Check this out, didn’t test the query but should work generally
    I decided to do the math in the select itself to prevent calculating the same thing twice.
    The HAVING conditions are applied after your result is ready to deliver, just before a LIMIT.
    In terms of speed optimization you could try and move the sSum block into the WHERE condition just like you had it before. Probably it doesn’t make much of a difference

    SUM() does not work because it is an aggregate function which summarizes values in a column

    I did some changes to your query in addition:

    • don’t SELECT *, select specific fields and add a table identifier. ( in this case I used the aliases s for student_test AND m for master_test )
    • you put s.batch = '2009' into single quotes – if the field batch is an integer field, you should use s.batch = 2009, which would prevent MySQL from casting every single row to string to be able to compare it (int = int much faster than cast(int as varchar) = varchar) same about the other numeric values in your table

    The Query:

    SELECT
        s.id,
        s.sex,
        s.course,
        s.branch,
        ( 
            IF ( m.sem1 IS NOT NULL, 1, 0 ) +
            IF ( m.sem2 IS NOT NULL, 1, 0 ) +
            IF ( m.sem3 IS NOT NULL, 1, 0 ) +
            IF ( m.sem4 IS NOT NULL, 1, 0 ) +
            IF ( m.sem5 IS NOT NULL, 1, 0 ) +
            IF ( m.sem6 IS NOT NULL, 1, 0 ) +
            IF ( m.sem7 IS NOT NULL, 1, 0 ) +
            IF ( m.sem8 IS NOT NULL, 1, 0 )
        ) AS sSum
    FROM
        student_test s
            INNER JOIN master_test m ON m.id = s.id
    WHERE
        s.sex = 'male'
        AND
        s.batch = '2009' # I dont see this field in your database diagram!?
        AND
        s.course = 'B.Tech'
        AND
        m.xthpercent > 60
        AND
        m.x2percent > 60
    HAVING
        (
            m.ranktype = 'OTHER'
            AND
            sSum > 4
        )
        OR
        (
            m.ranktype = 'LEET'
            AND
            sSum > 2
        )
    

    If you’re generally interested in learning database design and usage I found an very interesting opportunity for you.
    Stanford University offers a free database class “Introduction to databases”. This is free and will cost you approx. 2 hours a week for 3 weeks, final exam included.

    https://class2go.stanford.edu/db/Winter2013/preview/

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

Sidebar

Related Questions

I want to export data from a table to a specifically formatted XML file.
Hi i'm trying to get some information from a website (http://omhc.nl/site/default.asp?Option=10017&m=1). The table structure
I have a data structure similar to the following picture. Table A has_many Table
here's the table structure -- phpMyAdmin SQL Dump -- version 3.4.10.1 -- http://www.phpmyadmin.net --
Is it acceptable to make changes to standard Spring Security table fields? http://static.springsource.org/spring-security/site/docs/3.0.x/reference/appendix-schema.html In,
I have a table with the following structure and i want to get the
I'm trying to realise this table structure: http://www.img-teufel.de/uploads/Unbenannt1d4f8a349jpg.jpg (not embedded due to size) It
Table structure id (p) | date | id2 (fk) | id3 (fk) where (p)
Table structure- int PrimaryKey DateTime Date int Price What I would like to do
Table structure goes something like this: Table: Purchasers Columns: id | organization | city

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.