-- ORACLE SQL DML: SCHEMA C##ART
SET ECHO ON;
SET SERVEROUTPUT ON;
SET PAGESIZE 30;
SET HEADING ON;

-- 20 Queries
-- Q1: Select all columns and all rows from one table
SELECT * FROM ROLE;

-- Q2: Select five columns and all rows from one table
SELECT CONTACT_ID, FIRST_NAME, LAST_NAME, PHONE_NO, EMAIL FROM CONTACT;

-- Q3: Select all columns from all rows from one view
SELECT * FROM VIEW_AVAILABLE_INVENTORY;

-- Q4: Using a join on 2 tables, select all columns and all rows from the tables without the use of a Cartesian product
SELECT * FROM CONTACT ct
JOIN ADDRESS ad ON ad.FK_CONTACT_ID=ct.CONTACT_ID;

-- Q5: 5: Select and order data retrieved from one table
SELECT * FROM ZIP ORDER BY 1;

-- Q6: Using a join on 3 tables, select 5 columns from the 3 tables. Use syntax that would limit the output to 10 rows
SELECT iv.SKU, iv.PRODUCT_NAME,iv.BASE_PRICE,iv.LEAD_TIME_DAYS, iv.QUANTITY_AVAILABLE,
 au.AUTHORIZATION_STATUS, au.EXPIRATION_DATE, au.AUTHORIZATION_DATE, c.*, au.FK_DOCUMENT_ID
FROM INVENTORY iv
JOIN "AUTHORIZATION" au ON au.AUTHORIZATION_ID = iv.AUTHORIZATION_ID
join CONTACT c ON c.CONTACT_ID= iv.FK_CONTACT_ID
FETCH FIRST 10 ROWS ONLY;

--Q7: Select distinct rows using joins on 3 tables
SELECT DISTINCT iv.SKU, iv.PRODUCT_NAME,iv.BASE_PRICE,iv.LEAD_TIME_DAYS, iv.QUANTITY_AVAILABLE,
 au.AUTHORIZATION_STATUS, au.EXPIRATION_DATE, au.AUTHORIZATION_DATE, au.FK_CONTACT_ID, au.FK_DOCUMENT_ID,
 oi.ORDER_ITEM_ID, oi.QUANTITY, oi.UNIT_PRICE, oi.TRANSACTION_ID, oi.LINE_TOTAL
FROM INVENTORY iv
JOIN "AUTHORIZATION" au ON au.AUTHORIZATION_ID = iv.AUTHORIZATION_ID
JOIN ORDER_ITEMS oi ON oi.FK_INVENTORY_ID = iv.INVENTORY_ID;

-- Q8: Use GROUP BY and HAVING in a select statement using one or more tables
SELECT iv.SKU
FROM INVENTORY iv
JOIN "AUTHORIZATION" au ON au.AUTHORIZATION_ID = iv.AUTHORIZATION_ID
JOIN ORDER_ITEMS oi ON oi.FK_INVENTORY_ID = iv.INVENTORY_ID
GROUP BY iv.SKU, iv.QUANTITY_AVAILABLE HAVING iv.QUANTITY_AVAILABLE > 0;

-- Q9: Use IN clause to select data from one or more tables
SELECT * FROM PROFILE pf JOIN USERROLE ur ON pf.PROFILE_ID = ur.FK_PROFILE_ID  WHERE ur.FK_ROLE_ID  IN (100, 200, 300);

-- Q10: Select length of one column from one table (use LENGTH function)
SELECT LENGTH(ROLENAME) FROM ROLE;

-- Q11: Delete one record from one table. ROLLBACK afterwards so that the data will not be physically removed.
SAVEPOINT sp1;

SELECT * FROM PROFILE pf JOIN USERROLE ur ON pf.PROFILE_ID = ur.FK_PROFILE_ID  WHERE ur.FK_ROLE_ID  IN (100, 200, 300);
DELETE USERROLE WHERE FK_PROFILE_ID=3 AND FK_ROLE_ID=300;
SELECT * FROM PROFILE pf JOIN USERROLE ur ON pf.PROFILE_ID = ur.FK_PROFILE_ID  WHERE ur.FK_ROLE_ID  IN (100, 200, 300);

ROLLBACK TO SAVEPOINT sp1;

-- Q12: Update one record from one table. demonstrate table contents before and after the UPDATE.
SELECT * FROM PROFILE WHERE PROFILE_ID=3;
UPDATE PROFILE SET ACTIVE = FALSE WHERE PROFILE_ID=3;
SELECT * FROM PROFILE WHERE PROFILE_ID=3;

ROLLBACK TO SAVEPOINT sp1;
COMMIT;

-- Q13: Plans show
SELECT * FROM PLAN pl
LEFT OUTER JOIN DOCUMENT doc ON pl.FK_DOCUMENT_ID = doc.DOCUMENT_ID;

-- Q14: show authorized suppliers whose expiration date is greater than 30 days from now.
SELECT * FROM VIEW_SUPPLIER vsu
LEFT OUTER JOIN AUTHORIZATION aut ON aut.FK_CONTACT_ID = vsu.FK_CONTACT_ID
WHERE aut.AUTHORIZATION_STATUS='approved' AND aut.EXPIRATION_DATE > SYSDATE+30;

-- Q15:
SELECT r.ROLENAME, ct.FIRST_NAME, ct.LAST_NAME, pl.DESCRIPTION FROM SUBSCRIPTION srp
LEFT OUTER JOIN CONTACT ct ON srp.FK_CONTACT_ID = ct.CONTACT_ID
LEFT OUTER JOIN PROFILE pf ON pf.FK_CONTACT_ID = ct.CONTACT_ID
LEFT OUTER JOIN USERROLE ur ON ur.FK_PROFILE_ID = pf.PROFILE_ID
LEFT OUTER JOIN ROLE r ON r.ROLE_ID = ur.FK_ROLE_ID
LEFT OUTER JOIN PLAN pl ON srp.FK_PLAN_ID = pl.PLAN_ID;

-- Q16:
SELECT ct.FIRST_NAME, ct.LAST_NAME, adr.STREET, z.CITY, z.STATE, z.ZIP
FROM ADDRESS adr
LEFT OUTER JOIN CONTACT ct ON adr.FK_CONTACT_ID = ct.CONTACT_ID
LEFT OUTER JOIN ZIP z ON adr.ZIP = z.ZIP;

-- Q17:
SELECT COALESCE(vws.ROLENAME, vwsp.ROLENAME, vwc.ROLENAME, vwsys.ROLENAME) AS ROLENAME,
       COALESCE(vws.ACTIVE, vwsp.ACTIVE, vwc.ACTIVE, vwsys.ACTIVE) AS ACTIVE,
       ct.FIRST_NAME, ct.LAST_NAME, adr.STREET, z.CITY, z.STATE, z.ZIP
FROM ADDRESS adr
LEFT OUTER JOIN CONTACT ct ON adr.FK_CONTACT_ID = ct.CONTACT_ID
LEFT OUTER JOIN ZIP z ON adr.ZIP = z.ZIP
LEFT OUTER JOIN VIEW_STAFF vws ON vws.FK_CONTACT_ID = ct.CONTACT_ID
LEFT OUTER JOIN VIEW_SUPPLIER vwsp ON vwsp.FK_CONTACT_ID = ct.CONTACT_ID
LEFT OUTER JOIN VIEW_CUSTOMER vwc ON vwc.FK_CONTACT_ID = ct.CONTACT_ID
LEFT OUTER JOIN VIEW_SYSTEMROLE vwsys ON vwsys.FK_CONTACT_ID = ct.CONTACT_ID;

-- Q18: Verify rows in tables
SELECT 'CONTACT'        AS TABLE_NAME, COUNT(*) AS ROW_COUNT FROM CONTACT        UNION ALL
SELECT 'ZIP',                          COUNT(*)              FROM ZIP            UNION ALL
SELECT 'ADDRESS',                      COUNT(*)              FROM ADDRESS        UNION ALL
SELECT 'ROLE',                         COUNT(*)              FROM ROLE           UNION ALL
SELECT 'PROFILE',                      COUNT(*)              FROM PROFILE        UNION ALL
SELECT 'USERROLE',                     COUNT(*)              FROM USERROLE       UNION ALL
SELECT 'STAFF',                        COUNT(*)              FROM STAFF          UNION ALL
SELECT 'DOCUMENT',                     COUNT(*)              FROM DOCUMENT       UNION ALL
SELECT 'AUTHORIZATION',                COUNT(*)              FROM AUTHORIZATION  UNION ALL
SELECT 'PLAN',                         COUNT(*)              FROM PLAN           UNION ALL
SELECT 'INVENTORY',                    COUNT(*)              FROM INVENTORY      UNION ALL
SELECT 'EXHIBIT',                      COUNT(*)              FROM EXHIBIT        UNION ALL
SELECT 'ORDER_ITEMS',                  COUNT(*)              FROM ORDER_ITEMS    UNION ALL
SELECT 'INVOICE',                      COUNT(*)              FROM INVOICE        UNION ALL
SELECT 'SUBSCRIPTION',                 COUNT(*)              FROM SUBSCRIPTION   UNION ALL
SELECT 'TASK_HISTORY',                 COUNT(*)              FROM TASK_HISTORY
ORDER BY TABLE_NAME;

-- Q19: rough check uniqueness: across tables columns
SELECT RPAD(idx.TABLE_NAME,15,' '), COUNT( col.column_name ), COUNT( col.TABLE_NAME )
FROM user_indexes idx
JOIN user_ind_columns col ON ( col.index_name = idx.index_name )
WHERE idx.uniqueness = 'UNIQUE'
GROUP BY idx.TABLE_NAME;

-- Q20: Likely weak entity tables;
SELECT pk.table_name
       -- LISTAGG(pk.column_name, ', ') WITHIN GROUP (ORDER BY pk.position) AS primary_key_columns,
       -- LISTAGG(fk.column_name, ', ') WITHIN GROUP (ORDER BY fk.position) AS foreign_key_columns
FROM all_cons_columns pk
JOIN all_constraints pkc
  ON pk.constraint_name = pkc.constraint_name
 AND pk.owner = pkc.owner
JOIN all_cons_columns fk
  ON pk.table_name = fk.table_name
 AND pk.owner = fk.owner
JOIN all_constraints fkc
  ON fk.constraint_name = fkc.constraint_name
 AND fk.owner = fkc.owner
WHERE pkc.constraint_type = 'P'       -- Primary key
  AND fkc.constraint_type = 'R'       -- Foreign key
  AND pk.column_name = fk.column_name -- PK column is also FK column
  AND pk.owner = USER
GROUP BY pk.table_name
ORDER BY pk.table_name;-- CREATE SCHEMA

/
