SQLite Assignment – SQL Queries And Answers
Task 1: Show sums of line units for each invoice
select v.inv_number,sum(l.line_total) from invoice v inner join line l on l.inv_number=v.inv_number group by v.inv_number;
+————+——————–+
| inv_number | sum(ln.line_total) |
+————+——————–+
| 1001 | 24.9399995803833 |
| 1002 | 9.979999542236328 |
| 1003 | 153.8499984741211 |
| 1004 | 34.86999988555908 |
| 1005 | 70.44000244140625 |
| 1006 | 397.82998752593994 |
| 1007 | 34.96999931335449 |
| 1008 | 399.1500062942505 |
+————+——————–+
— Query 2
select prd.* from product prd where prd.v_code=0;
+———-+————————-+———————+——-+——-+———+————+——–+
| P_CODE | P_DESCRIPT | P_INDATE | P_QOH | P_MIN | P_PRICE | P_DISCOUNT | V_CODE |
+———-+————————-+———————+——-+——-+———+————+——–+
| 23114-AA | Sledge hammer, 12 lb. | 0000-00-00 00:00:00 | 8 | 5 | 14.4 | 0.05 | 0 |
| PVC23DRT | PVC pipe, 3.5-in., 8-ft | 0000-00-00 00:00:00 | 188 | 75 | 5.87 | 0 | 0 |
+———-+————————-+———————+——-+——-+———+————+——–+
— Query 3
select nvi.* from invoice nvi where nvi.inv_subtotal>25 and nvi.inv_subtotal<75;
+————+———-+———————+————–+———+———–+
| INV_NUMBER | CUS_CODE | INV_DATE | INV_SUBTOTAL | INV_TAX | INV_TOTAL |
+————+———-+———————+————–+———+———–+
| 1004 | 10011 | 0000-00-00 00:00:00 | 34.97 | 2.8 | 37.77 |
| 1005 | 10018 | 0000-00-00 00:00:00 | 70.44 | 5.64 | 76.08 |
| 1007 | 10015 | 0000-00-00 00:00:00 | 34.97 | 2.8 | 37.77 |
+————+———-+———————+————–+———+———–+
— Query 4
select nvi.* from invoice nvi having min(nvi.inv_subtotal);
+————+———-+———————+————–+———+———–+
| INV_NUMBER | CUS_CODE | INV_DATE | INV_SUBTOTAL | INV_TAX | INV_TOTAL |
+————+———-+———————+————–+———+———–+
| 1001 | 10014 | 0000-00-00 00:00:00 | 24.9 | 1.99 | 26.89 |
+————+———-+———————+————–+———+———–+
— Query 5
select vnr.v_code,vnr.v_name from vendor vnr where vnr.v_code in (select pdt.v_code from product pdt);
+——–+—————–+
| v_code | v_name |
+——–+—————–+
| 21225 | Bryson, Inc. |
| 21231 | D&E Supply |
| 21344 | Gomez Bros. |
| 23119 | Randsets Ltd. |
| 24288 | ORDVA, Inc. |
| 25595 | Rubicon Systems |
+——–+—————–+
— Query 6
select vnr.v_code from vendor vnr where vnr.v_code not in (select pdt.v_code from product pdt);
+——–+
| v_code |
+——–+
| 21226 |
| 22567 |
| 24004 |
| 25443 |
| 25501 |
+——–+
— Query 7
select vnr.v_code,vnr.v_name from vendor vnr where vnr.v_code not in (select pdt.v_code from product pdt);
+——–+—————-+
| v_code | v_name |
+——–+—————-+
| 21226 | SuperLoo, Inc. |
| 22567 | Dome Supply |
| 24004 | Brackman Bros. |
| 25443 | B&K, Inc. |
| 25501 | Damal Supplies |
+——–+—————-+
— Query 8
select vnr.v_code, count(pd.p_code) from vendor vnr inner join product pd on vnr.v_code=pd.v_code group by vnr.v_code;
+——–+——————+
| v_code | count(pr.p_code) |
+——–+——————+
| 21225 | 2 |
| 21231 | 1 |
| 21344 | 3 |
| 23119 | 2 |
| 24288 | 3 |
| 25595 | 3 |
+——–+——————+
Task 2: Show the details of the products that do not have a value for the attribute v_code
— Query 9
select vnr.v_code,vnr.v_name, count(pd.p_code) from vendor vnr inner join product pd on vnr.v_code=pd.v_code group by vnr.v_code;
+——–+—————–+——————+
| v_code | v_name | count(pr.p_code) |
+——–+—————–+——————+
| 21225 | Bryson, Inc. | 2 |
| 21231 | D&E Supply | 1 |
| 21344 | Gomez Bros. | 3 |
| 23119 | Randsets Ltd. | 2 |
| 24288 | ORDVA, Inc. | 3 |
| 25595 | Rubicon Systems | 3 |
+——–+—————–+——————+
— Query 10
select nvr.* from invoice nvr where nvr.cus_code=10011;
+————+———-+———————+————–+———+———–+
| INV_NUMBER | CUS_CODE | INV_DATE | INV_SUBTOTAL | INV_TAX | INV_TOTAL |
+————+———-+———————+————–+———+———–+
| 1002 | 10011 | 0000-00-00 00:00:00 | 9.98 | 0.8 | 10.78 |
| 1004 | 10011 | 0000-00-00 00:00:00 | 34.97 | 2.8 | 37.77 |
| 1008 | 10011 | 0000-00-00 00:00:00 | 399.15 | 31.93 | 431.08 |
+————+———-+———————+————–+———+———–+
— Query 11
select em.* from emp em where em.emp_areacode=615;
+———+———–+————+———–+————-+———————+———————+————–+———–+———+
| EMP_NUM | EMP_TITLE | EMP_LNAME | EMP_FNAME | EMP_INITIAL | EMP_DOB | EMP_HIRE_DATE | EMP_AREACODE | EMP_PHONE | EMP_MGR |
+———+———–+————+———–+————-+———————+———————+————–+———–+———+
| 100 | Mr. | Kolmycz | George | D | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 324-5456 | 0 |
| 101 | Ms. | Lewis | Rhonda | G | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 324-4472 | 100 |
| 103 | Ms. | Jones | Anne | M | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 898-3456 | 100 |
| 105 | Mr. | Williams | Robert | D | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 890-3220 | 0 |
| 106 | Mrs. | Smith | Jeanine | K | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 324-7883 | 105 |
| 107 | Mr. | Diante | Jorge | D | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 890-4567 | 105 |
| 108 | Mr. | Wiesenbach | Paul | R | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 897-4358 | 0 |
| 111 | Mr. | Washington | Rupert | E | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 890-4925 | 105 |
| 112 | Mr. | Johnson | Edward | E | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 898-4387 | 100 |
| 113 | Ms. | Smythe | Melanie | P | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 324-9006 | 105 |
| 115 | Mrs. | Saranda | Hermine | R | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 324-5505 | 105 |
| 116 | Mr. | Smith | George | A | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 615 | 890-2984 | 108 |
+———+———–+————+———–+————-+———————+———————+————–+———–+———+
— Query 12
select pd.* from product pd inner join line li on li.p_code=pd.p_code where li.line_price>100;
+———-+————————————-+———————+——-+——-+———+————+——–+
| P_CODE | P_DESCRIPT | P_INDATE | P_QOH | P_MIN | P_PRICE | P_DISCOUNT | V_CODE |
+———-+————————————-+———————+——-+——-+———+————+——–+
| 2232/QTY | B&D jigsaw, 12-in. blade | 0000-00-00 00:00:00 | 8 | 5 | 109.92 | 0.05 | 24288 |
| 89-WRE-Q | Hicut chain saw, 16 in. | 0000-00-00 00:00:00 | 11 | 5 | 256.99 | 0.05 | 24288 |
| WR3/TT3 | Steel matting, 4’x8’x1/6″, .5″ mesh | 0000-00-00 00:00:00 | 18 | 5 | 119.95 | 0.1 | 25595 |
+———-+————————————-+———————+——-+——-+———+————+——–+