---
title: "Struggling with SQL Query"  
description: "Struggling with SQL Query"  
author: "adhyaa khare"  
published: 2023-05-21  
updated: 2023-07-27  
canonical: https://www.mindstick.com/forum/158429/struggling-with-sql-query  
category: "sqlite"  
tags: ["sql"]  
reading_time: 2 minutes  

---

# Struggling with SQL Query

Hello everyone,

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to solve an [SQL query](https://www.mindstick.com/forum/529/rename-table-name-and-column-name-using-sql-query) [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) but I am having difficulty. I am trying to write a query that returns a list of [employees](https://www.mindstick.com/articles/44463/business-to-business-vat-reclaiming-a-guide-for-your-employees) who have worked in at least one department for more than 8 years.

The [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) consists of [two tables](https://www.mindstick.com/forum/159646/how-to-join-two-tables-in-oracle-to-get-single-line-results):

[Employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) Table

`Name | Age | Department`

`John | 30 | Marketing`

`Sara | 32 | Finance`

`Alice | 25 | HR`

`Employee History Table`

`Name | Department | Years of Experience`

`John | Marketing | 5`

`John | HR | 1`

`Sara | Finance | 8`

`Alice | HR | 10`

I am trying to write a query to return the names of the employees and the [departments](https://yourviews.mindstick.com/story/1782/important-ministries-and-departments-in-india) they have worked in for more than 8 years, but I am having difficulty [coming up](https://yourviews.mindstick.com/view/80/finally-some-good-news-coming-up-for-rajeev-chowk-metro-station) with the correct syntax. Any help would be greatly appreciated!

Thank you.

## Replies

### Reply by Aryan Kumar

To solve the given problem, you can use a combination of JOIN and GROUP BY clauses along with a HAVING clause to filter the results. Here's an example SQL query that should return the desired results:

```php
SELECT EH.Name, EH.Department
FROM EmployeeHistoryTable EH
JOIN EmployeeTable E ON EH.Name = E.Name
WHERE EH.YearsOfExperience > 8
GROUP BY EH.Name, EH.Department;
```

This query performs an inner join between the `EmployeeHistoryTable` and `EmployeeTable` on the `Name` column to retrieve the relevant information. The `WHERE` clause filters out the records where the years of experience are greater than 8. Then, the `GROUP BY` clause is used to group the results by employee name and department. Finally, the `SELECT` statement selects the employee name and department from the joined tables.

Please note that this query assumes that the employee's name is unique in the `EmployeeTable` and `EmployeeHistoryTable`. If there can be multiple entries for the same employee in the `EmployeeHistoryTable`, you may need to modify the query accordingly to consider the maximum years of experience for each employee.

### Reply by ICSM Computer

You should add **ID** column in all [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development).

Add [Inner Join](https://www.mindstick.com/blog/145/inner-join-in-sql-server) to merge all tables data in the new table.

```xml
SELECT E.Name, E.Age, E.Department, HE.[Years of Experience]
FROM Employee E
INNER JOIN Employee_History HE
ON HE.Name = E.Name
WHERE HE.[Years of Experience] >8
```


---

Original Source: https://www.mindstick.com/forum/158429/struggling-with-sql-query

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
