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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:06:41+00:00 2026-06-13T15:06:41+00:00

I have various tables like Student primary id , students name, course Papers paper

  • 0

I have various tables like

Student
primary id , students name, course

Papers
paper id, papername, course, semester, type

StudentOptions
primary id, studentid (foreign key - reference student id) and paperid (foreign key - references paper id)

StudentsTerm
studentid (foreign key- references student id) and student semester

Now the kind of result i want is,

I want to choose a course then the term, which will give me the number of papers/subject it has with their types (Mandatory/Optional) and with that i want to have the count of number of students studying those papers from all these tables.

I don’t wanna create any view or stuff, Just a normal select query will do.

The query i am running is :

SELECT p_name,
       p_id,
       type,
       Count(sps.studentid) AS counts
FROM   students,
       str,
       papers
       LEFT JOIN sps
              ON sps.paperid = papers.p_id
WHERE  sps.studentid = students.studentid
       AND students.studentid = str.studentid
       AND sps.studentid = str.studentid
       AND str.semesterid = p_semid
       AND str.sessionid = 12
       AND students.course = c_id
       AND c_id = 6
       AND p_semid = 1
GROUP  BY p_id 
  • 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-13T15:06:42+00:00Added an answer on June 13, 2026 at 3:06 pm

    As better practice, if you are going to be using explicit JOIN syntax, then don’t use implicit syntax. In the query you posted above, you select from papers, but you don’t use it anywhere. Also, your column names are slightly ambiguous. If it’s easier, alias each table using a single letter, or explicitly prefix the column names. If you’re using an aggregate with GROUP BY, you cannot select columns which are not in the group.

    Let’s assume this is your ER diagram:

    Let’s first join all the tables:

    SELECT a.id, a.name
    FROM student a
    JOIN str b ON b.student_id = a.id
    JOIN sps c ON c.student_id = a.id
    JOIN papers d ON d.id = c.paper_id
    

    Now you wish to find the number of students studying papers from a specific course and type:

    SELECT a.id, a.name
    FROM student a
    JOIN str b ON b.student_id = a.id
    JOIN sps c ON c.student_id = a.id
    JOIN papers d ON d.id = c.paper_id
    WHERE b.semester = 12
        AND d.course = 6
    

    Because your attributes are ambigiuous, it is hard to tell what tables they are coming from. If you can set up the structure and sample data on SQL Fiddle, we could help you better.

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

Sidebar

Related Questions

Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.
I have a Yahoo! Group that has its own Database tables for various reasons.
I have two tables. One has some product details, the other holds various photos
I have a handful of DBIx::Class::Core objects that model various database tables. For some
I have two tables, Band and Votes. Band has a name and an id,
I have some triggers on various tables that use raiserror to tell the user
I have a MySQL database with various tables whose records can be tagged, so
I have a query that selects a bunch of rows from various tables and
I have a maintenance application that has to turn enterprise data (from various databases/tables)
I have a set of tables in MySQL like this (foreign keys referenced by

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.