L
L
lanipov7952020-09-26 16:45:11
MySQL
lanipov795, 2020-09-26 16:45:11

SQL. How to find out which ref_id how many times are repeated?

Good afternoon, there is a Users table

. There are fields in it: id and ref_id
id - a unique number of each user
ref_id - referrer id (may be NULL if no one invited the user)

The task is this:
You need to make a query that will display the top users who invited the most people (display id and number of referrals)

referrer - a person who invites users to the system
referral - a person who was invited

Thank you in advance for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-09-26
@lanipov795

Like this?

SELECT id as refer, count(ref_id) as refers_count
FROM users
WHERE
ref_id is not null
GROUP BY ref_id
ORDER BY 2 DESC LIMIT 5;

Z
zexer, 2020-09-26
@zexer

Select ID, count(refer ID)
From table
Group buy ID

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question