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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:40:15+00:00 2026-05-25T03:40:15+00:00

I have a database with two tables: Users , Items Users looks like this:

  • 0

I have a database with two tables: Users, Items

Users looks like this:

userId     uniqueindentifier
username   nvarchar

Items looks like this:

userId     uniqueidentifier
itemName   nvarchar

So each user can have several items.

I need to do a query that returns a user. I could do something like this:

SELECT * FROM Users JOIN Items on Users.UserId = Items.UserId
WHERE UserId = 1

If user 1 has 2 items, the query will return something like this:

UserId    userName    ItemName
1         Oliver      Apple
1         Oliver      Orange

I want the query to return something like this:

UserId    userName    ItemNames
1         Oliver      Apple, Orange

Or even better, something like this:

UserId    userName    ItemName1    ItemName2
1         Oliver      Apple        Orange

Is it possible to achieve this? The reason I want to do it this way is because if I do a query with SELECT TOP 50, I want to return 50 distinct users, not (for example) 18 users in 50 rows, with several items per user.

  • 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-25T03:40:16+00:00Added an answer on May 25, 2026 at 3:40 am

    You could use PIVOT and ROW_NUMBER to pivot on if there was a known max number of columns. Or XML PATH to simulate GROUP_CONCAT for the delimited list. Both of these are covered in other SO questions.

    You don’t need to do this though to achieve the result stated in your last sentence. You can just do

    ;WITH TopUsers As
    (
    SELECT TOP (50) *
    FROM Users
    ORDER BY UserId
    )
    
    SELECT * 
    FROM TopUsers 
        JOIN Items on TopUsers.UserId = Items.UserId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with two tables, Users and Posts. I'd like to write
I have a simple database with two tables. Users and Configurations. A user has
I have a database where users are presented with two items and choose the
I have a database with two tables. One of the tables contains users, the
I have two database tables, users and logs. I need SQL code to do
I have a Database with the following two tables, USERS, POSTS I am looking
I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields
I have a database two tables and a linking table that I need a
I have a database with two tables ( Table1 and Table2 ). They both
I have a database with two tables - let's call them Foo and Bar.

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.