/* Given the following database schema: */
CREATE TABLE langs(lid    INTEGER PRIMARY KEY AUTOINCREMENT,
                            lang   TEXT UNIQUE,
                            locale TEXT UNIQUE,
                            pages  INTEGER DEFAULT 0,  -- total pages in this language
                            words  INTEGER DEFAULT 0)

CREATE TABLE sqlite_sequence(name,seq)

CREATE TABLE pages(pid INTEGER PRIMARY KEY AUTOINCREMENT,
                            lid INTEGER REFERENCES langs(lid) ON UPDATE CASCADE ON DELETE CASCADE,
                            page INTEGER DEFAULT NULL, -- wikipedia page id
                            revision INTEGER DEFAULT NULL, -- wikipedia revision page id
                            title TEXT,
                            words INTEGER DEFAULT 0, -- number of different words in this page
                            UNIQUE(lid,page,title))

CREATE TABLE words(wid INTEGER PRIMARY KEY AUTOINCREMENT,
                            word        TEXT UNIQUE,
                            occurrences INTEGER DEFAULT 0)

CREATE TABLE langs_words(lid INTEGER REFERENCES langs(lid) ON UPDATE CASCADE ON DELETE CASCADE,
                                        wid INTEGER REFERENCES words(wid) ON UPDATE CASCADE ON DELETE CASCADE,
                                        occurrences INTEGER, -- repetitions of this word in this language
                                        PRIMARY KEY(lid,wid))
                                        WITHOUT ROWID

CREATE TABLE pages_words(pid INTEGER REFERENCES pages(pid) ON UPDATE CASCADE ON DELETE CASCADE,
                                        wid INTEGER REFERENCES words(wid) ON UPDATE CASCADE ON DELETE CASCADE,
                                        occurrences INTEGER DEFAULT 0, -- times this word appears into this page
                                        PRIMARY KEY(pid,wid))
                                        WITHOUT ROWID

CREATE TABLE biwords(lid    INTEGER REFERENCES langs(lid) ON UPDATE CASCADE ON DELETE CASCADE,
                                w1st   INTEGER REFERENCES words(wid) ON UPDATE CASCADE ON DELETE CASCADE,
                                w2nd   INTEGER REFERENCES words(wid) ON UPDATE CASCADE ON DELETE CASCADE,
                                occurrences INTEGER DEFAULT 0, -- times this pair appears in this language/page
                                PRIMARY KEY(lid,w1st,w2nd))
                                WITHOUT ROWID

/* Answer the following: How many times greater is the appearances of the biword pair "a base" than "a decimal"? a, base AND decimal are words; wid is the ID of word; w1st is the first word of a biword pair; w2nd is the second word of a biword pair; appearances refers to biwords.occurrences; biword pair 'a base' refers to word = 'a' as w1st AND word = 'base' as w2nd; biword pair 'a decimal' refers to word = 'a' as w1st AND word = 'decimal' as w2nd; appearances of 'a base' greater than 'a decimal' refers to DIVIDE(SUBTRACT(biwords.occurrences'a base', biwords.occurrences'a decimal'), biwords.occurrences'a decimal') */
SELECT 
SELECT CAST(occurrences AS REAL) / ( SELECT occurrences FROM biwords WHERE w1st = ( SELECT wid FROM words WHERE word = 'a' ) AND w2nd = ( SELECT wid FROM words WHERE word = 'decimal' ) ) FROM biwords WHERE w1st = ( SELECT wid FROM words WHERE word = 'a' ) AND w2nd = ( SELECT wid FROM words WHERE word = 'base' )