w3resource

SQL Exercise: Using precedence with specified condition

SQL Boolean Operator Statement: Exercise-10 with Solution

Write a SQL query that displays order number, purchase amount, and the achieved and unachieved percentage (%) for those orders that exceed 50% of the target value of 6000.

Sample table: orders

ord_no      purch_amt   ord_date    customer_id  salesman_id
----------  ----------  ----------  -----------  -----------
70001       150.5       2012-10-05  3005         5002
70009       270.65      2012-09-10  3001         5005
70002       65.26       2012-10-05  3002         5001
70004       110.5       2012-08-17  3009         5003
70007       948.5       2012-09-10  3005         5002
70005       2400.6      2012-07-27  3007         5001
70008       5760        2012-09-10  3002         5001
70010       1983.43     2012-10-10  3004         5006
70003       2480.4      2012-10-10  3009         5003
70012       250.45      2012-06-27  3008         5002
70011       75.29       2012-08-17  3003         5007
70013       3045.6      2012-04-25  3002         5001

Sample Solution :

-- This query selects specific columns 'ord_no', 'purch_amt', and calculates two additional columns.
-- It calculates the percentage of the achieved and unachieved amounts based on a target of 6000.
SELECT ord_no, purch_amt,
       -- Calculates the percentage of the achieved amount.
       (100 * purch_amt) / 6000 AS "Achieved %",
       -- Calculates the percentage of the unachieved amount.
       (100 * (6000 - purch_amt) / 6000) AS "Unachieved %"
-- Specifies the table from which to retrieve the data (in this case, 'orders').
FROM orders
-- Filters the rows to only include those where the percentage of the achieved amount is greater than 50%.
WHERE (100 * purch_amt) / 6000 > 50;

Output of the Query:

ord_no	purch_amt	Achieved %			Unachieved %
70008	5760.00		96.0000000000000000	4.0000000000000000
70013	3045.60		50.7600000000000000	49.2400000000000000

Code Explanation:

The SQL query that is selecting specific columns "ord_no, purch_amt, (100*purch_amt)/6000 AS "Achieved %", (100(6000-purch_amt)/6000) AS "Unachieved %" from the 'orders' table where (100*purch_amt)/6000 is greater than 50.
Using mathematical expressions, the query calculates the percentage of achieved and unachieved amounts based on the value of the "purch_amt" column and a fixed value of 6000 as the value of the "purch_amt" column.
This query will return all rows in the table 'orders' that have a achieved percentage of greater than 50 as well as the selected columns in each row.

Explanation :

Syntax of display order number, purchase amount, the achieved and unachieved percentage (%) for those order which exceeds the 50% of target value of 6000

Visual presentation:

Result of display order number, purchase amount, the achieved and unachieved percentage (%) for those order which exceeds the 50% of target value of 6000

Practice Online


Query Visualization:

Duration:

Query visualization of Using precedence with specified condition - Duration

Rows:

Query visualization of Using precedence with specified condition - Rows

Cost:

Query visualization of Using precedence with specified condition - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Using NOT, OR & AND operators.
Next SQL Exercise: Find those employees whose last name is Dosni or Mardy.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-boolean-operator-exercise-10.php