Question

I'm pretty new to SQL... I have a table with the following columns :

Employee,Title,Age,Children

Output of a basic SELECT would be :

Steve |Foreman|40|Billy
Steve |Foreman|40|Amy
Steve |Foreman|40|Michelle
Daniel|Smith  |35|Eric
Daniel|Smith  |35|Jake
Erin  |Otis   |29|Eileen

Hopefully, I've shown that each record can contain multiple children. What I'd like to be able to do is to only return values if the Employee doesn't have a child who's name starts with 'E'. Right now I'm still returning Employees but it only lists the records that don't have a child starting with 'E'. I want to completely omit the Employee if any of their children start with an 'E' not just omit the child starting with 'E'.

Is this possible?

Thanks.

EDIT :

So in actuality there are two tables, one for EMPLOYEES and one for CHILDREN. So my current query looks like this :

SELECT E.EMPLOYEE_NAME, E.EMPLOYEE_TITLE, E.EMPLOYEE_AGE, C.CHILDREN_NAME
  FROM EMPLOYEE E INNER JOIN CHILDREN C ON E.EMPLOYEE_ID = C.EMPLOYEE_ID
 WHERE C.CHILDREN_NAME NOT LIKE 'E%'

This returns all rows minus any children that have a name starting with E. The desired effect, is solution 2 that Trinimon provided; do not return an employee if any of their children have a name that start with E.

I'm hoping that explains it a bit more and someone can explain how to produce the desired results. As mentioned, Trinimon's solution returns the proper results but since there are two tables I'm not how to adjust the solution to my schema.

Thanks.

Was it helpful?

Solution

Either go for ...

SELECT * 
  FROM employees
 WHERE Children NOT LIKE 'E%';

if you want all records where the child's name doesn't start with E or for ...

SELECT * 
  FROM employees e1
 WHERE NOT EXISTS (SELECT 1 
                     FROM employees e2
                    WHERE e1.Employee = e2.Employee
                      AND Children LIKE 'E%');

if none of the returned employees should have a child that starts with E.

Check out version one and version two.

p.s. based on your structure it's

SELECT E.EMPLOYEE_NAME, 
       E.EMPLOYEE_TITLE,
       E.EMPLOYEE_AGE, 
       C.CHILDREN_NAME
  FROM EMPLOYEE E INNER JOIN CHILDREN C ON E.EMPLOYEE_ID = C.EMPLOYEE_ID
 WHERE NOT EXISTS (SELECT 1 
                     FROM CHILDREN C2
                    WHERE E.EMPLOYEE_ID = C2.EMPLOYEE_ID
                      AND C2.CHILDREN_NAME LIKE 'E%');

Check this Fiddle.

OTHER TIPS

You could try:

select * from YourTable T
where NOT EXISTS (select 1 from YourTable where Employee=T.Employee and Children like 'E%')

This, of course, will have a problem if your have two employees with the same name. You could expand the WHERE clause to cover all the attributes that make an employee the same:

select * from YourTable T
where NOT EXISTS (select 1 from YourTable where Employee=T.Employee and Title=T.Title and Age=T.Age and Children like 'E%')

However, you should consider making Children a separate table. Have a single Employee (with a unique EmployeeID) in your table, and have Children contain each child with a reference to EmployeeID.

Here is a mysql solution in sql fiddle. I second, that you normalize your data and make a child table.

http://sqlfiddle.com/#!2/f52cc/8/0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top