Our Daily Bread

what I need to do ...

  • Continious learning -unlearn as necessary
  • Reach out to family & loves ones
  • Maintain fitness - strong core and clear IPPT
  • Pick up parenting skills
  • Save up and plan for renovation
  • Build passive business & investment income
  • Back up Data
  • Manage $ Cash flow
  • Learn sinan mori

Sunday, November 4, 2007

Solving 'All' like problems in SQL (DB)


The typical problem
:
Display the .... of all... that have ....
Display the ..yR1.. of all...R2.. that have ....

Using Relation algebra the R1 / R2 = ProjY(R1) - ProjY { ProjY(R1) x R2 - R1 )

R1(a,b)
R2(b)

Display the name a which consist of all b that have ....

ProjY(R1) - ProjY { ProjY(R1) x R2 - R1 )
........................^----------------^ ----------------Max possible result (a x b)
........................^----------------------^ ----------Result that do not qualify
..............^---------------------------------^ ---------Extract a that do not qualify
^-----------------------------------------------^ ---------Extract a that qualify

[1]Using SQL an example:

Supplier(S#,SName,City)
Part(P#,PName,Color,Weight,City)
Supplies(S#,P#,Qty)

Get supplier names for suppliers who supply ALL parts


SELECT DISTINCT S.SNAME
FROM S
WHERE NOT EXSITS
( SELECT *
FROM P -------------------->ALL Parts
WHERE NOT EXISTS
( SELECT *
FROM SP
WHERE SP.S# = S.S#
AND SP.P# = P.P# ---------------->Existing parts) ) ------>Unqualified parts

[2]Using SQL an example:

Find all Borrowers who have an member account at all Library branches located in
Singapore.

account (account_number, branch_name, numbooks)
branch (branch_name, branch_city, assets)
Lib_Member (borrower_name, account_number)

select distinct S.borrower_name
from Lib_Member as S
where not exists (
(select branch_name
from branch
where branch_city = ‘Singapore’--------------> All possible branch)
except
----------------------------->Subtracted branch quality
(select R.branch_name--------------->Branch that may or may not qualify
from Lib_Member as T, account as R
where T.account_number = R.account_number and
S.borrower_name = T.borrower_name ))


Other unrelated SQL an example:

Given:

teaching(prof_id, crs_code, term)
professor(prof_id, name, dept_id)

Display the names of all professors that have taught both 291 and 391 but have

never taught 229.


Solution:
SELECT name
FROM professor p,
((SELECT prof_id as pid
FROM teaching
WHERE crs_code = '291'
INTERSECT
SELECT prof_id as pid
FROM teaching
WHERE crs_code = '391'
)
EXCEPT
SELECT prof_id as pid
FROM teaching
WHERE crs_code = '229'
) as t
WHERE p.prof_id = t.pid



No comments:

Quotes

  • You are your thoughts.The thoughts in your head are what institute the laws of attraction. You think therefore you are. - Joe Vitalli, The Laws of Attraction
  • Don't react blatantly in Anger and become a Zero - Papati
  • Don't miss out in the learning values of problems by over-looking the root causes but starting at the occurences - Raj
  • You can do almost anything if you have a steady income. Little or much, what matters is that you can count it, month after month.Without the regular flow of funds, you will be constantly distracted from you goals - Norm & Bo
  • Time is greater than money, you can never really buy time. Don't let time slip away. - Raj
  • Ignore technology advancement and you will either be left behind or you have to fork out more - Raj
  • Without passion nothing happens in life but without compassion the wrong things happen - Jan Eliasson
  • The poorer you are the more you need to plan and act wisely. Any undesired outcome you have little resource to manoeuvre.
  • “Life changes when you least expect it to. The future is uncertain. So, seize this day, seize this moment, and make the most of it.”
  • To get to where you want to go in life you must start from where you are - Tan
  • Maturity does not come with age,it comes with the acceptance of responsibilities - Tan

ACM TechNews