/* Given the following database schema: */
CREATE TABLE authors
(
    au_id    TEXT
        primary key,
    au_lname TEXT not null,
    au_fname TEXT not null,
    phone    TEXT    not null,
    address  TEXT,
    city     TEXT,
    state    TEXT,
    zip      TEXT,
    contract TEXT     not null
)

CREATE TABLE jobs
(
    job_id   INTEGER
        primary key,
    job_desc TEXT not null,
    min_lvl  INTEGER     not null,
    max_lvl  INTEGER     not null
)

CREATE TABLE publishers
(
    pub_id   TEXT
        primary key,
    pub_name TEXT,
    city     TEXT,
    state    TEXT,
    country  TEXT
)

CREATE TABLE employee
(
    emp_id    TEXT
        primary key,
    fname     TEXT not null,
    minit     TEXT,
    lname     TEXT not null,
    job_id    INTEGER     not null,
    job_lvl   INTEGER,
    pub_id    TEXT     not null,
    hire_date DATETIME    not null,
    foreign key (job_id) references jobs(job_id)
            on update cascade on delete cascade,
    foreign key (pub_id) references publishers(pub_id)
            on update cascade on delete cascade
)

CREATE TABLE pub_info
(
    pub_id  TEXT
        primary key,
    logo    BLOB,
    pr_info TEXT,
    foreign key (pub_id) references publishers(pub_id)
            on update cascade on delete cascade
)

CREATE TABLE stores
(
    stor_id     TEXT
        primary key,
    stor_name    TEXT,
    stor_address TEXT,
    city         TEXT,
    state        TEXT,
    zip          TEXT
)

CREATE TABLE discounts
(
    discounttype TEXT   not null,
    stor_id      TEXT,
    lowqty       INTEGER,
    highqty      INTEGER,
    discount     REAL not null,
    foreign key (stor_id)  references stores(stor_id)
            on update cascade on delete cascade
)

CREATE TABLE titles
(
    title_id  TEXT
        primary key,
    title     TEXT not null,
    type      TEXT    not null,
    pub_id    TEXT,
    price     REAL,
    advance   REAL,
    royalty   INTEGER,
    ytd_sales INTEGER,
    notes     TEXT,
    pubdate   DATETIME    not null,
    foreign key (pub_id) references publishers(pub_id)
            on update cascade on delete cascade
)

CREATE TABLE roysched
(
    title_id TEXT not null,
    lorange  INTEGER,
    hirange  INTEGER,
    royalty  INTEGER,
    foreign key (title_id)  references titles(title_id)
            on update cascade on delete cascade
)

CREATE TABLE sales
(
    stor_id  TEXT     not null,
    ord_num  TEXT  not null,
    ord_date DATETIME    not null,
    qty      INTEGER     not null,
    payterms TEXT  not null,
    title_id TEXT   not null,
    primary key (stor_id, ord_num, title_id),
    foreign key (stor_id)   references stores(stor_id)
            on update cascade on delete cascade,
    foreign key (title_id)  references titles(title_id)
            on update cascade on delete cascade

)

CREATE TABLE titleauthor
(
    au_id      TEXT not null,
    title_id   TEXT  not null,
    au_ord     INTEGER,
    royaltyper INTEGER,
    primary key (au_id, title_id),
    foreign key (au_id) references authors(au_id)
            on update cascade on delete cascade,
    foreign key (title_id)    references titles (title_id)
            on update cascade on delete cascade
)

/* Answer the following: For all authors from CA who are not on contract, which title of his/hers has the most year to date sales. year to date sales refers to ytd_sales; on contract refers to contract = 1 */
SELECT 
SELECT T1.title FROM titles AS T1 INNER JOIN titleauthor AS T2 ON T1.title_id = T2.title_id INNER JOIN authors AS T3 ON T2.au_id = T3.au_id WHERE T3.contract = 0 AND T3.state = 'CA' ORDER BY T1.ytd_sales DESC LIMIT 1