edited by
1,067 views
1 votes
1 votes
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.
edited by

3 Answers

2 votes
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.

 

 

1 votes
1 votes
SELECT ind, sum(val)

FROM

(SELECT *

FROM U

UNION ALL

SELECT *

FROM V

)

GROUP BY ind
0 votes
0 votes

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

Related questions

1 votes
1 votes
2 answers
2
akash.dinkar12 asked May 12, 2019
1,270 views
Consider a $5$-stage instruction pipeline. The stages and the corresponding stage delays are given below.$$\begin{array}{|l|l|}\hline \textbf{Instruction}&\textbf{Stage d...