user1773626
user1773626

Reputation: 13

Need your help in SQL query

There are two tables:

Write a query that selects the identifier and name of the first table and the number of records in the second table, associated with them. The result should be sorted by surname in descending order.

I've tried

SELECT 
   Clients.id, Clients.name, count(id) 
FROM clients 
INNER JOIN Order ON Clients.id = Order.id_client 
GROUP BY 
   Clients.id, Clients.name 
ORDER BY 
   Clients.name DESC

But it doesn't work. What is wrong?

Upvotes: 0

Views: 68

Answers (4)

JonH
JonH

Reputation: 33143

SELECT
 c.ID,
 c.Name,
 COUNT(o.ID)
FROM
 Clients c
LEFT JOIN [Order] o
ON
 o.id_client = c.id
GROUP BY
 c.ID,
 c.Name
ORDER BY
 c.Name DESC

Upvotes: 3

Sandeep Kumar Vuppala
Sandeep Kumar Vuppala

Reputation: 19

SELECT
 c.ID,
 c.Name,
 COUNT(o.ID)
FROM
 Clients c,
 Order o
WHERE o.id_client = c.id
GROUP BY
 c.ID
 c.Name

Upvotes: 0

Muthu Kumaran
Muthu Kumaran

Reputation: 17910

Change count(id) to

count(Clients.id) or count(Order.id)

I don't know which table you need count(id) from. I hope you understand where the issue is.

Upvotes: 0

SRIRAM
SRIRAM

Reputation: 1888

SELECT Clients.id, Clients.name, count(client.id) FROM clients INNER JOIN Order on Clients.id=Order.id_client GROUP BY Clients.id, Clients.name ORDER BY Clients.name DESC

Upvotes: 0

Related Questions