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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:45:00+00:00 2026-06-06T22:45:00+00:00

I know that many threads has been created here & on the internet about

  • 0

I know that many threads has been created here & on the internet about this topic. But I really can’t get the final point on the difference between the two statements! I mean, trying and trying I can reach all the results I need with my queries, but I really don’t have full control of the knife!

I’m considering myself a very good programmer and a very good SQL-ista and I feel a little ashamed about this…

Here’s an example:

  • I have a table with the pages of a website (“web_page”)
  • a table with the categories (“category”).
  • a category can contain one or more pages, but not vice versa
  • a category may contain NO pages at all
  • a page can be visible or not in the website

So if I want to show all the categories and their pages, I mean both categories with pages and without, I have to do something like this:

FROM category
LEFT JOIN web_page ON ( web_page.category_id = category.category_id AND web_page.active = "Y" )

So if a category has no pages, I’ll see web_page_id NULL on the record of that category.

But if I do:

FROM category
LEFT JOIN web_page ON ( web_page.category_id = category.category_id )
...
WHERE web_page.active = "Y"...

I’ll select only the categories that have at least one web_page… But WHY?

This was just an example… I’d like to understand once forever this difference!

Thank you.

  • 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-06T22:45:01+00:00Added an answer on June 6, 2026 at 10:45 pm

    To make your query to work as you intended, put the condition into the ON clause:

    FROM category
    LEFT JOIN web_page ON web_page.category_id = category 
       and web_page.active = "Y"
    

    The reason this works is (with most databases, but not all) the WHERE clause filters the rows After they are joined. If the join doesn’t result in a web page row joining (because the category had no web pages), then all the columns of web page will be null, and any comparison of a value (like "Y") to a null is false, so those non-joining rows will be filtered out.

    However, by moving the condition into the ON clause, the condition is executed as the join is made, so that you only join rows that are active = "Y", but if there aren’t any such rows, you’ll just get the left join null web page.

    This version of the query is really saying: “give me all categories and their active web pages (if any)”

    Note that I said “most databases”… mysql for example is smart enough to understand what you are trying to do, and your query will work as you intended if run on mysql.

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

Sidebar

Related Questions

I know that this has been talked about many times here, and I have
I know that this question has been resolved in many other threads for example:
I know that there are many different questions about this sort of topic on
I know this has been addressed before but I have service that returns a
Ok I know this question has been asked many many many times before, but
I know the tagging stuff has been mentioned many times, but I can't seem
I know that the same question has been asked many times on SO and
I know that there are many Delphi database related questions available, but I'm considering
I know that horizontal partitioning...you can create many tables. How can you do this
I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many

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.