Question: I need to get number of record in child table.

Parent table 

```
+-------------+------------+  
| Id          | Name       |  
+-------------+------------+  
| 1           | a          |  
+-------------+------------+    
| 2           | b          |    
+-------------+------------+ 
| 3           | c          | 
+-------------+------------+ 

```

Child table: 

```
+-------------+------------+  
| Id          | Code       |  
+-------------+------------+
| 1           | d1         |  
+-------------+------------+ 
| 1           | d2         | 
+-------------+------------+    
| 1           | d3         |  
+-------------+------------+ 
| 1           | d1         | 
+-------------+------------+    
| 2           | d2         |  
+-------------+------------+ 
| 2           | d3         | 
+-------------+------------+ 
| 3           | d1         |  
+-------------+------------+ 
| 3           | d2         | 
+-------+-----+------------+   

```

Result should like:

```
+-------------+------------+------------+ 
| Id          | Name       | NumOfRec   |  
+-------------+------------+------------+ 
| 1           | a          |        4   |
+-------------+------------+------------+  
| 2           | b          |        2   |
+-------------+------------+------------+  
| 3           | c          |        2   |
+-------------+------------+ -----------+

```

I've tried with COUNT but then I get only number of records for one id but not for other ID's.

Answer: 
Use the GROUP BY clause to group the result by every distinct value of id. This should do it

To get the edited result, you've got to join the parent table.

```
SELECT 
    parent.id,
    parent.Name, 
    parent.other_columns_that_you_need,
    COUNT(id) as NumOfRec 
FROM 
    child 
INNER JOIN
    parent
ON
    parent.id = child.id
GROUP BY 
    parent.id

```

**Note**

In standard SQL you will have to group by all columns without an aggregate function. MySQL has got [extended the use of GROUP BY](https://dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html) and you can omit those columns with the same values. This is here valid, because all not aggregated columns are directly dependent of the parent id.
Use this code. Writing query with count will give you solutions 

```
select distinct a.id,count(b.id) from parent a, child b where a.id=b.id; 

```