A fast food joint keeps a database of menu items and calories.
A very smart database engineer realised that this design was inefficient.
One of the fields Other calories doesn't need to be stored in the database.
It could just be calculated when required ie when a query is run.
The calculation is:
'Calories' - 'Calories from fat'
This approach saves memory because data in the Other calories field does not have to saved in the table.
We can delete the Other calories field from the table.
Here is an attempt at creating a calculated field in an SQL query.
SELECT ('Calories' - 'Calories from fat') AS 'Other calories'
FROM menudata;
Download the csv file about peripheral devices. Save it in a project folder.
Import the data into a XAMPP database table.
Remember to ask yourself a couple of questions:
Design the following queries. Paste your SQL into the worksheet where indicated.
This query creates a new field called Threshold which displays 80% of the Calories value.
The output only shows the Item and the Threshold fields, where the Category is Breakfast
Number of records:
This query creates the same calculated field, Threshold, as Query 1.
It displays the Category, Item and Threshold fields
but only selects records related to Chicken items.
Number of records: 40
This query calculates the average Threshold of all records containing Chicken items and groups the items by Category
It display the Category, Category Count and Average Threshold fields
And ordered in ascending order of Average Threshold.
Number of records: 4
The output should look like this:
Adapt Query 3 so that the output is shown in descending order of Average Threshold
Database engineers are constantly thinking of new ways to use memory efficiently. Computer memory is not free and efficiency reduces costs. Calculated fields allow only necessary data to be stored permanently and when a user interacts with the database, any fields relying on calculations can be displayed.
database sigma querycriteriaimport evidence table integer screenshot record text ascending field double descending data type Yes/No