search
Log In
1 vote
447 views
Consider two $n \times 1$ vectors $u$ and $v$ , stored as table $U(\text{ind,val})$ and $V(\text{ind,val})$ with the same schema A row $(i,u_i)$ of table $U$ specifies the $i^{th}$ element of vector $u$ has value $u_i$  (similarly for $v$, respectively). Only the non-zero entries of the vectors are stored in the corresponding tables. For example, if the vector $u$ equals $(0, 1, 3, 0, 2, 0)$, then it is represented in table $U$ as $$\begin{array}{|l|l|} \hline \textbf{ind}&\textbf{val} \\\hline 2&1 \\3&3 \\5&2\\ \hline \end{array}$$

Write a relational algebra expression or an SQL query to compute the sum $u + v$ of the two vectors $u$ and $v$. Explain your solution.
in Databases
edited by
447 views

3 Answers

2 votes

SINCE THE QUESTION ASKED FOR RELATIONAL ALGEBRA OR SQL EXPRESSION, I AM PROVIDING THE RELATIONAL ALGEBRA EXPRESSION BECAUSE IN ISI MTECH CS EXAM SYLLABUS RELATIONAL ALGEBRA IS THERE.

THE SOLUTION IS AS FOLLOWS:-

SINCE IN QUESTION IT IS MENTIONED THAT VECTOR IS ALREADY STORED IN THE FORM OF TABLE WITH NON ZERO ENTRIES, SO WE WILL NOT WRITING THE EXPRESSION FOR CREATING AND STORING THE TABLE. (IF YOU ARE WRITING SQL QUERY ALSO THEN THERE IS NO NEED OF USING THE CREATE AND INSERT COMMAND). NOW THE SOLUTION IS AS FOLLOWS:- 

1) FIRST WE TAKE THE UNION OF TWO RELATIONS U AND V.

2)THEN,WE USE AGGREGATION FUNCTION SUM(val) USING GROUP BY ind.

NOW THE SOLUTION IS ATTACHED BELOW

NOW HOW I WRITE THE SECOND EXPRESSION, BELOW I AM GIVING YOU THE DBMS KORTH BOOK IMAGE WHICH USES THAT SIMILAR KIND OF  EXPRESSION

YOU SHOULD ALSO REFER KORTH BOOK FOR THE SECOND EXPRESSION.

 

 

0 votes

Select ind,sum(val) from u,v where u.ind=v.ind Group by ind;

0
Your answer is Wrong as it prints the sum of all the values of the column and not val1+val2 of two different columns and stores the result of it.
0 votes
SELECT ind, sum(val)

FROM

(SELECT *

FROM U

UNION ALL

SELECT *

FROM V

)

GROUP BY ind
ago

Related questions

1 vote
0 answers
1
345 views
The data link layer uses a fixed-size sliding window protocol, where the window size for the connection is equal to twice the bandwidth-delay product of the network path. Consider the following three scenarios, in each of which only the given parameter changes as specified (no other ... the minimum value of the round trip time $R$ increases to $1.8R$; the window size $W$ decreases to $W/3$
asked May 12, 2019 in Computer Networks akash.dinkar12 345 views
1 vote
2 answers
2
544 views
Consider a $5$ ... $\text{(in ns)}$ needed to execute the program.
asked May 12, 2019 in Operating System akash.dinkar12 544 views
1 vote
2 answers
3
518 views
A context switch from a process $P_{old}$ to a process $P_{new}$ consists of the following steps: Step I:saving the context of $P_{old}$; Step II: running the scheduling algorithm to pick $P_{new}$; Step III: restoring the saved context of $P_{new}$. Suppose Steps I and ... same instant in the order $P_1, P_2, . . . , P_k;$ each process requires exactly one CPU burst of $20$ms and no I/O burst.
asked May 12, 2019 in Operating System akash.dinkar12 518 views
0 votes
1 answer
4
261 views
The following function computes an array $SPF$, where, for any integer $1 < i < 1000$, $SPF[i]$ is the smallest prime factor of $i$. For example, $SPF[6]$ is $2$, and $SPF[11]$ is $11$. There are five missing parts in the following code, commented as $/* Blank */$. For each of them, copy the entire ... ; j < 1000; j+= i) { /* Blank 4 */ if (SPF[j] == j) { SPF[j] = _____; /* Blank 5 */ } } } } }
asked May 12, 2019 in Algorithms akash.dinkar12 261 views
...