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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:57:28+00:00 2026-05-22T00:57:28+00:00

I have my tables like that: [users] [chapters] [exam_scores] – id – title –

  • 0

I have my tables like that:

[users] [chapters] [exam_scores]
- id    - title    - fk_chapter
                   - fk_user
                   - grade

I need a query to return all the chapter titles and where available the grade of the user. Here is my try:

select chapters.title as Title, exam_scores.grade as Results
from users, exam_scores left join chapters
on chapters.id = exam_scores.fk_chapter
where exam_scores.fk_user = users.id and users.id = 15;

That one returns records if there is available grade, but not the rest of the chapters.

  • 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-05-22T00:57:29+00:00Added an answer on May 22, 2026 at 12:57 am

    Since you want to get a list of all the chapters, regardless of whether or not there is a user score for the chapter. Because of that, you want to SELECT from the chapters table. Then, since you want a result for each chapter, you need to LEFT JOIN the exam_scores table so you will get records for rows that have scores, and records for scores that don’t. When using a natural join (JOIN or FROM table, table2, table3) records are only returned for which there are matching left and right values.

    So, your query should look like this:

    SELECT c.title Title, s.grade Results
    FROM chapters c
    LEFT JOIN exam_scores s ON s.fk_chapter = c.id
    WHERE (s.fk_user = 15 OR s.fk_user IS NULL)
    

    Notice, you don’t need to even include the users table here, as you’re not accessing any information for it. Your query was joining the users table and then doing the WHERE clause on it’s id key. This is not required, unless you’re getting data out of the users table. If you do, in fact, need data out of the user’s table, you’d LEFT JOIN it onto the exam_scores table like so:

    SELECT c.title Title, s.grade Results, u.*
    FROM chapters c
    LEFT JOIN exam_scores s ON s.fk_chapter = c.id
    LEFT JOIN users u ON u.id = s.fk_user
    WHERE (s.fk_user = 15 OR s.fk_user IS NULL)
    

    Hope that helps and sheds some light as to how you should structure your queries and JOINs better in the future.

    edit: I edited my answer in response to your comment. It makes complete sense why you wouldn’t be getting responses without a grade attached, and here’s why:

    When you join the 2 tables together, they’re being joined by the user id. That’s fine, because when the user id doesn’t map over to the chapters table, you get a NULL value for the user id and grade. However, since in our WHERE clause we are filtering for the user with id 15, it filters out all the NULL values for the user columns. So, to fix against this, you add an OR statement to your WHERE clause that says a user id of NULL is okay.

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

Sidebar

Related Questions

I have 2 tables: users and log. Currently my query looks like that. $stmt
I have three tables like that: Articles IdArticle Title Content Tags IdTag TagName ContentTag
I have three tables, that are like: users --------------- id etc... badges --------------- id
I have a user table 'users' that has fields like: id first_name last_name ...
I have a table that saves some account limits like users. For most rows
Say that I have two tables like those: Employers (id, name, .... , deptId).
Let's say I have two tables that look like this: TH TH TH TH
I have a 3 tables that look like this: (source: InsomniacGeek.com ) On the
I have a number of static classes that contain tables like this: using System;
I have a few tables that I've defined like the below examples: class TableA

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.