Question

Consider the following three tables where keys are underlined: Graphs(graphid, graphname), Nodes(nodeid, nodename, nodeweight, graphid), Edges(parentid,...

Consider the following three tables where keys are underlined: Graphs(graphid, graphname), Nodes(nodeid, nodename, nodeweight, graphid), Edges(parentid, childid, edgeweight, graphid), formulate a SQL statement for each query in the following.

3. Suppose there is a node X in graph with graphid = 1234 such that all other nodes are the parents of node X. Return the nodeid of node X.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The basic logic behind the following SQL query is to first check for all nodes of graphid = 1234 whether they satisfy the condition that no. of times they appear in childid field = no. of nodes in the graph (graphid = 1234) - 1(node itself)

The node which has all other nodes as his parents should appear (n-1) times in edges table where n is the no. of nodes in the given graph.

SQL statement:

SELECT nodeid

FROM Nodes

WHERE graphid = 1234

AND

(

SELECT count(childid)+1

FROM Edges

WHERE childid=nodeid

AND

graphid = 1234

GROUP BY childid

)

=

(

SELECT count(nodeid)

FROM Nodes

WHERE graphid = 1234

GROUP BY graphid

) ;

For implementation, I made simple tables inside MySQL 5.5

mysql> select * from Graphs; graphid | graphname | + 1234 | Graphi T + 1 row in set (0.00 sec) mysql> select from Nodes; node

Then I ran the given query:

mysql> select nodeid -> from nodes -> where graphid -> and = 1234 -> select count(childid) + 1 -> from edges -> where childid

Add a comment
Know the answer?
Add Answer to:
Consider the following three tables where keys are underlined: Graphs(graphid, graphname), Nodes(nodeid, nodename, nodeweight, graphid), Edges(parentid,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT