Question

In: Computer Science

Write a query that returns all of the main information that are associated with the purchase...

Write a query that returns all of the main information that are associated with the purchase orders that have an amount greater than 70% of the average amount of all purchase orders, the output should be sorted by the largest purchase order amount

Solutions

Expert Solution

I have answered this based on a dummy database table but should work on a different schema as well.  

Database table

PurchaseOrders
ID OrderName OrderAmount OrderItems OrderDate OrderPerson
1 ABC 1000 10 Oct-18 DEF
2 XYZ 1500 4 Nov-19 DEF
3 TUV 5000 6 Dec-20 WXY
4 XUV 10000 100 Jan-19 ZUH
5 KUV 4000 1 Mar-18 DUH

Snapshot:

It is called "PurchaseOrders" and has 6 fields (ID being the primary key)

To do the above question, I have used two SQL queries:

Average order amount query:

This calculates the average amount of the the order from the above table.

SELECT avg(PurchaseOrders.OrderAmount) AS AverageOrder
FROM PurchaseOrders;

Snapshot:

Query output:

In this particular example, the average comes out to be 4,300. It would change with the values in the table.

Final query:

This will extract the records from the PurchaseOrders table where the order amount is greater than 70% of the average order amount (in this case - 3,010).

SELECT *
FROM PurchaseOrders, [Average order]
WHERE (((PurchaseOrders.OrderAmount)>0.7*[Average order].AverageOrder))
ORDER BY PurchaseOrders.OrderAmount DESC;

Snapshot:

Description:

1st line - selects all the fields from the table. However, if you wish to select only a few you can replace * with PurchaseOrders.ID, PurchaseOrders.OrderAmount so on and so forth.

2nd line - Extracts the information from PurchaseOrders table and Average order query

3rd line - Checks the condition if the order amount in PurchaseOrders table is more than 70% of the average amount of all the orders

4th line - Sorts the output by the largest purchase order amount

Output snapshot:

Let me know if anything is not clear.


Related Solutions

Write a query to display the columns listed below. For each customer the query should show...
Write a query to display the columns listed below. For each customer the query should show the current system date, the current day (when you do the problem the date and day will be different), the number of characters in the member last name, the last date the customer rented a video and how many total videos the person rented. /* Database Systems, 9th Ed., Coronel/MOrris/Rob */ /* Type of SQL : MySQL */ CREATE SCHEMA IF NOT EXISTS TINY_VIDEO;...
1.Write an SQL query that retrieves all pairs of suppliers who supply the same product, along...
1.Write an SQL query that retrieves all pairs of suppliers who supply the same product, along with their product purchase price if applicable. 2.Create a view SUPPLIEROVERVIEW that retrieves, for each supplier, the supplier number, the supplier name, and the total amount of quantities ordered. Once created, query this view to retrieve suppliers for whom the total ordered quantity exceeds 30. 3.Write a nested SQL query to retrieve all purchase order numbers of purchase orders that contain either sparkling or...
Choose all the optional query block in “Select “retrieval query? A. Select B. Where C. From...
Choose all the optional query block in “Select “retrieval query? A. Select B. Where C. From D. Order By A NULL value means A. Value missing B. Value unknown C. More than 1000 D. All the above Delete from DEF; /*DEF is a table/relation*/ What action does this command perform? A. Delete the table B. Delete all the rows from the table C. Deletes a column D. Deletes the top 10 tuples A attribute/column, fName, is of Data Type varchar(100)....
I want to return a query that returns film titles that contain letter 'a' followed by...
I want to return a query that returns film titles that contain letter 'a' followed by repeating characters from the table below. For example, Married Go (a film in the table) has repeating characters following the letter a. Table: CREATE TABLE film_cat_rate ( category character varying(25), title character varying(255), rental_rate numeric(4,2) ); INSERT INTO film_cat_rate (category, title, rental_rate) VALUES ('New', 'Amistad Midsummer', 2.99); INSERT INTO film_cat_rate (category, title, rental_rate) VALUES ('Drama', 'Apollo Teen', 2.99); INSERT INTO film_cat_rate (category, title, rental_rate)...
From the following mySql query create a function that returns a nth value of a table:...
From the following mySql query create a function that returns a nth value of a table: SELECT name, (SUM(price * quantity)) AS TotalPrice FROM Items NATURAL JOIN Inventory GROUP BY name ORDER BY (TotalPrice) DESC LIMIT 1, OFFSET 1; Shema: Inventory(name: String,item:String,quantity:integer) Items(item: String, brand: String, price:double)
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all letters occurring in the parameter text. For example: if __name__ == '__main__': d = letter_frequencies('hello, world') print(d) # show the contents of this dictionary will produce the following output: {'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.1, 'e': 0.1, 'f': 0.0, 'g': 0.0, 'h': 0.1, 'i': 0.0, 'j': 0.0, 'k': 0.0, 'l': 0.3, 'm': 0.0, 'n': 0.0, 'o': 0.2, 'p': 0.0, 'q': 0.0,'r': 0.1,...
How to write a query in SQL using the Group by function to decide if the...
How to write a query in SQL using the Group by function to decide if the results of the diet worked. There are 6 columns ID, Gender, treatment, start_weight_kg, end_weight_kg, and correct change (end weight minus start weight). With the information given write a SQL to determine if the the results(correct change) worked. Question is not incomplete.
Write a query to return the date and day of the week of the first day...
Write a query to return the date and day of the week of the first day of the month two years from today. If today is 10/16/20, then the expect output is 10/01/2022 and the day of the week is thursday. I am using postgresql.
QUESTION 34 seqAID is a valid Oracle sequence: select seqAID.nextval from dual; the above query returns...
QUESTION 34 seqAID is a valid Oracle sequence: select seqAID.nextval from dual; the above query returns 1500, what the following query will return? Select seqAID.nextval From dual; A. 1499 B. 1500 C. 1501 D. 1502 QUESTION 35 after running the query in previous Question, what the following query will return? Select seqAID.nextval-1 From dual; A. 1499 B. 1500 C. 1501 D. 1502 QUESTION 36 after running queries in previous two Questions (34 & 35), what the following query will return?...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT