(ii) Display Name and Total Salary (sum of Salary and Allowance) of all personals. The column heading ‘Total Salary’ should also be displayed.
Answer by student:
SELECT Name, (Salary + Allowance) AS
"Total Salary" FROM Personal;
Detailed answer by teachoo:
- The question asks us to write a SQL query to display Name and Total Salary (sum of Salary and Allowance) of all personals. The column heading ‘Total Salary’ should also be displayed. To do this, we use the SELECT statement, which retrieves data from a table. The syntax of the SELECT statement is:
SELECT column_name(s) FROM table_name WHERE condition;
-
- The column_name(s) are the names of the columns that we want to display. We can use a comma (,) to separate multiple columns. In this case, we want to display Name and Total Salary.
- The table_name is the name of the table that we want to retrieve data from. In this case, it is Personal.
- The condition is an optional clause that specifies which rows to display. If we omit the condition, all rows in the table will be displayed. In this case, we don’t have any condition, so we display all rows.
- The semicolon (;) is used to end the SQL statement.
- To calculate the Total Salary as the sum of Salary and Allowance, we use the + operator, which adds two values together.
- To display the column heading ‘Total Salary’, we use the AS keyword, which assigns an alias or a temporary name to a column or a table. For example, if we write (Salary + Allowance) AS “Total Salary”, then the column will be displayed as Total Salary instead of (Salary + Allowance).
So, the SQL query to display Name and Total Salary (sum of Salary and Allowance) of all personals is:
SELECT Name, (Salary + Allowance) AS
"Total Salary" FROM Personal;