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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:54:24+00:00 2026-06-03T03:54:24+00:00

I have a query where I want to select all users who like a

  • 0

I have a query where I want to select all users who like a given set of artists. There also are some other WHERE criteria on country etc. Here’s what the schema looks like.

          users                     favourite_artists             artists

+----------+------------+    +-----------+------------+    +--------+--------+
|    id    |  country   |    |  user_id  |  artist_id |    |  id    |  name  |
+----------+------------+    +-----------+------------+    +--------+--------+
|     1    |     gb     |    |     1     |      6     |    |   1    |  Muse  |
|     2    |     gb     |    |     1     |      5     |    |   2    |  RATM  |
|     3    |     us     |    |     1     |      3     |    |   3    |  ABBA  | 
|     4    |     us     |    |     2     |      3     |    |   4    |   U2   |
+----------+------------+    +-----------+------------+    +--------+--------+

I want to order them by the number of those artists they like. I also want to include users who don’t like any of the artists but who match the WHERE criteria. The expected result set would look like.

+--------+---------------+----------------+
|   id   |    country    |   match_count  |
+--------+---------------+----------------+
|    6   |      gb       |       4        |
|    9   |      gb       |       4        |
|    2   |      gb       |       3        |
|    1   |      gb       |       2        |
|    5   |      gb       |       0        |
|    4   |      gb       |       0        |
+--------+---------------+----------------+

I’ve been trying to do it using a subquery to get the match_count and ordering by that but it’s performing pretty slowly so I thought there’d have to be a better way.

   SELECT users.id, users.country
   (SELECT COUNT(*) FROM favourite_artists 
    WHERE user_id = users.id AND artist_id IN (1,3,4,9)) AS match_count        
   FROM "users" 
   WHERE users.country = 'gb'
   ORDER BY match_count DESC;

I’m using Postgresql 9.0.7. Any thoughts?

  • 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-03T03:54:25+00:00Added an answer on June 3, 2026 at 3:54 am

    Your query is executing one subquery for every row in users. Such queries are called “correlated subqueries” and their performance, quite understandably, sucks.

    Instead you want a join:

    SELECT users.id, users.country, count(artist_id) as match_count
    FROM users
    LEFT JOIN favourite_artists ON user_id = users.id AND artist_id IN (1,3,4,9)
    WHERE users.country = 'gb'
    GROUP BY 1, 2
    ORDER BY 3 DESC;
    

    This query will get the joining rows far more efficiently, assuming you have an index on favourite_artists(user_id) – or better yet a multi-column index favourite_artists(user_id, artist_id).

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

Sidebar

Related Questions

I want to find the users(userid) from a permissions table who have all of
I have an SQL query that lists the uid of all users who have
I have a query with loads of columns. I want to select rows where
I have a select query. I want to replace that select query with another
I have the following query: SELECT carBrand, carYear, carModel FROM Cars; What I want
Hi i have a store procedure, where i do a select query. I want
I have two tables, users and events, and I want to select e.g. 6
How should the SQL SELECT query look like if I want to select definite
I have 2 tables, voter and user. I want to show all users with
I want to see all users which following me or another user. I have

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.