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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:45:29+00:00 2026-05-21T07:45:29+00:00

I have database with two tables: Customers (Id PK, LastName) and Orders (Id PK,

  • 0

I have database with two tables:
Customers (Id PK, LastName)
and
Orders (Id PK, CustomerId FK, ProductName, Price, etc.)

I want to retrieve only customer’ last orders details together with customer name.
I use .NET L2SQL but I think it’s SQL question more than LINQ question so I post here SQL query I tried:

SELECT [t0].[LastName], (
    SELECT [t2].[ProductName]
    FROM (
        SELECT TOP (1) [t1].[ProductName]
        FROM [Orders] AS [t1]
        WHERE [t1].[CustomerId] = [t0].[Id]
        ORDER BY [t1].[Id] DESC
        ) AS [t2]
    ) AS [ProductName], (
    SELECT [t4].[Price]
    FROM (
        SELECT TOP (1) [t3].[Price]
        FROM [Orders] AS [t3]
        WHERE [t3].[CustomerId] = [t0].[Id]
        ORDER BY [t3].[Id] DESC
        ) AS [t4]
    ) AS [Price]
FROM [Customers] AS [t0]

Problem is that Orders has more columns (30) and with each column the query gets bigger and slower because I need to add next subqueries.

Is there any better way?

  • 1 1 Answer
  • 3 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-21T07:45:30+00:00Added an answer on May 21, 2026 at 7:45 am

    In SQL Server 2005 and above:

    SELECT  *
    FROM    (
            SELECT  o.*,
                    ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY o.id DESC) rn
            FROM    customers c
            LEFT JOIN
                    orders o
            ON      o.customerId = c.id
            ) q
    WHERE   rn = 1
    

    or this:

    SELECT  *
    FROM    customers c
    OUTER APPLY
            (
            SELECT  TOP 1 *
            FROM    orders o
            WHERE   o.customerId = c.id
            ORDER BY
                    o.id DESC
            ) o
    

    In SQL Server 2000:

    SELECT  *
    FROM    customers с
    LEFT JOIN
            orders o
    ON      o.id = 
            (
            SELECT  TOP 1 id
            FROM    orders oi
            WHERE   oi.customerId = c.id
            ORDER BY
                    oi.id DESC
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database that look like that: Customer: C_ID city
I have a SQL Server 2008 database. This database has two related tables: Customer
I have two tables in a 2010 Access Database. One for Customers and One
I have a database with these two tables: Customer and CustomerStatus . CustomerStatus is
I have two tables - let's call them Customers and Orders. It's a 1:n
In my MySQL database I have two tables: 'orders' and 'orders_lines'. Orders contains info
I have a database two tables and a linking table that I need a
I have a database with two tables - let's call them Foo and Bar.
I have a database with two tables, tblPlayers and tblMatches. tblPlayers contains the following
I have a database with two tables: data and file . file_id is a

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.