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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:12:15+00:00 2026-06-05T16:12:15+00:00

I have a customers table, and an orders table. Each customer can have many

  • 0

I have a customers table, and an orders table. Each customer can have many orders.

I want to select every customer, along with their earliest order number from the orders table (it is important that I select the earliest order number, not just any order). I want to list customers whether they have an order or not, and I don’t want to include customers twice if they have more than one order.

I’m using this:

  SELECT *
    FROM customers
    LEFT JOIN orders
    ON customers.id = orders.customer_id 
    GROUP BY customers.id

This gives me almost what I want, except it will pick whatever order ID it likes from the table. I need to be able to sort, and pick the smallest order ID.

Any ideas please?

Im pretty sure its something that’s staring me in the face…

EDIT: Tables’ Structures as requested

  • Customers:


    | ID | Name | Address            | Etc |
    ----------------------------------------
    | 1  | Joe  | 123 Fake Street    |     |
    | 2  | Mike | 1600 Fake Road     |     |
    | 3  | Bill | Red Square, Moscow |     |
    ----------------------------------------
    
  • Orders:


    | ID | Customer_ID | Date |
    ---------------------------
    | 1  | 1           |  ... |
    | 2  | 2           |  ... |
    | 3  | 2           |  ... |
    | 4  | 1           |  ... |
    ---------------------------
    
  • 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-05T16:12:17+00:00Added an answer on June 5, 2026 at 4:12 pm

    Create a virtual table (a/k/a subquery) with the lowest numerical order ID for each customer.

    SELECT customer_id, min(order_id)
      FROM orders
     GROUP BY customer_id
    

    Then join that table with the customer table, like so.

    SELECT C.customer_id, firstorder.order_id
      FROM CUSTOMERS as C
      LEFT JOIN (
        SELECT customer_id, min(order_id)
          FROM orders
         GROUP BY customer_id
      ) AS firstorder ON c.customer_id = firstorder.customer_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have 2 tables: Customers and Orders. A Customer can have many Orders.
Lets say I have a Customers table and an Orders table with a one-to-many
If I have a Customers table linked to an Orders table, and I want
I'm putting together a system to track customer orders. Each order will have three
I have a table called order which has many order_items , each order_items is
I have two tables - Customers, and Orders. Each Customer will have 1..n Orders.
I am designing an app that tracks orders. Each order can have > 1
If I have two tables, Customers and Orders, and I want to look up
I have a Customers table and would like to assign a Salesperson to each
I have couple of rows in database table (lets call it Customer), each row

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.