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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:09:42+00:00 2026-06-17T09:09:42+00:00

I don’t know if I am doing the below method correctly when using sql

  • 0

I don’t know if I am doing the below method correctly when using sql but here it goes.

I want to display all exam details (exams details (Also know as Session), questions in exam, answers, penalty, images, videos etc) But the way I am trying to do it is one big query but it is not working as no rows are being displayed.

Below are what I want to display for one exam (and only one exam):

  • Exam Details
  • All questions in Exam
  • All Answers to each question and marks each answer worth
  • Penalty (see if enabled or not)
  • Penalty marks for incorrect answers
  • Images in question
  • Videos in questions
  • Audio in questions
  • Module Details
  • Student

Below show the fields each section above require from database:

  • Exam Details

    SessionId, SessionName, SessionDuration, TotalMarks, SessionWeight

  • All questions in Exam

    QuestionId, QuestionNo, QuestionContent, NoofAnswers, QuestionMarks, OptionId, OptionType, ReplyId, ReplyType

  • All Answers to each question and marks each answer worth

AnswerId, Answer, AnswerMarks

  • Penalty (see if enabled or not)

PenaltyEnabled

  • Penalty answers

PenaltyAnswerId, PenaltyAnswer, PenaltyMarks

  • Images in question

ImageId, ImageFile

  • Videos in questions

VideoId VideoFile

  • Audio in questions

AudioId, AudioFile

  • Module Details

ModuleId, ModuleNo, ModuleName

  • Student

StudentId

My question is how should the SQL code be written in order for all these data to be shown? Do I just need one big query or a lot of little queries? My attempt is at bottom of question but below is the database tables which shows details dealing with one exam and all its details.

TABLES:

Student

StudentId (PK)  StudentForename  StudentSurname
1              James            Parker

Student_Session

SessionId (FK)  StudentId (FK)
1              1

Session

SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1

Module

ModuleId (PK)  ModuleNo  ModuleName
1              CHI2513   ICT

Question

SessionId FK) QuestionId (PK)  QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1             4                1           Question 1      1           5              1            1
1             5                2           Question 2      1           3              2            1
1             6                3           Question 3      2           6              2            2
1             7                4           Question 4      3           7              5            2
1             8                5           Question 5      1           9              5            1

Answer:

AnswerId (PK) Answer  QuestionId (FK)
1              A       4
2              C       5
3              A       6
4              B       6
5              B       7
6              D       7
7              E       7
8              G       8

Individual Answer

IndividualId (PK)  AnswerId (FK) AnswerMarks  
1                    1              3       
2                    2              5       
3                    3              3       
4                    4              3       
5                    5              2       
6                    6              2       
7                    7              3       
8                    8              9       

Penalty

PenaltyId(PK) SessionId (FK)  PenaltyEnalbed
1             1               1

PenaltyMarks

PenaltyAnswerId (PK) PenaltyAnswer PenaltyMarks QuestionId (FK)
1                     B            1            4
2                     C            1            4
3                     A            1            5
4                     B            1            5
5                     D            1            5
6                     C            2            6
7                     D            2            6
8                     A            1            7
9                     C            1            7
10                    F            1            7
11                    G            1            7
12                    A            0            8
13                    B            0            8
14                    C            1            8
15                    D            1            8
16                    E            1            8
17                    F            0            8

Reply

ReplyId (PK)  ReplyType
1               Single
2               Multiple

Option_Table

OptionId (PK)  OptionType
1               A-C        
2               A-D
3               A-E
4               A-F
5               A-G

Image

ImageId (PK)  ImageFile
1              ImageFile/Tulips.png
2              ImageFile/Daisys.png

Video

VideoId (PK) VideoFile
1              VideoFile/Speech.png

Audio

AudioId (PK) AudioFile
1               AudioFile/Song.png

Image_Question

   ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo (FK)
   1                    1             1                2

Video_Question

VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                   4

Audio_Question

AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                  5

Below was my failed attempt:

SELECT s.SessionId, 
       SessionName, 
       SessionDuration, 
       TotalMarks, 
       SessionWeight, 
       q.QuestionId, 
       q.QuestionNo, 
       QuestionContent, 
       QuestionMarks, 
       q.OptionId, 
       OptionType, 
       q.ReplyId, 
       ReplyType, 
       a.AnswerId, 
       Answer, 
       NoofAnswers, 
       AnswerMarks, 
       PenaltyEnabled, 
       PenaltyAnswerId, 
       PenaltyAnswer, 
       PenaltyMarks, 
       i.ImageId, 
       au.AudioId, 
       v.VideoId, 
       ImageFile, 
       AudioFile, 
       VideoFile, 
       s.ModuleId, 
       ModuleNo, 
       ModuleName, 
       ss.StudentId 
FROM   Student st 
       INNER JOIN Student_Session ss 
               ON st.StudentId = ss.StudentId 
       INNER JOIN Session s 
               ON ss.SessionId = s.SessionId 
       INNER JOIN Question q 
               ON s.SessionId = q.SessionId 
       INNER JOIN Answer a 
               ON q.QuestionId = a.AnswerId 
       INNER JOIN Individual_Answer ia 
               ON a.AnswerId = ia.AnswerId 
       LEFT JOIN Module m 
              ON s.ModuleId = m.ModuleId 
       LEFT JOIN Penalty p 
              ON q.SessionId = p.SessionId 
       LEFT JOIN Option_Table o 
              ON q.OptionId = o.OptionId 
       LEFT JOIN Reply r 
              ON q.ReplyId = r.ReplyId 
       LEFT JOIN Penalty_Marks pm 
              ON q.QuestionId = pm.QuestionId 
       LEFT JOIN Image_Question iq 
              ON q.QuestionId = iq.QuestionNo 
       INNER JOIN Image i 
               ON iq.ImageId = i.ImageId 
       LEFT JOIN Audio_Question aq 
              ON q.QuestionId = aq.QuestionNo 
       INNER JOIN Audio au 
               ON aq.AudioId = au.AudioId 
       LEFT JOIN Video_Question vq 
              ON q.QuestionId = vq.QuestionNo 
       INNER JOIN Video v 
               ON vq.VideoId = v.VideoId 
WHERE  s.SessionId = 1
ORDER  BY q.QuestionId 

UPDATE:

Will the code below be able to insert QuestionId into Image_Question Table after questions submitted: (This is something I found on SO but updated to my table design)

ImageQuestionToken
{
  ImageToken (PK auto)
  SessionId
}

Image{
   ImageId 
   ImageFile
}

Image_Question
{
  Image_QuestionId
  ImageId FK references Image(ImageId)
  QuestionId FK references Question(QuestionId)

}

Question
{
  QuestionId (PK Auto)
  QuestionNo
  QuestionContent
  ....
}

TempImage
{
  ImageToken FK references ImageQuestionToken(ImageToken)
  ImageFile
}

And the logic like so:

  1. User requests question form. Server sets token and includes in response to user.
  2. User uploads picture including token. Image is stored in temporary table.
  3. Step 2 repeated n times.
  4. If user submits question with token value, an entry is placed in the questions table and an id is assigned. All images in the TempImage table that share the token get inserted to the image table with the now known QuestionId. The ImageQuestionToken entry is then deleted and cascade deletes the temp images in TempImage.
  5. Else user doesn’t submit question then files are deleted and ImageQuestionToken entry deleted.
  • 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-17T09:09:43+00:00Added an answer on June 17, 2026 at 9:09 am

    There’s a problem with your foreign keys in these tables: Image_Question, Audio_Question, and Video_Question.

    You are using the column QuestionNo which is not indexed by the table Question.
    I would suggest replacing the columns as QuestionId instead and used their values.

    Image_Question
    
    ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo(FK)
       1                    1             1                2
    
    Video_Question
    
    VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo(FK)
       1                      1            1               4
    
    Audio_Question
    
    AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo(FK)
       1                      1            1               5
    

    and your statement should look like this:

    SELECT s.SessionId, 
           SessionName, 
           SessionDuration, 
           TotalMarks, 
           SessionWeight, 
           q.QuestionId, 
           q.QuestionNo, 
           QuestionContent, 
           QuestionMarks, 
           q.OptionId, 
           OptionType, 
           q.ReplyId, 
           ReplyType, 
           a.AnswerId, 
           Answer, 
           NoofAnswers, 
           AnswerMarks, 
           PenaltyEnabled, 
           PenaltyAnswerId, 
           PenaltyAnswer, 
           PenaltyMarks, 
           i.ImageId, 
           au.AudioId, 
           v.VideoId, 
           ImageFile, 
           AudioFile, 
           VideoFile, 
           s.ModuleId, 
           ModuleNo, 
           ModuleName, 
           ss.StudentId 
    FROM   Student st 
           INNER JOIN Student_Session ss 
                   ON st.StudentId = ss.StudentId 
           INNER JOIN Session s 
                   ON ss.SessionId = s.SessionId 
           INNER JOIN Question q 
                   ON s.SessionId = q.SessionId 
           INNER JOIN Answer a 
                   ON q.QuestionId = a.AnswerId 
           INNER JOIN Individual_Answer ia 
                   ON a.AnswerId = ia.AnswerId 
           LEFT JOIN Module m 
                  ON s.ModuleId = m.ModuleId 
           LEFT JOIN Penalty p 
                  ON q.SessionId = p.SessionId 
           LEFT JOIN Option_Table o 
                  ON q.OptionId = o.OptionId 
           LEFT JOIN Reply r 
                  ON q.ReplyId = r.ReplyId 
           LEFT JOIN Penalty_Marks pm 
                  ON q.QuestionId = pm.QuestionId 
           LEFT JOIN Image_Question iq 
                  ON q.QuestionNo= iq.QuestionNo
           LEFT JOIN Image i
                   ON iq.ImageId = i.ImageId 
           LEFT JOIN Audio_Question aq 
                  ON q.QuestionNo= aq.QuestionNo
           LEFT JOIN Audio au 
                   ON aq.AudioId = au.AudioId 
           LEFT JOIN Video_Question vq 
                  ON q.QuestionNo= vq.QuestionNo
           LEFT JOIN Video v 
                   ON vq.VideoId = v.VideoId 
    WHERE  s.SessionId = 1
    ORDER  BY q.QuestionId
    

    update:
    You can use the column QuestionNo afterall, you could just add index so you don’t have to overhaul your codes. hehehe

    i suggest you add this:

    ALTER TABLE `your_schema`.`Question` 
    ADD INDEX `your_idx` (`QuestionNo` ASC);
    

    and instead of these:

      LEFT JOIN Image_Question iq 
        ON q.QuestionId = iq.QuestionId -- from iq.QuestionNo
      LEFT JOIN Image i
        ON iq.ImageId = i.ImageId 
      LEFT JOIN Audio_Question aq 
        ON q.QuestionId = aq.QuestionId -- from aq.QuestionNo
      LEFT JOIN Audio au 
        ON aq.AudioId = au.AudioId 
      LEFT JOIN Video_Question vq 
        ON q.QuestionId = vq.QuestionId -- from vq.QuestionNo
      LEFT JOIN Video v 
        ON vq.VideoId = v.VideoId
    

    you use these:

      LEFT JOIN Image_Question iq 
        ON q.QuestionNo= iq.QuestionNo
      LEFT JOIN Image i
        ON iq.ImageId = i.ImageId 
      LEFT JOIN Audio_Question aq 
        ON q.QuestionNo= aq.QuestionNo
      LEFT JOIN Audio au 
        ON aq.AudioId = au.AudioId 
      LEFT JOIN Video_Question vq 
        ON q.QuestionNo= vq.QuestionNo
      LEFT JOIN Video v 
        ON vq.VideoId = v.VideoId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know what's wrong here, when I run the application it says Specified method
Don't know why this is happening, but after submitting a form via JS (using
don't know better title for this, but here's my code. I have class user
Don't know a better title but here is what im trying to do. I
Don't know if I worded the question right, but basically what I want to
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
I don't know why, but this code worked for me a month ago... maybe

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.