SQL Queries Assignment
VerifiedAdded on 2019/11/29
|13
|559
|82
Practical Assignment
AI Summary
This document presents a practical assignment focused on SQL queries. It contains a series of eleven SQL queries designed to test understanding of database manipulation. Each query includes a problem statement, the SQL code used to solve the problem, and space for the results. The queries cover various aspects of SQL, including aggregate functions (SUM, AVG, COUNT), joins (INNER JOIN, NATURAL JOIN), subqueries, and filtering using WHERE and HAVING clauses. The queries operate on a database named 'assignment5', which is assumed to contain tables such as 'customer', 'invoice', 'line', 'product', and 'vendor'. The assignment aims to assess the student's ability to write and execute effective SQL queries to retrieve specific information from a relational database.

Student ID
Student Name
Course ID
Instructor Name
Student Name
Course ID
Instructor Name
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Table of Contents
Query 1........................................................................................................................................................3
Query 2........................................................................................................................................................4
Query 3........................................................................................................................................................5
Query 4........................................................................................................................................................6
Query 5........................................................................................................................................................7
Query 6........................................................................................................................................................8
Query 7........................................................................................................................................................9
Query 8......................................................................................................................................................10
Query 9......................................................................................................................................................11
Query 10....................................................................................................................................................12
Query 11....................................................................................................................................................13
Query 1........................................................................................................................................................3
Query 2........................................................................................................................................................4
Query 3........................................................................................................................................................5
Query 4........................................................................................................................................................6
Query 5........................................................................................................................................................7
Query 6........................................................................................................................................................8
Query 7........................................................................................................................................................9
Query 8......................................................................................................................................................10
Query 9......................................................................................................................................................11
Query 10....................................................................................................................................................12
Query 11....................................................................................................................................................13

Before executing any of the Query please select the database ‘assignment5’ by executing the below
command: use assignment5;
Query 1
Show sums of line totals for each invoice.
SQL Statement
SELECT INV_NUMBER, ROUND(SUM(LINE_TOTAL), 2) AS SUM
FROM line
GROUP BY INV_NUMBER;
Result
command: use assignment5;
Query 1
Show sums of line totals for each invoice.
SQL Statement
SELECT INV_NUMBER, ROUND(SUM(LINE_TOTAL), 2) AS SUM
FROM line
GROUP BY INV_NUMBER;
Result
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Query 2
Locate the record in the vendor table that does not have a value for the attribute V_STATE
SQL Statement
SELECT * FROM vendor
WHERE V_STATE IS NULL;
Result
Locate the record in the vendor table that does not have a value for the attribute V_STATE
SQL Statement
SELECT * FROM vendor
WHERE V_STATE IS NULL;
Result
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Query 3
Find the customers whose balance is greater than 200 but less than 300
SQL Statement
SELECT * FROM customer
WHERE CUS_BALANCE > 200 AND CUS_BALANCE < 300;
Result
Find the customers whose balance is greater than 200 but less than 300
SQL Statement
SELECT * FROM customer
WHERE CUS_BALANCE > 200 AND CUS_BALANCE < 300;
Result

Query 4
Using natural join and having, list the customers whose invoice total is greater than 200
SQL Statement
SELECT cus.CUS_CODE, cus.CUS_LNAME, cus.CUS_FNAME, cus.CUS_INITIAL,
cus.CUS_AREACODE, cus.CUS_PHONE, cus.CUS_BALANCE, inv.INV_TOTAL
FROM customer AS cus
INNER JOIN invoice AS inv ON cus.CUS_CODE = inv.CUS_CODE
HAVING inv.INV_TOTAL > 200;
Result
Using natural join and having, list the customers whose invoice total is greater than 200
SQL Statement
SELECT cus.CUS_CODE, cus.CUS_LNAME, cus.CUS_FNAME, cus.CUS_INITIAL,
cus.CUS_AREACODE, cus.CUS_PHONE, cus.CUS_BALANCE, inv.INV_TOTAL
FROM customer AS cus
INNER JOIN invoice AS inv ON cus.CUS_CODE = inv.CUS_CODE
HAVING inv.INV_TOTAL > 200;
Result
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Query 5
Show the names of the customers for whom we have invoices.
SQL Statement
SELECT DISTINCT (cus.CUS_FNAME), cus.CUS_LNAME
FROM customer AS cus
INNER JOIN invoice AS inv ON cus.CUS_CODE = inv.CUS_CODE;
Result
Show the names of the customers for whom we have invoices.
SQL Statement
SELECT DISTINCT (cus.CUS_FNAME), cus.CUS_LNAME
FROM customer AS cus
INNER JOIN invoice AS inv ON cus.CUS_CODE = inv.CUS_CODE;
Result
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Query 6
Using natural join and having, list the customers whose invoice total is greater than the average
SQL Statement
SELECT cus.CUS_CODE, cus.CUS_LNAME, cus.CUS_FNAME, cus.CUS_INITIAL,
cus.CUS_AREACODE, cus.CUS_PHONE, cus.CUS_BALANCE, inv.INV_TOTAL
FROM customer AS cus
INNER JOIN invoice AS inv ON cus.CUS_CODE = inv.CUS_CODE
HAVING inv.INV_TOTAL > (SELECT ROUND(AVG(INV_TOTAL),2) FROM INVOICE);
Result
Using natural join and having, list the customers whose invoice total is greater than the average
SQL Statement
SELECT cus.CUS_CODE, cus.CUS_LNAME, cus.CUS_FNAME, cus.CUS_INITIAL,
cus.CUS_AREACODE, cus.CUS_PHONE, cus.CUS_BALANCE, inv.INV_TOTAL
FROM customer AS cus
INNER JOIN invoice AS inv ON cus.CUS_CODE = inv.CUS_CODE
HAVING inv.INV_TOTAL > (SELECT ROUND(AVG(INV_TOTAL),2) FROM INVOICE);
Result

Query 7
Show the names of the customers for whom we DO NOT have invoices.
SQL Statement
SELECT CUS_FNAME, CUS_LNAME
FROM customer
WHERE CUS_CODE NOT IN (SELECT DISTINCT CUS_CODE FROM invoice);
Result
Show the names of the customers for whom we DO NOT have invoices.
SQL Statement
SELECT CUS_FNAME, CUS_LNAME
FROM customer
WHERE CUS_CODE NOT IN (SELECT DISTINCT CUS_CODE FROM invoice);
Result
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Query 8
List the first name and last name of the customers and how many times a customer has
generated an invoice.
SQL Statement
SELECT cus.CUS_FNAME, cus.CUS_LNAME, COUNT(inv.INV_NUMBER)
FROM customer AS cus
INNER JOIN invoice AS inv
ON cus.CUS_CODE = inv.CUS_CODE
GROUP BY inv.CUS_CODE;
Result
List the first name and last name of the customers and how many times a customer has
generated an invoice.
SQL Statement
SELECT cus.CUS_FNAME, cus.CUS_LNAME, COUNT(inv.INV_NUMBER)
FROM customer AS cus
INNER JOIN invoice AS inv
ON cus.CUS_CODE = inv.CUS_CODE
GROUP BY inv.CUS_CODE;
Result
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Query 9
List all purchases by customer 10018
SQL Statement
SELECT inv.INV_NUMBER, inv.INV_DATE, lin.P_CODE, pro.P_DESCRIPT,
inv.INV_SUBTOTAL, inv.INV_TAX, inv.INV_TOTAL, lin.LINE_UNITS, lin.LINE_PRICE,
lin.LINE_TOTAL
FROM invoice AS inv
INNER JOIN line AS lin
ON inv.INV_NUMBER = lin.INV_NUMBER
INNER JOIN product AS pro
ON lin.P_CODE = pro.P_CODE
WHERE inv.CUS_CODE = 10018;
Result
List all purchases by customer 10018
SQL Statement
SELECT inv.INV_NUMBER, inv.INV_DATE, lin.P_CODE, pro.P_DESCRIPT,
inv.INV_SUBTOTAL, inv.INV_TAX, inv.INV_TOTAL, lin.LINE_UNITS, lin.LINE_PRICE,
lin.LINE_TOTAL
FROM invoice AS inv
INNER JOIN line AS lin
ON inv.INV_NUMBER = lin.INV_NUMBER
INNER JOIN product AS pro
ON lin.P_CODE = pro.P_CODE
WHERE inv.CUS_CODE = 10018;
Result

Query 10
List the names of the vendors who provided products
SQL Statement
SELECT DISTINCT ven.V_Name
FROM vendor ven
INNER JOIN product pro
ON ven.V_Code = pro.V_Code
WHERE pro.V_CODE <> 0;
Result
List the names of the vendors who provided products
SQL Statement
SELECT DISTINCT ven.V_Name
FROM vendor ven
INNER JOIN product pro
ON ven.V_Code = pro.V_Code
WHERE pro.V_CODE <> 0;
Result
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 13
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.