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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:15:06+00:00 2026-05-19T02:15:06+00:00

As I build bigger, more advanced web applications, I’m finding myself writing extremely long

  • 0

As I build bigger, more advanced web applications, I’m finding myself writing extremely long and complex queries. I tend to write queries within queries a lot because I feel making one call to the database from PHP is better than making several and correlating the data.

However, anyone who knows anything about SQL knows about JOINs. Personally, I’ve used a JOIN or two before, but quickly stopped when I discovered using subqueries because it felt easier and quicker for me to write and maintain.

Commonly, I’ll do subqueries that may contain one or more subqueries from relative tables.
Consider this example:

SELECT 
  (SELECT username FROM users WHERE records.user_id = user_id) AS username,
  (SELECT last_name||', '||first_name FROM users WHERE records.user_id = user_id) AS name,
  in_timestamp,
  out_timestamp
FROM records
ORDER BY in_timestamp

Rarely, I’ll do subqueries after the WHERE clause.
Consider this example:

SELECT
  user_id,
  (SELECT name FROM organizations WHERE (SELECT organization FROM locations WHERE records.location = location_id) = organization_id) AS organization_name
FROM records
ORDER BY in_timestamp

In these two cases, would I see any sort of improvement if I decided to rewrite the queries using a JOIN?

As more of a blanket question, what are the advantages/disadvantages of using subqueries or a JOIN? Is one way more correct or accepted than the other?

  • 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-19T02:15:07+00:00Added an answer on May 19, 2026 at 2:15 am

    JOINs are preferable to separate [sub]queries.
    If the subselect (AKA subquery) is not correlated to the outer query, it’s very likely the optimizer will scan the table(s) in the subselect once because the value isn’t likely to change. When you have correlation, like in the example provided, the likelihood of single pass optimization becomes very unlikely. In the past, it’s been believed that correlated subqueries execute, RBAR — Row By Agonizing Row. With a JOIN, the same result can be achieved while ensuring a single pass over the table.

    This is a proper re-write of the query provided:

       SELECT u.username,
              u.last_name||', '|| u.first_name AS name,
              r.in_timestamp,
              r.out_timestamp
         FROM RECORDS r 
    LEFT JOIN USERS u ON u.user_id = r.user_id
     ORDER BY r.in_timestamp
    

    …because the subselect can return NULL if the user_id doesn’t exist in the USERS table. Otherwise, you could use an INNER JOIN:

      SELECT u.username,
             u.last_name ||', '|| u.first_name AS name,
             r.in_timestamp,
             r.out_timestamp
        FROM RECORDS r 
        JOIN USERS u ON u.user_id = r.user_id
    ORDER BY r.in_timestamp
    

    Derived tables/inline views are also possible using JOIN syntax.

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

Sidebar

Related Questions

I've build a small module in python which takes a list of strings/buffers in
I have an XML like the following: <documentation> This value must be <i>bigger</i> than
I developed an application locally using the Visual Studio 2008 built-in web server and
I would like to hear your directions on how to insert lines of header
I have created an AVD with the following characteristics: Target: Android Honeycomb (API level
Lately i've been through a lot of times on a single situation problem: I
Given this table structure and example data (t3 should not be used in the
We are creating a javascript library from scratch and we need some utilities functions,
My application built with Google App Engine has this page that displays search results
Okay, this problem is a bit odd. I have an application built in VS2010

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.