Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

With SQL, you can add, subtract, multiply, and divide...just like you would do within an Excel file. Each mathematical operator is represented like so: Add:

With SQL, you can add, subtract, multiply, and divide...just like you would do within an Excel file.

Each mathematical operator is represented like so:

Add: +

Subtract: -

Multiply: *

Divide: /

When using mathematical operations, you can only do the arithmetic across a row, not up and down a column (mathematical operations within columns requires an aggregate function, which will be covered in a much later lesson).

SELECT product_code, original_price, current_price, original_price - current_price AS discount_amount FROM example.products; 

In the example above, you can see that the value in current_price is being subtracted from the value in original_price. Since every row in each of those columns has a value, the mathematical operation is being done across each row.

Note from Jason: You may have noticed the AS discount_amount portion of the last query. This is a calculated column and is not saved data in the database. To have a descriptive name on this column, you can rename the resulting column in your query by writing AS and then whatever name you choose. If you want spaces, symbols, or capitalized letters in the name, you will have to format it with quotes: AS "Discount Amount". If you don't format the name of the column with quotes then it can only be one word and cannot begin with a number. Throughout this course, most column names will not use quotes.

Mathematical operations can also be done in the WHERE clause, they aren't just limited to the SELECT statement.

Something like the example below is perfectly fine!

SELECT product_code, full_name FROM example.products WHERE (original_price - current_price) = 40; 

The subtraction of the two fields was put in parentheses to show that the mathematical operation is clearly separate from the =. And remember from your old math classes in school: whatever is within the parentheses is calculated first!

The data columns you are calculating are not displayed in the results. If you want to see those as well, add them to the column list after the SELECT (and remember the , (commas). You can also change the 40 to another amount to see how this changes the results displayed.

The Situation:

A member of the Merchandising team for Monday Boots is expecting a shipment of additional items for one specific product. She wants to see what percentage of the future inventory level for that product is currently in stock.

The current level of inventory is represented in the retail.inventory table by the total_on_hand column, while the number of items included in upcoming shipments is represented by the total_on_order column. Combining those two columns would give you the future inventory level.

Using the retail.inventory table, pull the current stock percentage out of the future stock level for the product with a product_id = 21.

Hints:

The percentage can be calculated by dividing the current inventory level by the current inventory level plus the total number of items shipping in the future. Remember your math and the order of operations.

You will need to multiply one of the fields in your calculation by 1.00 to allow there to be decimal values in your answer. Try running your query without doing so to see what result you get.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2023 Workshops 34th International Conference Dexa 2023 Penang Malaysia August 28 30 2023 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Maqbool Khan

1st Edition

303139688X, 978-3031396885

More Books

Students also viewed these Databases questions

Question

Explain basic guidelines for effective multicultural communication.

Answered: 1 week ago

Question

Identify communication barriers and describe ways to remove them.

Answered: 1 week ago

Question

Explain the communication process.

Answered: 1 week ago