SQL Exercises: Salesmen commission with the percent sign (%)
SQL Formatting Output: Exercise-1 with Solution
From the following table, write a SQL query to select all the salespeople. Return salesman_id, name, city, commission with the percent sign (%).
Sample table: salesman
salesman_id | name | city | commission -------------+------------+----------+------------ 5001 | James Hoog | New York | 0.15 5002 | Nail Knite | Paris | 0.13 5005 | Pit Alex | London | 0.11 5006 | Mc Lyon | Paris | 0.14 5007 | Paul Adam | Rome | 0.13 5003 | Lauson Hen | San Jose | 0.12
Sample Solution:
-- This query selects specific columns from the 'salesman' table and performs a calculation on the 'commission' column.
-- It also appends '%' to the result of the commission calculation.
SELECT salesman_id, name, city, '%', commission * 100
-- Specifies the table from which to retrieve the data (in this case, 'salesman').
FROM salesman;
Output of the Query:
salesman_id name city ?column? ?column? 5001 James Hoog New York % 15.00 5002 Nail Knite Paris % 13.00 5005 Pit Alex London % 11.00 5006 Mc Lyon Paris % 14.00 5007 Paul Adam Rome % 13.00 5003 Lauson Hen San Jose % 12.00
Code Explanation:
The said query in SQL retrieves data from the "salesman" table and returns the following columns:
salesman_id
name
city
The string "%"
The result of commission column multiplied by 100.
The percentage symbol in the query used to represent the data in the "commission" column as a percentage.
Relational Algebra Expression:
Relational Algebra Tree:
Explanation:
Explanation:
Visual presentation :
Practice Online
Query Visualization:
Duration:
Rows:
Cost:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: SQL Formatting Output Exercises Home
Next SQL Exercise: Number of orders booked for each day.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql-exercises/sql-formatting-output-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics