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.
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
Then I ran the given query:
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. 2. Assume each node has a different weight, return the nodeids of the top three nodes that have the highest weight for the graph with graphid = 1234.
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. 9. Return all graphids of those graphs such that every entry node is also a fork node.
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. 5. Return all graphids of those graphs in which every node is the child of all other nodes.
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. 1. Return the graphids of those graphs in which every exit node has a different weight.
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. 7. Return all graphids of those graphs such that the graph has at least 1 node that is not connected to any other nodes (i.e. neither as a parent nor as a child).
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. 6. Return all graphids of those graphs that have some fork nodes but no join nodes.
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. 8. Return all graphids of those graphs such that every edge has a different weight.