---
title: "MySQL Query suggestion"  
description: "MySQL Query suggestion"  
author: "Anonymous User"  
published: 2013-05-02  
updated: 2013-05-02  
canonical: https://www.mindstick.com/forum/789/mysql-query-suggestion  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 1 minute  

---

# MySQL Query suggestion

Hi All!\
I have a [MySQL](https://www.mindstick.com/articles/12156/what-is-mysql) [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) like this -\
ID [NAME](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) [PARENT](https://www.mindstick.com/blog/154/how-to-find-parent-of-control-in-wpf)== ======== ===========1 [Class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) 02 Math 13 Physics 14 [Cooking](https://answers.mindstick.com/qa/49056/when-and-who-invented-the-electric-cooking-utensil) 05 Italian 46 [Chinese](https://www.mindstick.com/blog/301815/chinese-scientists-clone-super-cows) 4\
I want a [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) that will give me this [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) -\
ID NAME== ===================1 Class2 Math Class3 Physics Class4 Cooking5 Italian Cooking6 Chinese Cooking\
The parent's name will be appended with the child's name, like surname.\
Please help me! Any [succession](https://answers.mindstick.com/qa/107655/what-are-the-causes-of-the-war-of-spanish-succession) will be appreciated!

## Replies

### Reply by AVADHESH PATEL

Hi Tanuj!\
\
You can achieve above description as below using LEFT JOIN \
\
LEFT JOIN is needed on this case since there are PARENT that has no match on the ID.\
\
SELECT a.ID,\
CONCAT(a.Name, ' ', COALESCE(b.name,'')) Name\
FROM TableName a\
LEFT JOIN TableName b\
ON a.Parent = b.ID\
SQLFiddle Demo\
you can also use CONCAT_WS() so that you can omit COALESCE()\
\
SELECT a.ID,\
CONCAT_WS(' ', a.Name, b.name) Name\
FROM TableName a\
LEFT JOIN TableName b\
ON a.Parent = b.ID


---

Original Source: https://www.mindstick.com/forum/789/mysql-query-suggestion

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
