Shobhit Singh
Shobhit Singh

Reputation: 11

MySQL Group by query for specific condition

If I want to know the total amount of salary on each customer, then GROUP BY query would be as follows:

SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP BY NAME;

But, if I don't want sum of salaries and just need to look all salaries grouped by name then what will be query or approach?

Upvotes: 1

Views: 150

Answers (3)

Jopie
Jopie

Reputation: 332

If you want to retrieve name and variation of salary for each customers (grouped by name and salary). You can try this.

SELECT name, salary FROM customers GROUP BY name, salary;

Upvotes: 0

Muhammad Arif
Muhammad Arif

Reputation: 1022

Try to use GROUP BY clause for getting desired results.

SELECT NAME,SALARY FROM CUSTOMERS GROUP BY NAME;

Upvotes: 0

sagi
sagi

Reputation: 40491

That depends on which salary you want to show..

Option 1 -> maximum:

SELECT NAME, max(SALARY) FROM CUSTOMERS GROUP BY NAME;

Option 2 -> minimum:

SELECT NAME, min(SALARY) FROM CUSTOMERS GROUP BY NAME;

Option 3 -> list of them:

SELECT NAME, group_concat(SALARY separator ',') FROM CUSTOMERS GROUP BY NAME;

And last option -> doesn't matter :

SELECT NAME,SALARY FROM CUSTOMERS GROUP BY NAME;

Upvotes: 1

Related Questions