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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:59:46+00:00 2026-06-03T23:59:46+00:00

I have two tables – Customers, and Orders. Each Customer will have 1..n Orders.

  • 0

I have two tables – Customers, and Orders. Each Customer will have 1..n Orders. They have a relation on a Foreign Key to the Customers table in the Orders table. So far, so DB 101.

I need a query that will return a single row per Customer, together with the date and Order ID of that customer’s latest order. ALL Customers have at least one order. I can easily do this with a function, but I’d prefer to do it in SQL.

  • 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-03T23:59:48+00:00Added an answer on June 3, 2026 at 11:59 pm

    In case of auto incremented ID-s, and no variance with dates, the solution is that simple:

    SELECT c.*, o.*
    FROM Customer c 
    JOIN (SELECT max(id) as order_id, customer_id 
          FROM Order GROUP BY curtomer_id) conn on c.id = conn.customer_id
    JOIN Order o on o.id = conn.order_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, orders and old_orders. Each table has some similar fields and
I have two tables CREATE TABLE table1 (id int primary key auto_increment,....); CREATE TABLE
I have two tables: Transactions and TransactionAgents. TransactionAgents has a foreign key to Transactions
I have two tables with a foreign key constraint how can I delete rows
I have two tables: A lookup table ( tabOne ): KEY | Group |
I have two tables named MEMBER - columns id(primary key), name, email & TOPICS
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have two tables. One table contains information about Assets, another Table about their
I have two tables. One is temp table and another is main table. In

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.