Skip to main content
added 49 characters in body
Source Link
Akina
  • 20.3k
  • 2
  • 19
  • 22

ERD for my simple db design: ERD

I want to order children based on their parent's last name. I am trying to use the following query:

SELECT *  
FROM children  
WHERE parent_id IN (SELECT parent_id  
                    FROM parent 
                    ORDER BY lastName ASC);

SELECT parent_id FROM parent ORDER BY lastName ASC returns the parent_ids in the order of the parent's last names (this is the intended behavior). The ordering of parent_ids according to their last names in ascending order is 154,156,155,157

However, when used as a subquery, the ordering is apparently not used correctly. The results are: query results

As you can see the results are ordered in ascending order of parent_id (not of the child's respective parent's last name).

Is there a way to correctly structure this query without changing my design?

Thank you.

ERD for my simple db design: ERD

I want to order children based on their parent's last name. I am trying to use the following query:

SELECT * FROM children WHERE parent_id IN (SELECT parent_id FROM parent ORDER BY lastName ASC);

SELECT parent_id FROM parent ORDER BY lastName ASC returns the parent_ids in the order of the parent's last names (this is the intended behavior). The ordering of parent_ids according to their last names in ascending order is 154,156,155,157

However, when used as a subquery, the ordering is apparently not used correctly. The results are: query results

As you can see the results are ordered in ascending order of parent_id (not of the child's respective parent's last name).

Is there a way to correctly structure this query without changing my design?

Thank you.

ERD for my simple db design: ERD

I want to order children based on their parent's last name. I am trying to use the following query:

SELECT *  
FROM children  
WHERE parent_id IN (SELECT parent_id  
                    FROM parent 
                    ORDER BY lastName ASC);

SELECT parent_id FROM parent ORDER BY lastName ASC returns the parent_ids in the order of the parent's last names (this is the intended behavior). The ordering of parent_ids according to their last names in ascending order is 154,156,155,157

However, when used as a subquery, the ordering is apparently not used correctly. The results are: query results

As you can see the results are ordered in ascending order of parent_id (not of the child's respective parent's last name).

Is there a way to correctly structure this query without changing my design?

Thank you.

Source Link

What is the correct usage of IN and ORDER BY keywords when used in conjunction with subqueries?

ERD for my simple db design: ERD

I want to order children based on their parent's last name. I am trying to use the following query:

SELECT * FROM children WHERE parent_id IN (SELECT parent_id FROM parent ORDER BY lastName ASC);

SELECT parent_id FROM parent ORDER BY lastName ASC returns the parent_ids in the order of the parent's last names (this is the intended behavior). The ordering of parent_ids according to their last names in ascending order is 154,156,155,157

However, when used as a subquery, the ordering is apparently not used correctly. The results are: query results

As you can see the results are ordered in ascending order of parent_id (not of the child's respective parent's last name).

Is there a way to correctly structure this query without changing my design?

Thank you.